moving changed files to another branch for check-in

Git

Git Problem Overview


This often happens to me: I write some code, go to check in my changes, and then realize I'm not in the proper branch to check in those changes. However I can't switch to another branch without my changes reverting. Is there a way to move changes to another branch to be checked in there?

Git Solutions


Solution 1 - Git

git stash is your friend.

If you have not made the commit yet, just run git stash. This will save away all of your changes.

Switch to the branch you want the changes on and run git stash pop.

There are lots of uses for git stash. This is certainly one of the more useful reasons.

An example:

# work on some code
git stash
git checkout correct-branch
git stash pop

Update: No need to use stash command. uncommitted changes do not belong to any branch so just use git checkout -b <new-branch>

Solution 2 - Git

If you haven't already committed your changes, just use git checkout to move to the new branch and then commit them normally - changes to files are not tied to a particular branch until you commit them.

If you have already committed your changes:

  1. Type git log and remember the SHA of the commit you want to move.
  2. Check out the branch you want to move the commit to.
  3. Type git cherry-pick SHA substituting the SHA from above.
  4. Switch back to your original branch.
  5. Use git reset HEAD~1 to reset back before your wrong-branch commit.

cherry-pick takes a given commit and applies it to the currently checked-out head, thus allowing you to copy the commit over to a new branch.

Solution 3 - Git

Sadly this happens to me quite regularly as well and I use git stash if I realized my mistake before git commit and use git cherry-pick otherwise, both commands are explained pretty well in other answers

I want to add a clarification for git checkout targetBranch: this command will only preserve your working directory and staged snapshot if targetBranch has the same history as your current branch

> If you haven't already committed your changes, just use git checkout > to move to the new branch and then commit them normally

@Amber's statement is not false, when you move to a newBranch,git checkout -b newBranch, a new pointer is created and it is pointing to the exact same commit as your current branch.
In fact, if you happened to have an another branch that shares history with your current branch (both point at the same commit) you can "move your changes" by git checkout targetBranch

However, usually different branches means different history, and Git will not allow you to switch between these branches with a dirty working directory or staging area. in which case you can either do git checkout -f targetBranch (clean and throwaway changes) or git stage + git checkout targetBranch (clean and save changes), simply running git checkout targetBranch will give an error:

> error: Your local changes to the following files would be overwritten > by checkout: > ... Please commit your changes or stash them before you switch branches. Aborting

Solution 4 - Git

A soft git reset will put committed changes back into your index. Next, checkout the branch you had intended to commit on. Then git commit with a new commit message.

  1. git reset --soft <commit>

  2. git checkout <branch>

  3. git commit -m "Commit message goes here"

From git docs:

> git reset [<mode>] [<commit>] This form resets the current branch head > to and possibly updates the index (resetting it to the tree > of ) and the working tree depending on . If is > omitted, defaults to --mixed. The must be one of the following: > > --soft Does not touch the index file or the working tree at all (but resets the head to , just like all modes do). This leaves all > your changed files "Changes to be committed", as git status would put > it.

Solution 5 - Git

If you created new files you can do this:

git checkout main
git checkout -b branch-b
git checkout branch-a :rel/path/to/yourchangedfiles
git commit -m "w"
git checkout branch-a

git checkout main :rel/path/to/yourchangedfiles

# if this happens:
error: pathspec ':rel/path/to/yourchangedfiles' did not match any file(s) known to git

# then just rm the folder
trash-put rel/path/to/yourchangedfiles

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmainsocialView Question on Stackoverflow
Solution 1 - GitBill DoorView Answer on Stackoverflow
Solution 2 - GitAmberView Answer on Stackoverflow
Solution 3 - GitwatashiSHUNView Answer on Stackoverflow
Solution 4 - GitJSON C11View Answer on Stackoverflow
Solution 5 - GitjakscoView Answer on Stackoverflow