Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

version control - how do you push only some of your local git commits?

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...