Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently rebase and push a local git branch?

Tags:

git

merge

rebase

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.

like image 322
siebert Avatar asked Jun 03 '09 15:06

siebert


People also ask

How do I rebase a local branch?

To rebase a branch, checkout the branch and then rebase it on top of another branch. Important: After the rebase, the applied commits will have a different hash. You should not rebase commits you have already pushed to a remote host.

What is the golden rule of rebasing in git?

The Golden Rule of Rebasing reads: “Never rebase while you're on a public branch.” This way, no one else will be pushing other changes, and no commits that aren't in your local repo will exist on the remote branch.


3 Answers

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
like image 52
CB Bailey Avatar answered Oct 23 '22 08:10

CB Bailey


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
like image 6
Pat Notz Avatar answered Oct 23 '22 08:10

Pat Notz


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.

like image 1
Cody Caughlan Avatar answered Oct 23 '22 08:10

Cody Caughlan