Friday, April 8, 2016

Learn Git_Lesson_Git Branching

1.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.
You can use the command below to answer the question: “which branch am I on?”
$ git branch
1.1.Check what branch you are currently on.
In the output, the * (asterisk) is showing you what branch you’re on.
$ git branch                                                                                     
master
2.branching overview


The diagram below illustrates branching.

  • The circles are commits, and together form the Git project's commit history.
  • New Branch is a different version of the Git project. It contains commits from Master but also has commits that Master does not have.
3.git branch 2
 Right now, the Git project has only one branch: master.
To create a new branch, use:
$ git branch new_branch
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.
3.1.Create a new branch called fencing.Next, view your branches as you did in the previous exercise. Notice in the output there now appear two branches: master and fencing
$ git branch fencing                                                                             
$ git branch                                                                                     
  fencing                                                                                        
* master 4.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 branch_name
You will be now able to make commits on the fencing branch that have no impact on master.
4.1.Switch to the fencing branch from the master branch.
$ git checkout fencing                                                                           
Switched to branch 'fencing' 
4.2.Use git branch to verify that you have switched branches.
In the output, notice the * is now over the fencing branch.
$ git branch                                                                                     
* fencing                                                                                        
  master    
5.commit on a new branch
The diagram below shows what will happen to the Git project. 


5.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.
$ git log                                     
commit 79a1cc5e23d46fd2f4459440cc18624d874cf36
Author: danasselin <johndoe@example.com>      
Date:   Wed Oct 28 17:10:17 2015 -0400                                                     
    Add heading for volunteer experience                                                   
commit df765b18c9b767089ef649f7ece33b58c27b897
Author: danasselin <johndoe@example.com>      
Date:   Wed Oct 28 17:09:50 2015 -0400                                                     
    Add experience scheming against hook                                                   
commit 1baf497026042a0938bc353c2d5406e31860f43
Author: danasselin <johndoe@example.com> 
5.2.In resume.txt,
Delete this line:

-Scheme against Captain Hook
and type this line in its place:

-Engage in swordfights with pirates
5.3.Add resume.txt into the staging area. $ git add resume.txt                           5.4commit on a new branch
Commit the changes to the repository with a commit message. 
$ git commit -m "Commit to Fencing fetch"     
[fencing 2c38d27] Commit to Fencing fetch     
 1 file changed, 1 insertion(+), 1 deletion(-)
6.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:
git merge branch_name
In a moment, you'll merge branches. Keep in mind:
  • Your goal is to update master with changes you made to fencing.
  • fencing is the giver branch, since it provides the changes.
  • master is the receiver branch, since it accepts those changes. 
6.1.You are currently on the fencing branch. Switch over to the master branch.
$ git checkout master                                                                            
Switched to branch 'master'
6.2.From the terminal, merge the fencing branch into the master branch.
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.

$ git merge fencing                                                                              
Updating 79a1cc5..2c38d27                                                                        
Fast-forward                                                                                     
 resume.txt | 2 +-                                                                               
 1 file changed, 1 insertion(+), 1 deletion(-)                                                  
7.merge conflict I
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.

7.1.You are on the master branch. In the code editor, where you have written:

-Engage in swordfights with pirates
Add the word "professional", so the text reads:

-Engage in swordfights with professional pirate
7.2Add resume.txt to the staging area. 
$ git add resume.txt  
7.3.Commit the changes to the repository with a commit message.
$ git commit -m "change to professional"       
[master 8f4ab6c] change to professional        
 1 file changed, 1 insertion(+), 1 deletion(-)
7.4.Imagine a few weeks have passed, and you'd like to develop your fencing resumé some more.
Switch back to the fencing branch.
$ git checkout fencing                         
Switched to branch 'fencing'
7.5.From fencing, change the line so it reads:

-Engage in swordfights with professional pirates such as Smee.

7.6.Once again, add resume.txt to the staging area. 
$ git add resume.txt 
7.7.Commit the changes to the repository with a commit message.
$ git commit -m "Smee"                        
[fencing 700e536] Smee                        
 1 file changed, 1 insertion(+), 1 deletion(-)
8.merge conflict II
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.
8.1.Switch to the master branch. 
$ git checkout master                          
Switched to branch 'master' 
8.2.merge fencing into master.In the output, notice the lines:
$ git merge fencing                            
Auto-merging resume.txt                        
CONFLICT (content): Merge conflict in resume.txt                                              
Automatic merge failed; fix conflicts and then 
commit the result. 
8.3.We must fix the merge conflict.
In the code editor, look at resume.txt. Git uses markings to indicate the HEAD (master) version of the file and the fencing version of the file, like this:

<<<<<<< HEAD master version of line ======= fencing version of line >>>>>>> fencing
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 code editor:
Delete the content of the line as it appears in the master branch
Delete 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.
Try reloading the page if Git's markings don't show up.
 -Engage in swordfights with professional pirates such as Smee.
8.4.Add resume.txt to the staging area. 
$ git add resume.txt 
8.5.Now, make a commit. For your commit message, type "Resolve merge conflict" to indicate the purpose of the commit. 
$ git commit -m "Resolve mrege confict"        
[master 2c0a4f9] Resolve mrege confict  9.delete granch
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 command
git branch -d branch_name
will delete the specified branch from your Git project.
Now that master contains all the file changes that were in fencing, let's delete fencing.

9.1.Delete the fencing branch.
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.
$ git branch -d fencing                                                                          
Deleted branch fencing (was 700e536).                                                            
$ git branch                                                                                     
* master 
10.generalizations
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.
  • git branch: Lists all a Git project's branches.
  • git branch branch_name: Creates a new branch.
  • git checkout branch_name: Used to switch from one branch to another.
  • git merge branch_name: Used to join file changes from one branch to another.
  • git branch -d branch_name: Deletes the branch specified.

2 comments :

  1. It is really crazy just how predominant this trend is, until you know to look for it. As I proceed with this series of posts, I will try to put together some montages of what I am talking about. wood fence panels for sale

    ReplyDelete