简体   繁体   中英

Transfer changes from one committed branch to another branch

I made changes to the feature / project branch and committed it, and then I realized that I did something wrong in the branch, I had to do it in the feature / nfts-list-and-detail branch I used the following method, but this is if we do not have a committed

$ git stash

$ git checkout feature / nfts-list-and-detail

$ git stash pop

How can I transfer my changes to the feature / nfts-list-and-detail ?

"Git is all about commits" as explained by fellow user torek .

So what does this mean for you? A commit is not on or in a branch. A branch points to a commit and if you follow the parent commits, you get the history of this branch. But branches no more than dynamic labels. These they can be created, renamed, deleted.

As long as you have not pushed anything to a remote repository, there is nothing to worry about: cherry-pick the commit to the correct branch and then roll back your other branch. Cherry-picking creates a new commit with the same (or similar) changes than your original one.

Here are two ways to do it (note that branch names cannot contain blanks):

git checkout feature/nfts-list-and-detail # switch to correct branch
git cherry-pick feature/project # create copy of latest commit of wrong branch
git branch -f feature/project feature/project # undo latest commit on wrong branch
git checkout feature/project # switch to wrong branch
git reset --mixed HEAD^ # undo latest commit, keep changes in working tree
git stash
git checkout feature/nfts-list-and-detail
git stash pop
git add files you want to commit
git commit

There are other ways with varying degrees of complexity and possibilities to shoot yourself in the foot.

If you have already pushed to the wrong branch, things become a bit more tricky. But if you don't care for the history showing that the commit was there, a simple git revert will do the job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM