Git Tutorial 5 - Branch and Merge

Branch is a very important concept in Git and doing branch is one of the greatest features. It plays like the concept of context when you‘re working on it. You can have multiple branches in your repo, and in a time you‘re working in only one of them. It‘s easy to forth or switch back between your branches. When you switch into a branch, Git will replace your working directory with the snapshot of the last commit on that branch for you.

As we‘ve seen in Git Tutorial 1, Git created a branch called ‘master‘ for me by default when I used command git init to create a repo. Since your teammates or others who‘re working with you or interested at your project will always create branch from master. You‘d better make master stable and clean. So please don‘t try to do lots of commits directly to branch master to mess it. Instead, to merge it.

Let‘s create my second branch(you know master is the first).

git branch devgit branchgit branch -v

The first line command creates a new branch called "dev". Since I am currently working in branch master (this is the only one branch I have before I create branch dev.) the new branch dev was created at the last commit of branch master.

The second line command will list out the local branches you have now.

The third line command is similar to the second one but with one more option "-v" which will show you not only the local branches you have but also the last commits on each of them.

See the screen as the results shown in my terminal

figure 5.1

It displays there are 2 branches in my local repo. The branch in green preceded by a star(*) is the current branch I am working in. Since I just create new branch dev from master, they have the same last commit ID (2de5470 in this example, and the command git log proves that).

Let‘s switch to new branch and do some changes. That‘s typical thing a developer always does after creating a new branch.

Before switch to new branch I‘m working in branch master (easy to know that by seeing the path of working directory which is followed by branch name in the screen.).

Run the command git checkout <branchname>

git checkout dev

figure 5.2

Look at the prompt message in the screen above. The path of working directory is followed by string "(dev)" which indicates you‘re currently working in branch dev rather than master.

Don‘t forget that I said the new branch dev was forked on master, which means they keep the same content of each file completely. Now I‘m going to modify the content of a file in branch dev, and see if there‘s any change to the file in another branch.

Please mind your attention on the line No.5 in openofficetemplates_net.html which I‘m going to modify with a letter.

Before I modify openofficetemplates_net.html... see the picture below

figure 5.3

After I modified openofficetemplates_net.html... see the picture below.

figure 5.4

The change I made in openofficetemplates_net.html is on the yellow. The modified time of it has also been updated to a later timestamp.

Next I‘m going to commit the change to branch dev in local repo.

git add --all
git commit -m "Change element title"

figure 5.5

The screen shows me the commit ID for "Change element title" is a676977. That‘s a new commit to dev. Comparing this with the one in figure 5.1, it‘s easy to realize that the last commit ID of dev at this moment has updated to a676977. The return value of the following command "git branch -v" in the screen has proved that.

Take a look at openofficetemplates_net.html, the change‘s still here. Branch dev is clean now as nothing in staging area.

What do you think I should get if I switch currently working branch from dev to master? Just do it.

git checkout masterls -l
head -5 openofficetemplates_net.html | cat -n

figure 5.6

Please mind your attention at the place on the green. The change to openofficetemplates_net.html has been rolled back and the modified time of it has been updated to a later timestamp, which means Git automatically modified openofficetemplates_net.html back to the snapshot of its last commit when I switched to branch ‘master‘. That‘s exactly what happen to the switch between branches.

Thinking further, we can conclude that each time you switch to branch X Git will automatically update files that‘re different from the snapshot of X‘s last commit back to the snapshot of X‘s last commit. That‘s why we should care about the last commit ID of each branch, that‘s also why Git provides us the command ‘git branch -v‘ to list out all of branches you have and their corresponding last commit ID.

Evently I think branch dev is stable and clean, I want to apply the change to branch master. Don‘t try to add and commit to master directly because that will mess it. We should merge it if we want to apply new changes to master. Before running merge command you must make sure you are working in master otherwise you have to switch to it.

git checkout master
git merge dev

See what we got in my terminal

figure 5.7

The change in dev has been merged to master and both of branches now have the same last commit ID (a676977 here).

