- Git Branch:
Up to this point, you've worked in a single Git branch called
master
. Git allows us to create branches to experiment with versions of a project. Imagine you want to create version of a story with a happy ending. You can create a new branch and make the happy ending changes to that branch only. It will have no effect on the master
branch until you're ready to merge the happy ending to the master branch.
In this lesson, we'll be using Git branching to develop multiple versions of a resumé.
You can use the command below to answer the question: “which branch am I on?”
Command: git branch
Check what branch you are currently on. In the output, the * (asterisk) is showing you what branch you’re on.
The diagram above illustrates branching.
- 1. The circles are commits, and together form the Git project's commit history.
- 2. New Branch is a different version of the Git project. It contains commits from Master but also has commits that Master does not have.
- Creating new Branch:
To create a new branch, use:
Command: git branch new_branch
Some points to be noted:-
Here
new_branch
would be the name of the new branch you create, like photos
or blurb
. Be sure to name your branch something that describes the purpose of the branch. Also, branch names can’t contain whitespaces: new-branch
and new_branch
are valid branch names, but new branch
is not.
For example, here we are gonna create a new branch name "fencing".
Let's create the new branch: git branch fencing
Check the branches: git branch
- Git checkout:
The master and fencing branches are identical: they share the same exact commit history. You can switch to the new branch with-
git checkout new_branch
You will be now able to make commits on the fencing branch that have no impact on master. Let's switch to the fencing branch.
git checkout fencing >> for changing from master to fencing branch;
git branch >> confirming that fencing in now the current branch you are working on;
- Commit on a new branch:
After switching to a new branch, now we could add and commit our changes to the new branch.
Exercise steps:
1. Print the Git commit log.
Notice the output:
- The commits you see were all made in the
master
branch.fencing
inherited them. - This means that every commit
master
has,fencing
also has.
Note: if you find that your cursor is stuck in Git log, press
q
to escape.
2. Make changes in any file.
3. Add modified file in the staging area.
4. Commit the changes to the repository (aka repo) with a commit message.
3. Add modified file in the staging area.
4. Commit the changes to the repository (aka repo) with a commit message.
- Git Merge:
What if you wanted include all the changes made to the fencing branch on the master branch? We can easily accomplish this by merging the branch into master with:
Command: git merge branch_name
In a moment, you'll merge branches. Keep in mind:
1. Your goal is to update master with changes you made to fencing.2. fencing is the giver branch, since it provides the changes.3. master is the receiver branch, since it accepts those changes.
Exercise:
1. If you are on the fencing branch, Switch over to the
2. merge changes from fencing branch to master branch-
git merge fencing
Output:
master
branch.2. merge changes from fencing branch to master branch-
git merge fencing
Output:
Notice the output: The merge is a "fast forward" because Git recognizes that fencing contains the most recent commit. Git fast forwards master to be up to date with fencing.
- Merge conflict:
The merge was successful because master had not changed since we made a commit on fencing. Git knew to simply update master with changes on fencing.
What would happen if you made a commit on master before you merged the two branches? Furthermore, what if the commit you made on master altered the same exact text you worked on in fencing? When you switch back to master and ask Git to merge the two branches, Git doesn't know which changes you want to keep. This is called a merge conflict.
Exercise:
1. Check if you are on the master branch.
2. Make some changes on the test file.
3. Add the edited file to the staging area.
4. Commit the changes to the repo with a commit message.
5. Imagine a few weeks have passed, and you'd like to develop your fencing branch some more.Switch back to the fencing branch.
6. From fencing branch, make changes on the same file you made changes on step 02.
7. Add the file to the staging area
8. Commit to the repo.
1. Check if you are on the master branch.
2. Make some changes on the test file.
3. Add the edited file to the staging area.
4. Commit the changes to the repo with a commit message.
5. Imagine a few weeks have passed, and you'd like to develop your fencing branch some more.Switch back to the fencing branch.
6. From fencing branch, make changes on the same file you made changes on step 02.
7. Add the file to the staging area
8. Commit to the repo.
Let's say you decide you'd like to merge the changes from fencing into master.Here's where the trouble begins!You've made commits on separate branches that alter the same line in conflicting ways. Now, when you try to merge fencing into master, Git will not know which version of the file to keep.
Exercise:
1. Switch to the master branch.
2. Then merge fencing branch to the master branch.
3. In the output, notice the lines:CONFLICT (content): Merge conflict in testFile.Automatic merge failed; fix conflicts and then commit the result.
1. Switch to the master branch.
2. Then merge fencing branch to the master branch.
3. In the output, notice the lines:CONFLICT (content): Merge conflict in testFile.Automatic merge failed; fix conflicts and then commit the result.
4. We must fix the merge conflict.
look at the test file above. Git uses markings to indicate the HEAD (master) version of the file and the fencing version of the file, like this:
Git asks us which version of the file to keep: the version on master or the version on fencing. You decide you want the fencing version.
From the test file-Delete the content of the line as it appears in the master branchDelete all of Git's special markings including the words HEAD and fencing. If any of Git's markings remain, for example, >>>>>>> and =======, the conflict remains.
5. Add the changes to the staging area.
6. Commit the changes to the repo.
6. Commit the changes to the repo.
- Delete Branch:
In Git, branches are usually a means to an end. You create them to work on a new project feature, but the end goal is to merge that feature into the master branch. After the branch has been integrated into master, it has served its purpose and can be deleted.
The commandgit branch -d branch_namewill delete the specified branch from your Git project.
Now that master contains all the file changes that were in fencing, let's delete fencing.
Exercise:Delete the fencing branch and see the output message. Now, verify that you have indeed deleted fencing by listing all your project's branches on the terminal.Notice in the output that only one branch, master, is shown.
Let's have a recap:
Git branching allows users to experiment with different versions of a project by checking out separate branches to work on.
The following commands are useful in the Git branch workflow.
1. git branch: Lists all a Git project's branches.
2. git branch branch_name: Creates a new branch.
3. git checkout branch_name: Used to switch from one branch to another.
4. git merge branch_name: Used to join file changes from one branch to another.
5. git branch -d branch_name: Deletes the branch specified.
Comments
Post a Comment