Git and Github Bootcamp
Collection of notes and exercises for learning how to use Git and Github.
Table of Contents
1. Git Core
Git is a version control system that tracks and manages changes to files over time. It allows users to review and compare previous file versions, revert changes, and more. For initial setup and configuration of git, refer to the official git documentation.
One of Git's key concepts is the HEAD, a reference to your current location in the repository. It acts as a pointer to the currently checked-out branch, which in turn points to the latest commit on that branch. By default, upon initializing or cloning a repository, HEAD typically points to the last commit of the default branch (main or master). However, its position can change as we navigate through the repository, either by switching branches or by checking out specific commits, potentially leading to a detached HEAD state.
Let's illustrate this with an example:
- First commit: Both the
master branch and HEAD point to this commit.
- Second commit: The
master branch and HEAD update to point to this new commit.
- Create "new-feature" branch: This branch, now also pointed to by
HEAD, mirrors the master branch's current commit.
- Commit in "new-feature": This new commit updates the
new-feature branch and HEAD, leaving master unchanged.
- Switch back to master:
HEAD updates to point back to the master branch, leaving the branch pointers untouched.
We can consider the branch references as "bookmarks" in a book that we can use to jump to a specific page. These bookmarks keep track of different paths in the story. The HEAD is the page we are currently reading. At any given time, we're only reading from one page (the HEAD), but we can have multiple bookmarks in the book (branches). This way, we can keep track of where we left off in different parts of the story (different lines of development).
Also, it's important to note that we can move to a specific page without using a bookmark (i.e., without using a branch reference) by using the git switch --detach (or git checkout) command and specifying the commit hash. This is similar to remembering a specific page number and going directly to it. However, without a bookmark, it might be harder to remember where you were, especially if you move to other pages (commits). That's why it's often easier to work with branches: they are like bookmarks that help you keep track of where you've been in the project history.
After branching off from the master branch, we can make changes to the code and commit them to the new-feature branch. The master branch remains unchanged. When we're done with the new feature, we can merge the new-feature branch back into the master branch. This will combine all the changes made in the new-feature branch with the master branch since their common ancestor (base) commit.
It's important to note that we merge branches, not individual commits (see 6.1. Merge Branches, Not Commits) . When we perform a merge operation, Git identifies the common ancestor commit of the two branches and integrates the changes made in both branches since that commit into the current HEAD branch. If there are conflicting changes, these will need to be resolved manually.
2. Amend, Switching, Branching, and Merging
Amend the most recent commit:
git add <forgotten file>
git commit --amend -m 'New commit message'
Create a New Branch and Switch to It
Creating a new branch and switching to it are often steps performed in sequence when you're starting work on a new feature, bugfix, or experiment. Traditionally, git checkout was used for these tasks, but Git has introduced more intuitive, task-specific commands like git switch and git restore for better clarity and separation of concerns.
The git switch command is more specific to the task of switching branches and makes the action clearer:
git switch <branch_name>
If you want to create a new branch and switch to it in one go, you can do:
git switch -c <new_branch_name>
This is similar to git checkout -b <new_branch_name>, but it's clearer in intent.
Always Stash or Commit Changes Before Switching! Before you switch branches, it's good practice to make sure your working directory is clean. There are two primary ways to do this:
Committing Changes: If the changes are complete, and you're ready to save them, commit those to the current branch.
Stashing Changes: If your changes are not complete and you're not ready to commit, you can "stash" them. This takes your changes and stores them in a temporary area so that you can reapply them later.
Why It's Important: If you try to switch branches with a dirty working directory, i.e., with uncommitted changes, Git will throw an error if those changes conflict with the branch you're trying to switch to. Even if there's no conflict, carrying uncommitted changes across branches can make for a confusing history and can complicate things like code reviews.
Delete a branch:
git branch -d <branch name>
Note that you can't delete the branch you're currently on. If you want to delete the current branch, you need to switch to another branch first.
If the branch you want to delete hasn't been merged yet, you can use the -D flag instead of -d to force the deletion.
Rename a branch:
git branch -m <old_branch> <new_branch>
Merge a branch into the current (HEAD) branch. Let's assume you want to merge changes from a branch named feature into the main branch. First, switch to the main branch:
git switch main
Next, merge the feature branch:
git merge feature
If the main branch has not diverged from when feature was created (meaning, no other changes were made to main after branching/main is a direct ancestor of feature), Git performs a fast-forward merge. In this case, Git simply moves the HEAD pointer and main branch pointer to the latest commit of feature.
However, if changes were made on main after feature branched off, Git performs a three-way merge. This involves finding the common ancestor of main and feature and creating a new merge commit that combines changes from each branch. This merge commit has two parents: the latest commits of main and feature. Both HEAD and main branch pointer then move to this new commit.
If main and feature have conflicting changes, you'll need to resolve these manually. Once resolved, create the merge commit by running git commit. If you decide to abort the merge, run git merge --abort to revert your repository to its state before attempting the merge.
3. Mergin and Resolving Conflicts
First, start the merge process by switching to the main branch:
git switch main
git merge feature
If there are any conflicts, Git will let you know in the command output
Open the file with conflicts in your text editor. You'll see the conflicting changes marked in the following way:
<<<<<<< HEAD
[Changes made on the current branch]
=======
[Changes made on the other branch]
>>>>>>> feature
Everything between <<<<<<< HEAD and ======= represents the changes on the current branch (main in our case). And everything between ======= and >>>>>>> feature are the changes on the feature branch.
To resolve the conflict, you need to decide if you want to keep the changes from the main branch, the feature branch, or a combination of both. Edit the file to make it look like the way you want the final code to be. Delete the conflict markers <<<<<<<, =======, and >>>>>>> when you're done editing.
After resolving the conflict in a file, you need to add it to the staging area:
git add <filename>
Repeat the process for all files with conflicts.
Once you've resolved all conflicts and staged the changes, commit the merge:
git commit -m "Resolved merge conflicts for merging feature into main"
Remember, resolving conflicts can sometimes be tricky if the same part of the code has been significantly changed in both branches. It might be necessary to consult with the author of the changes or to understand the context of each change to make an informed decision.
For complex projects with multiple contributors, it's good practice to communicate openly about conflicts and to resolve them collaboratively.
Note that after merging you can delete all branches that are no longer needed. For example, if you've merged the feature branch into main, you can delete the feature branch:
git branch -d feature
Or multiple branches at once:
git branch -d feature1 feature2 feature3
Or all branches but main with one of the following commands:
git branch -d $(git branch | grep -v main)
# or git branch | grep -v main | xargs git branch -d
4. Comparing Files, Commits, and Branches
Note that in the documentation, the stagging area is also called index.
Working Directory vs. Staging Area (only unstaged changes)
git diff
This command highlights what has been changed but is not yet staged for the next commit. Note that you can compare individual files by specifying the file name, i.e., git diff <filename>.
Staged File vs. Last Commit (HEAD) (only staged changes)
git diff --staged
# or git diff --cached
Working Directory (and Staging Area) vs. Last Commit (HEAD) (both staged and unstaged changes)
git diff HEAD
This command will show both staged and unstaged changes combined, as they would look if you committed them right now.
Compare Branches, Commits, or Files in Different Commits
git diff <branch1> <branch2>
git diff <commit1> <commit2>
git diff <commit1>:<file1> <commit2>:<file1>
# or git diff <commit1> <commit2> <file1>
5. Stashing Changes
5.1. Introduction
Stashing is a way to temporarily store both staged and unstaged changes that you don't want to commit immediately. This is particularly useful when you need to quickly switch context and work on a different branch, but you're in the middle of making code changes that aren't ready to be committed.
- Changes Come With You