siebert
siebert

Reputation: 155

How to efficiently rebase and push a local git branch?

We're using a central git repository which I've cloned and I'm working on a local branch.

When I want to make my changes available in the central repository, I have to issue the following commands (starting on mybranch):

#Stash local changes not yet ready for checkin
git stash

#Make sure we have all changes from the central repository
git checkout master
git pull

#Rebase local changes
git checkout mybranch
git rebase

#Push changes
git checkout master
git merge mybranch
git push

#Back to my branch and continue work
git checkout mybranch
git stash apply

I'd like to know if it is possible to use fewer git commands to accomplish the same goal. The several switches between master and mybranch are especially annoying, as our repository is rather huge so they take some time.

Upvotes: 9

Views: 9940

Answers (4)

CB Bailey
CB Bailey

Reputation: 791651

There is no need to touch your local master branch if you don't need to update it and this seems to be causing a lot of your unnecessary branch switching.

This is a more minimal workflow.

git fetch

# ensure that everything is committed
# perhaps git commit -a is required...

git rebase origin/master


# If you don't want to push the very latest commits you might
# want to checkout a parent or ancestor of the current commit
# to test that the proposed commit passes tests, etc.
# e.g. git checkout HEAD~n

# push to the remote master
git push origin HEAD:master

# if you checked out a parent, go back to the original branch
git checkout mybranch

If you're super confident about a parent commit, you can skip the checkout steps and just do the following, but I'd strongly recommend against it. Publishing untested commits is not a 'best practice'.

git push origin HEAD^:master

Upvotes: 13

Pat Notz
Pat Notz

Reputation: 214206

It's not necessary to do the pull on both the master and mybranch branches. Since you're being such a nice citizen and doing fast-forward updates it's pretty straight forward:

# Save local mods not ready for commit
git stash
# Do the pull & rebase local work assuming this is a remote tracking branch
git pull --rebase
git checkout master
git merge mybranch
git push

Of course, you can also push from your mybranch branch

# Save local mods not ready for commit
git stash
# Do the pull & rebase local work assuming this is a remote tracking branch
git pull --rebase
git push origin mybranch:master

Upvotes: 6

Marko
Marko

Reputation: 31375

I usualy do.

git co master
git pull
git rebase master mywrk # fix conflicts if any
git rebase mywrk master
git push

You can define aliases to save typing, if you like that way.

Upvotes: 0

Cody Caughlan
Cody Caughlan

Reputation: 32748

You could combine the pull and rebase into one:

git pull --rebase master

But overall, yes, from my experience it involves all these commands.

To keep your repository clean, its helpful to often run "git gc" which will remove unused objects. This should cut down on branch switching time.

Upvotes: 1

Related Questions