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
339 views
in Technique[技术] by (71.8m points)

git - How do I move unpushed committed code to another branch?

So I have the following situation:

I committed some work locally, without pushing to the remote repository. I want to move this local code to another branch, because if I pull, there will be modifications that will ruin all the work I put locally.

This is the output of git status on the old branch:

On branch <branch_name>
Your branch is ahead of 'origin/<branch_name>' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean

And this is the output of git status on the newly created branch:

On branch <branch_name>
nothing to commit, working directory clean
question from:https://stackoverflow.com/questions/45216144/how-do-i-move-unpushed-committed-code-to-another-branch

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

1 Reply

0 votes
by (71.8m points)

If it's just one commit, you can simply do

git reset HEAD~1
git stash
git checkout anotherbranch
git stash pop

And if you want to put it in a fresh new branch, another way is

git branch newbranch
git reset --hard HEAD~1

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

...