How to move changes to another branch in Git – CloudSavvy IT

While Git keeps track of your daily changes, it also has systems like branches that help you organize. However, if you are not careful, you can end up with problems like commits and changes in the wrong branch, which can be difficult to solve without the right commands.

Moving changes (if you have not committed yet)

Git monitors your entire directory, but changes you make to files are not tied to a specific branch before you commit them. You can move branches and take these changes with you.

The simplest option is to simply switch branches, but this will only work if the two branches you are targeting has a matching story:

git checkout feature

You can get around this in a few ways. The first is by making a new branch and then merging the divergent stories:

git checkout -b tempfeature

git checkout feature

git merge tempfeature

You can also use git stash to save changes for later and reuse them on a new branch:

git stash
git switch feature
git stash apply

Relocation obligations (if you have already committed)

If you have already committed, do not despair – You can always soft reset, so commitments are not final until they are pushed to remote control of the source. If you have already done so, you can still fix the problem, but the registration of your error will live on in your Git history, so it’s best to do it locally before your colleagues see it.

To undo commits, you can just soft reset, usually just undo the last commit that was made, but you can also send a reference to the commit ID:

git reset HEAD~1

This will leave you in the “not committed yet” mode, after which you can use the methods above to resolve the issue.

Alternatively, you can use git cherry-pick. This command copies commits from one branch to another and is a great way to select commits and move them to new industries in a clean way.

race git log to find the ID of the commitment you want to recover:

git log

Then check the feature branch, provided your changes have been made, and run cherry-pick:

git switch feature

git cherry-pick

Then there will still be a duplicate commit on the main branch. You can reset this and discard the changes if the function branch is in the correct order, or keep it and let Git fix it when you merge.

Leave a Comment