Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pull, rebase, push, in one command (or just a few)

Tags:

When using Git, I often find myself doing the following when working in master:

# work work work...
$ git checkout -b temp
$ git commit -a -m 'more work done'
$ git checkout master
$ git pull origin master
# turns out master was updated since my previous pull
$ git checkout temp
# I don't want a merge commit for a simple bugfix
$ git rebase master
$ git checkout master
$ git merge temp
$ git push origin master
$ git branch -d temp

... and I get tired of doing this. Is there a way to do this dance without all of the checkouts, and preferably without (manually) creating the temporary branch?

like image 721
Fred Foo Avatar asked Sep 26 '11 13:09

Fred Foo


2 Answers

If you don't mind not creating a branch called temp, you could just do the following all on master:

git commit -a -m 'more work done'
git fetch origin
git rebase origin/master

... or equivalently:

git commit -a -m 'more work done'
git pull --rebase origin master

If you do want to keep the temp branch, however, you can still make this a bit shorter by not checking out master just to do the pull - you only need to fetch and then rebase your branch onto origin/master:

# work work work...
$ git checkout -b temp
$ git commit -a -m 'more work done'
$ git fetch origin
# It looks like origin/master was updated, so:
$ git rebase origin/master
# Then when you finally want to merge:
$ git checkout master
$ git merge temp
$ git push origin master
$ git branch -d temp

sehe's answer reminds me that you could replace:

$ git fetch origin
$ git rebase origin/master

... with:

$ git pull --rebase origin master

... which is nearly equivalent. The difference is that when you run git fetch origin, all of your remote-tracking branches for origin will be updated, whereas when you pull a particular branch from origin, none of them are - it's just the temporary ref FETCH_HEAD that is updated. I personally prefer running one extra command (git fetch origin), and seeing all the remote branches that have changed in the output.

like image 73
Mark Longair Avatar answered Oct 01 '22 19:10

Mark Longair


You can at least optimize the rebasing: git pull --rebase

I'm not exactly sure how you like things, but I like my workflow sort of like this:

git checkout -b temp
git commit -a -m 'more work done'
git pull --rebase origin master

That way I keep my focus on the branch at hand.

Then later, I do

git checkout master
git pull origin master
git merge temp

etc.

like image 28
sehe Avatar answered Oct 01 '22 19:10

sehe