Section 14.3 Advanced Git
As you continue to use Git, whether it be individually or as part of a team, you will encounter more advanced concepts and workflows. This section will introduce some of these concepts.
Subsection 14.3.1 Conflicts
Conflicts occur when two or more people make changes to the same file. Git will automatically merge changes when possible, but when it cannot, it will mark the file as having a conflict. When this happens, you will need to manually resolve the conflict.
When a conflict occurs, Git will mark the file with conflict markers. These markers look like this:
<<<<<<<< HEAD This is the original text. >>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086 This is the modified text.
The text between the
<<<<<<<< HEAD
and =======
markers is the original text. The text between the =======
and >>>>>>>>77976da35a11db4580b80ae27e8d65caf5208086
markers is the modified text. You will need to manually edit the file to resolve the conflict. Once you have resolved the conflict, you will need to add the file to the staging area and commit it.Subsection 14.3.2 Branching
Branching is a powerful feature of Git. Branches allow you to work on multiple features at the same time. Branches also allow you to work on features without affecting the main codebase.
When you create a branch, you are creating a copy of the current state of the repository. You can then make changes to the branch without affecting the main codebase. When you are ready to merge your changes into the main codebase, you can merge the branch into the main codebase.
To create a branch, use the
git branch
command. For example, to create a branch named feature
, you would use the following command:git branch feature
To switch to a branch, use the
git checkout
command. For example, to switch to the feature
branch, you would use the following command:git checkout feature
To create a branch and switch to it at the same time, use the
git checkout
command with the -b
option. For example, to create a branch named feature
and switch to it, you would use the following command:git checkout -b feature
Subsection 14.3.3 Merging Branches
Merging is the process of combining two or more branches into one. When you merge branches, Git will automatically merge changes when possible. When it cannot, it will mark the file as having a conflict. When this happens, you will need to manually resolve the conflict.
To merge a branch into the current branch, use the
git merge
command. For example, to merge the feature
branch into the current branch, you would use the following command:git merge feature
When you merge a branch into the current branch, Git will automatically merge changes when possible. When it cannot, it will mark the file as having a conflict. When this happens, you will need to manually resolve the conflict.
Subsection 14.3.4 Rebasing
Rebasing is the process of moving a branch to a new base commit. When you rebase a branch, Git will automatically merge changes when possible. When it cannot, it will mark the file as having a conflict. When this happens, you will need to manually resolve the conflict.
To rebase a branch, use the
git rebase
command. For example, to rebase the feature
branch onto the master
branch, you would use the following command:git rebase master
When you rebase a branch, Git will automatically merge changes when possible. When it cannot, it will mark the file as having a conflict. When this happens, you will need to manually resolve the conflict.
Subsection 14.3.5 Stashing
Stashing is the process of saving your changes without committing them. When you stash your changes, Git will save your changes in a temporary location. You can then restore your changes at a later time.
To stash your changes, use the
git stash
command. To restore your changes, use the git stash pop
command. For example, to restore your changes, you would use the following command:git stash pop
You have attempted of activities on this page.