tags:

views:

1036

answers:

4

Hi,

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. Especially the several switches between master and mybranch are very annoying, as our repository is rather huge so they take some time.

Ciao,
Steffen

+1  A: 

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.

Cody Caughlan
A: 

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.

Dev er dev
+2  A: 

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
Pat Notz
What do you mean by "assuming this is a remote tracking branch"? mybranch is a local branch, only existing in my own repository. Will your solution work under these circumstances, too?
siebert
A "remote tracking branch" is really just a local branch except that git remembers a default remote repository and branch to use when you do a "pull". Otherwise, the 'git push' and 'git pull' commands would have to give a full repository location.Here's an example of creating a remote tracking branch$ git branch feature_x origin/masteror $ git checkout -b feature_x origin/masterIf you leave off the remote spec ('origin/master' in this case) then feature_x is not a remote tracking branch.
Pat Notz
+2  A: 

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
Charles Bailey