mainsocial
mainsocial

Reputation: 7823

moving changed files to another branch for check-in

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?

Upvotes: 656

Views: 327308

Answers (6)

Hashim Aziz
Hashim Aziz

Reputation: 6052

A more modern answer

Since checkout is probably Git's least intuitive porcelain command and responsible for way too much functionality, use the easier-to-remember, more intuitive switch instead (introduced in 2019 as part of Git 2.23):

git switch <NEW BRANCH> # Add -c to create the branch if it doesn't exist
git add <FILES TO COMMIT>
git commit -m "Commit changed files to new branch"
git switch <ORIGINAL BRANCH>

If your branches are not in sync with each other the first git switch command will give you an error like this:

error: Your local changes to the following files would be overwritten by checkout:
        file1.html
        file2.js
        file3.php
Please commit your changes or stash them before you switch branches.
Aborting

At this point you will have to use the alternative git stash method:

git stash save
git switch <NEW BRANCH> # Add -c to create the branch if it doesn't exist
git add <FILES TO COMMIT>
git commit -m "Commit changed files to new branch"
git switch <ORIGINAL BRANCH>
git stash pop

Don't forget to replace NEW BRANCH with the branch you want to switch to and ORIGINAL_BRANCH with the branch you were originally in (usually master or main). To see a list of all your repository's branches, run git branch -l.

Upvotes: 1

Bill Door
Bill Door

Reputation: 18926

Updated Answer

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


Orginal Answer

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

Upvotes: 1184

jaksco
jaksco

Reputation: 531

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

Upvotes: 0

JSON C11
JSON C11

Reputation: 11802

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.

Upvotes: 11

watashiSHUN
watashiSHUN

Reputation: 10484

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

Upvotes: 24

Amber
Amber

Reputation: 526573

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.

Upvotes: 329

Related Questions