views:

1191

answers:

3

Suppose I have 5 local commits. I want to push only 2 of them to a centralized repo (using an SVN-style workflow). How do I do this?

This did not work:

git checkout HEAD~3  #set head to three commits ago
git push #attempt push from that head

That ends up pushing all 5 local commits.

I suppose I could do git reset to actually undo my commits, followed by git stash and then git push -- but I've already got commit messages written and files organized and I don't want to redo them.

My feeling is that some flag passed to push or reset would work.

If it helps, here's my git config

[ramanujan:~/myrepo/.git]$cat config 
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = ssh://server/git/myrepo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
+2  A: 

What I do is work on a local branch called "work". This branch contains all the temporary commits (like workarounds or private build options or whatever) that I don't intend to push to the upstream repository. I work away on that branch, then when I want to commit I switch to the master branch, cherry-pick the appropriate commits that I do want to commit, then push master.

After pulling changes from the upstream into my master branch, I git checkout work and git rebase master. That rewrites all my local changes to be at the end of the history.

I'm actually using git svn with this workflow, so my "push" operation involves git svn dcommit. I also use tig which is a nice text mode gui repository viewer, to cherry-pick the appropriate commits to master.

Greg Hewgill
with git svn dcommit, you can specify a commit to dcommit up to, so the desired effect is quite trivial with git-svn.
Ryan Graham
+15  A: 

Assuming your commits are on the master branch and you want to push them to the remote master branch:

$ git push origin master~3:master

If you were using git-svn:

$ git svn dcommit master~3

In the case of git-svn, you could also use HEAD~3, since it is expecting a commit. In the case of straight git, you need to use the branch name because HEAD isn't evaluated properly in the refspec.

You could also take a longer approach of:

$ git checkout -b tocommit HEAD~3
$ git push origin tocommit:master

If you are making a habit of this type of work flow, you should consider doing your work in a separate branch. Then you could do something like:

$ git checkout master
$ git merge working~3
$ git push origin master:master

Note that the "origin master:master" part is probably optional for your setup.

Ryan Graham
+1  A: 

By default, git-push pushes all branches. When you do this:

 git checkout HEAD~3  #set head to three commits ago
 git push #attempt push from that head

You move to a detached HEAD (you're not on any branch) and then you push all the branches, including the local master (which is still where it was) to the remote master.

The manual solution is:

 git push origin HEAD:master

If you find the default behaviour of pushing all branches confusing (and dangerous!), add this to your ~/.gitconfig:

 [remote.origin]
    push = HEAD

Then only the branch you're on is pushed. In your example (a detached head), you would have got this error message, rather than accidentally pushing the wrong commits:

 error: unable to push to unqualified destination: HEAD
Thomas Leonard