//只创建分支不切换; $ git branch testing
//创建并切换分支$ git checkout -b iss53
$ git log --oneline --decorate f30ab (HEAD, master, testing) add feature #32 - ability to add new 34ac2 fixed bug #1328 - stack overflow under certain conditions 98ca9 initial commit of my project
//当前 “master” 和 “testing” 分支均指向校验和以f30ab
开头的提交对象。
$ git checkout testing
$ git log --oneline --decorate --graph --all * c2b9e (HEAD, master) made other changes | * 87ab2 (testing) made a change |/ * f30ab add feature #32 - ability to add new formats to the * 34ac2 fixed bug #1328 - stack overflow under certain conditions * 98ca9 initial commit of my project
1:如果当前分支是合并目标分支的直接祖先,就可以使用快速合并,当前分支的指针直接移动到目标分支; 例如:master分支合并hotfix分支 $ git checkout master $ git merge hotfix Updating f42c576..3a0874c Fast-forward index.html | 2 ++ 1 file changed, 2 insertions(+)
2:如果当前分支并不是合并目标分支的直接祖先,Git不得不做一些额外的工作。出现这种情况的时候,Git会使用两个分支的末端所指的快照(C4
和C5
)以及这两个分支的工作祖先(C2
),做一个简单的三方合并。 例如:master分支合并iss53分支
//删除hotfix分支 $ git branch -d hotfix Deleted branch hotfix (3a0874c).
//合并分支时产生冲突,指示index.html文件有冲突 $ git merge iss53 Auto-merging index.html CONFLICT (content): Merge conflict in index.html Automatic merge failed; fix conflicts and then commit the result.
//观察文件状态 $ git status On branch master You have unmerged paths. (fix conflicts and run "git commit") Unmerged paths: (use "git add <file>..." to mark resolution) both modified: index.html no changes added to commit (use "git add" and/or "git commit -a")
//使用工具打开冲突的文件 <<<<<<< HEAD:index.html <div id="footer">contact : email.support@github.com</div> ======= <div id="footer"> please contact us at support@github.com </div> >>>>>>> iss53:index.html
//把文件手工修改为我们想要的内容 <div id="footer"> please contact us at email.support@github.com </div> //重新添加到暂存库 git add index.html //重新添加到版本库 git commit -m ‘解决冲突文件index.html‘
$ git branch iss53 * master testing *代表的是当前的分支
$ git branch -v iss53 93b412c fix javascript issue * master 7a98805 Merge branch ‘iss53‘ testing 782fd34 add scott to the author list in the readmes
//查看哪些分支已经合并到当前分支; 使用git branch -d iss53可以正常删除已合并的分支; $ git branch --merged iss53 * master //查看哪些分支目前还没有合并到当前分支;使用git branch -d testing不能正常删除没有被合并的分支,需用git branch -D testing强制删除 $ git branch --no-merged testing
(remote)/(branch)
形式命名,例如: origin/master
//远程主机的所有更新全部拉取到本地中;只拉取,不会自动合并;相当于origin/master只读分支不断向前走动 $ git fetch <远程主机名> //只拉取特定的分支 $ git fetch <远程主机名> <分支名>
//拉取并自动合并
$ git pull <远程主机名>
//推送serverfix分支 $ git push origin serverfix Counting objects: 24, done. Delta compression using up to 8 threads. Compressing objects: 100% (15/15), done. Writing objects: 100% (24/24), 1.91 KiB | 0 bytes/s, done. Total 24 (delta 2), reused 0 (delta 0) To https://github.com/schacon/simplegit * [new branch] serverfix -> serverfix
git merge origin/serverfix
$ git checkout -b serverfix origin/serverfix Branch serverfix set up to track remote branch serverfix from origin. Switched to a new branch ‘serverfix‘
//当运行fetch的时候,本地分支sf(新建)
会自动从origin/serverfix
拉取。
$ git checkout -b sf origin/serverfix Branch sf set up to track remote branch serverfix from origin. Switched to a new branch ‘sf‘
//设置本地已有的分支(当前分支)跟踪刚刚拉取下来的一个分支$ git branch -u origin/serverfix
$ git branch -vv iss53 7e424c3 [origin/iss53: ahead 2] forgot the brackets //超前两个提交 master 1ae2a45 [origin/master] deploying index fix //与远程分支同步 * serverfix f8674d9 [teamone/server-fix-good: ahead 3, behind 1] this should do it //超前三个提交,落后一个提交 testing 5ea463a trying something new //没有跟踪任何远程分支;
$ git push origin --delete serverfix To https://github.com/schacon/simplegit - [deleted] serverfix
原文:http://www.cnblogs.com/oufeng/p/6697026.html