Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2445064
  • 博文数量: 328
  • 博客积分: 4302
  • 博客等级: 上校
  • 技术积分: 5486
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-01 11:14
个人简介

悲剧,绝对的悲剧,悲剧中的悲剧。

文章分类

全部博文(328)

文章存档

2017年(6)

2016年(18)

2015年(28)

2014年(73)

2013年(62)

2012年(58)

2011年(55)

2010年(28)

分类: 项目管理

2016-09-21 17:11:01

I’m sure you’ve all come across merge commits when using Git, those pesky commits with a message reading something like Merge branch 'master' of github.com:kernow/Project-X. We’ve all been guilty of creating git merge commits, but don’t worry there’s a way to stop being a “Git Tit” and make everyone in your team happy which will no doubt lead to them buying you cake! But first, how do these commits get into the repository in the first place? You start out being a good gitizen, git checkout master, git pull, feverishly code away, commit, commit, commit. Woo I’m done, everyone will love my wicked new code! git push rejection!! what! Other people have been working too, jerks. git pull, git push, and there we have it, a merge commit. So how do we stop this madness?


Rebase to the rescue

When running git pull we need to rebase, and so to the first way to avoid merge commits…

1.
1   git pull --rebase What’s happening here? Git will rewind (undo) all of your local commits, pull down the remote commits then replay your local commits on top of the newly pulled remote commits. If any conflicts arise that git can’t handle you’ll be given the opportunity to manually merge the commits then simply run git rebase --continue to carry on replaying your local commits.

2.  
2  Tell git to always rebase when pulling, to do this on a project level add this to your .git/configfile:

1

2

[branch “master”]

  rebase = true

3.    Or do it all on the command line with git config branch.master.rebase true

4.  
3  Add a global config option to always rebase when pulling

1

2

[branch]

  autosetuprebase = always

5.    Or again do it all on the command line with git config --global branch.autosetuprebase always

6.  
4  And the final way, which is what I personally use, in ~/.gitconfig

1

2

[alias]

  pl = pull —rebase

7.  
  I have a bunch of aliases setup so I can type less and save myself those valuable microseconds. This will allow you to type git pl (or in my case g pl as I have git aliased to g) and it will automatically rebase. If I want to do a pull and not rebase for a specific reason I can use the command git pull which will do an pull without rebaseing.


Of course you could use the 3rd solution and run the command git pull --no-rebase but that involves more typing, and I’m a lazy typer!


Repost from http://kernowsoul.com/blog/2012/06/20/4-ways-to-avoid-merge-commits-in-git/
阅读(1101) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~