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.

  • interactive git rebase failed with “git-rebase-todo: No such file or directory”
  • How to reset a Git repository and check out sources from SVN afresh?
  • How to squash two commits which are actually before a merge commit?
  • Git: push for teamwork, then rebase for integration
  • Git rebase failing
  • git, merge overwrites my changes
  • Git push rejected “non-fast-forward”
  • where is git-svn's git-svn TAG?
  • 4 Solutions collect form web for “How to efficiently rebase and push a local git branch?”

    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
    

    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
    

    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.

    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.

    Git Baby is a git and github fan, let's start git clone.