Finally if you don‘t need branch dev anymore since it‘s been merged to master, you can use command to delete it.

git branch -D dev

figure 5.8

Please make sure you‘re not currently on the branch dev that you‘re going to delete, otherwise you‘ll got error as picture shown above.

时间: 2024-08-01 03:24:13

Git Tutorial 5 - Branch and Merge的相关文章

eclipse git 报错:the current branch is not configured for pull No value for key branch.xxx.merge found

eclipse git 报错:the current branch is not configured for pull No value for key branch.xxx.merge found in configuration 如图: 这是因为 在用gui创建分支的时候 config文件没有创建branch 在config文件加上 [branch "xxx"] remote = origin merge = refs/heads/xxx 即可 eclipse git 报错:th

The current branch is not configured for pull No value for key branch.master.merge found in configur

github虽说挺好用,但对于新手来说,还是个噩梦,比如说对我- - ,搞得乱七八糟,还把之前用来爬微博数据的爬虫给搞没了 这个问题是在Myeclispe 中,右键项目 team 选择pull时报错的,国内很多人都有各自乱七八糟的解答,没几个能用的 在Stackoverflow找到了答案,原题地址 http://stackoverflow.com/questions/8820668/the-current-branch-is-not-configured-for-pull-no-value-fo

git报错:Auto Merge Failed; Fix Conflicts and Then Commit

本文来源:http://blog.csdn.net/trochiluses/article/details/101007191.出错场景: 协同开发时,我们从远程服务器上pull下代码的时候,出现以下提示信息: Auto Merge Failed; Fix Conflicts and Then Commit the Result. 2.原因分析: 利用git status,输出如下: [email protected]:/etc# git status# On branch master# Yo

Git Tutorial 3 - Scenario Extension - revert

Git tutorial previously, I have committed the 3 files to local repository. Then suddently I was aware of the file list.png was committed by mistake. It would've been left behind and been clean from my working directory after I get other files in loca

git:解决The current branch is not configured for pull No value for key branch.master.merge found in config

网上多半都是命令行下的解决方案,我用的是EGit,所以要在eclipse里(我的版本是kepler)把下面这句话添加到配置文件中. Window->Preference->Team->Git->Configuration->Repository Settings->选择你的repository,然后点open [branch "master"]  remote = origin  merge = refs/heads/master 第二种 1.在本地

git 出现 The current branch is not configured for pull No value for key branch.master.merge found in configuration

以下是我在网上找到的不错的文章,我参考后已解决我的问题: http://my.oschina.net/robinsonlu/blog/144085 http://www.cnblogs.com/zhanglanyun/archive/2013/06/05/3119335.html http://blog.csdn.net/androidzhaoxiaogang/article/details/16859099

解决The current branch is not configured for pull No value for key branch.master.merge found in config

网上多半都是命令行下的解决方案,我用的是EGit,所以要在eclipse里(我的版本是kepler)把下面这句话添加到配置文件中. Window->Preference->Team->Git->Configuration->Repository Settings->选择你的repository,然后点open [branch "master"]  remote = origin  merge = refs/heads/master 第二种 1.在本地

Git 使用心得: fetch, merge, remote

最近做project,用到了Git,因此写下来以便总结. git clone, add 和commit 什么的就不说了... 主要说说远程端仓库的事. 首先,先得到远程库上master分支的东西: git fetch origin master origin是远程仓库默认名,如果在 remote add 的时候自己重新取了远程仓库名,那就用自己取的名字:master 则是远程分支名. 这时候只是相当于从远程库拷了代码到本地,还没有和本地的merge,这就是为什么不直接pull,因为先fetch再

git clone远程branch和tag

接着上一个笔记讲,我们想从remote repository上获取某个branch的某个tag.这句可以理解为,以angular,我们想获得angular的angular1的v.0.1.1的版本. 1.查看远程分支git branch -r //显示内容为origin/Androidorigin/mesa-esorigin/mesa-es-dri 如git checkout origin/Android 是不会clone远程的内容(也是错误做法) ------------------------