Getting Started with Git
Git is a version control system that helps you track changes in code, collaborate with others, and manage different versions of a project. This guide introduces the core concepts and basic commands to get you started with Git.
What is Version Control?
Version control allows you to:
Track the history of changes to files
Revert to previous versions if something breaks
Work on new features or fixes without affecting the main project
Collaborate with others without overwriting each other’s work
Git is a distributed version control system, meaning every user has a complete copy of the project history.
Installing Git
sudo apt update
sudo apt install git
Basic Git Workflow
Initialize a repository
Turns a directory into a Git repository
git init
NOTE: This creates a hidden
.gitsubdirectory within the directory that contains necessary structures that git uses to track changes, manage histroy, and perform version control operations for the project directory. If you want to collaborate with others, or back up your code, you will need to create a remote repositroy on GitHub, or GitLab. See: “Connecting to a Remote Repository” below.
Check repository status
Allows you to monitor changes made since your last commit
git status
Add files to a staging area
Add changes to a your staging area that are ready to be included in your next commit
git add filename
git add . # Adds all files in the directory
git add -p # Sorts through changes to add one by one
Commit changes
Create a permanent snapshot of your changes in your local repository
git commit -m "Describe what you changed or added"
View commit history
Shows a list of commits made in reverse chronological order
git log
Connecting to a Remote Repository
To collaborate with others, you’ll typically push your code to a platform like GitHub, GitLab, or Bitbucket. You will need to create the repository from GitHub, or GitLab’s UI. Once created, you can add your initialized local repository to the remote repository by following these steps:
Add a remote repository
git remote add origin https://github.com/yourusername/your-repo.git
Push your code
git push -u origin main
The
-u(upstream) links your local branch to a remote repository.
Branching and Merging
Creating a new branch
git checkout -b branch-name
Switch branches
git checkout branch-name
Merge a branch
git merge branch-name
Delete a branch
git branch -d branch-name
Cloning a Repository
To download a remote project and work on it:
git clone https://github.com/username/project.git
.gitignore File
Use a .gitignore file to exclude files or directories from being tracked by Git.
Example:
*.log
.env
__pycache__/
node_modules/
Undoing Changes
Unstage a file
git reset HEAD filename
Discard changes in a file
git checkout -- filename
Revert a commit
git revert <commit-id>
Remove a file from staging
git rm <file-name>
Resources
GitHub Learning Lab: bot lead series of practical projects to level up your GitHub skills
Git Branching: a visual and interactive way to learn Git on the web
Git Book (free): a free complete and indepth Git resource
Git Cheatsheet (GitHub): features the most important and commonly used Git commands for easy reference.
Git-it: a desktop app that teaches you how to use Git and GitHub on the command line.
Missing Semester of Your CS Education: Version Control (Git)