ChatGPT解决这个技术问题 Extra ChatGPT

How do I merge my local uncommitted changes into another Git branch?

How can I do the following in Git?

My current branch is branch1 and I have made some local changes. However, I now realize that I actually meant to be applying these changes to branch2. Is there a way to apply/merge these changes so that they become local changes on branch2 without committing them on branch1?

There is a great Git Tutorial right here on SO. Its kind of a central for all git questions on stack overflow.
This link exists in the "linked" set of questions on the right thanks to StackOverflow magic, but I think it deserves a comment link: see also Move existing, uncommitted work to a new branch in Git.

V
VonC

Since your files are not yet committed in branch1:

git stash
git checkout branch2
git stash pop

or

git stash
git checkout branch2
git stash list       # to check the various stash made in different branch
git stash apply x    # to select the right one

Above is the longer more explicit version of rbento's answer:

git stash
git stash branch branch2

It uses:

git stash branch [] Creates and checks out a new branch named starting from the commit at which the was originally created, applies the changes recorded in to the new working tree and index. If that succeeds, and is a reference of the form stash@{}, it then drops the . This is useful if the branch on which you ran git stash push has changed enough that git stash apply fails due to conflicts. Since the stash entry is applied on top of the commit that was HEAD at the time git stash was run, it restores the originally stashed state with no conflicts.

As commented by benjohn (see git stash man page):

To also stash currently untracked (newly added) files, add the argument -u, so:

git stash -u

You are welcome. More examples of stash usage at unethicalblogger.com/posts/2008/11/… .
If you are looking for a solution to the same issue but with TFS, the equivalent solution is to shelve your changes then use TFS Power Tools to unshelve to the correct branch using the /migrate switch.
This worked for me. However, I also had to create a local branch for the 'stash pop' to work. Checkout out stackoverflow.com/questions/1783405/git-checkout-remote-branch if something similar is happening to you.
To also stash currently untracked (newly added) files, add the argument -u, so: git stash -u.
@Benjohn Good point. I have included your comment in the answer for more visibility.
C
CB Bailey

Stashing, temporary commits and rebasing may all be overkill. If you haven't added the changed files to the index, yet, then you may be able to just checkout the other branch.

git checkout branch2

This will work so long as no files that you are editing are different between branch1 and branch2. It will leave you on branch2 with you working changes preserved. If they are different then you can specify that you want to merge your local changes with the changes introduced by switching branches with the -m option to checkout.

git checkout -m branch2

If you've added changes to the index then you'll want to undo these changes with a reset first. (This will preserve your working copy, it will just remove the staged changes.)

git reset

I thought the stash "simpler" somehow to understand, but your approach is better at taking into account the working directory across different branches. +1
A plain traditional checkout seemed more appropriate to the problem in hand. checkout is lighter weight, it just updates the files that need to change. Perhaps it's easier to understand the stash approach, or it may just be that it's not obvious enough that checkout is 'safe' in this use case.
If checkout -m isn't "safe" in some situation (maybe it would cause a merge conflict), would stash provide any advantage (e.g. can you unpop a stash pop)?
@craigMcQueen You can't unpop a popped stash but stash would complain about conflicts when you pop it. You can fix the conflicts and then commit, but the original stash is still on the stack in this case ! :)
In the event of a merge conflict, aren't the files backed up as .orig?
r
rbento

A shorter alternative to the accepted answer would be:

Temporarily move the changes to a stash.

git stash

Create and switch to a new branch and then pop the stash to it in just one step.

git stash branch new_branch_name

Then just add and commit the changes to this new branch.


c
chakrit

WARNING: Not for git newbies.

This comes up enough in my workflow that I've almost tried to write a new git command for it. The usual git stash flow is the way to go but is a little awkward. I usually make a new commit first since if I have been looking at the changes, all the information is fresh in my mind and it's better to just start git commit-ing what I found (usually a bugfix belonging on master that I discover while working on a feature branch) right away.

It is also helpful—if you run into situations like this a lot—to have another working directory alongside your current one that always have the master branch checked out.

So how I achieve this goes like this:

git commit the changes right away with a good commit message. git reset HEAD~1 to undo the commit from current branch. (optional) continue working on the feature.

Sometimes later (asynchronously), or immediately in another terminal window:

cd my-project-master which is another WD sharing the same .git git reflog to find the bugfix I've just made. git cherry-pick SHA1 of the commit.

Optionally (still asynchronous) you can then rebase (or merge) your feature branch to get the bugfix, usually when you are about to submit a PR and have cleaned your feature branch and WD already:

cd my-project which is the main WD I'm working on. git rebase master to get the bugfixes.

This way I can keep working on the feature uninterrupted and not have to worry about git stash-ing anything or having to clean my WD before a git checkout (and then having the check the feature branch backout again.) and still have all my bugfixes goes to master instead of hidden in my feature branch.

IMO git stash and git checkout is a real PIA when you are in the middle of working on some big feature.


Interesting and valid alternative to my answer. +1
Are you coming from mercurial? The my-project-master sharing the same .git makes it sound like it. Why not git checkout -b bugfixABC; git commit -a; git reset HEAD^ --hard, then later (asynchronously) while on master, git cherry-pick <SHA1 of the commit(s) in bugfixABC? (or even, to avoid having to find out the SHA1, git rebase --onto master feature bugfixABC, from wherever branch you currently are on. Which means you can do that directly after the git reset above, while on feature.)
However, OP sounds like they are not ready to commit the changes, in that case just checkout -m is better.
c
claf

If it were about committed changes, you should have a look at git-rebase, but as pointed out in comment by VonC, as you're talking about local changes, git-stash would certainly be the good way to do this.


I do not understand this solution: it would rewrite commit history of branch2 from branch1... why getting all committed changes from branch1 in branch2 when we only want to get local non-committed changes of branch1 in branch2 ?...
@VonC : agreed, in this case, the rebase gets all committed changes since last merge between branches into branch1. I didn't get the "non-committed" parameter of this question at first. rebase isn't the good answer.
@claferri: pfew... I was beginning to have an headache ;) I would have downvoted your answer, but since I had myself published one, there was a "clear conflict of interest". With your updated post, I do not have to downvote at all now. Thanks :)
@VonC : next time, feel free to down vote as long as my answer is as wrong as this one ;)
S
Singh

These are the steps I follow:

git clone {link}

cd {repo folder}

You can check the status and which branch you are on using:

git status

git branch

git branch -a

Note: Here if you make changes in your local repo before moving to the new branch, the following steps should still work.

If "git branch" shows master, and you want to create+move to another branch:

git checkout -b {branch name}

Check branch again using "git branch" It should now show that you are in the new branch.

Now add, commit and push:

git add .

git commit -m "added new branch"

git push origin {branch name}

The above steps work for me in both the situation when I have made changes before moving to the new local branch or making changes after moving to the new branch. I hope it helps people running into similar situations.


Y
Yves Boutellier

If the uncommitted changes are a mix of untracked and tracked changes

What are untracked changes?

When you created e.g a new file. VSCode e.g displays a U next to the file in the file explorer.

What are tracked changes?

When you make changes to files that were committed to the repo earlier (in previous commits).

Quick explanation of procedure

So imagine you are on branch A, but you want to commit only changes to existing files to branch A, while the newly created file (untracked) should be committed to a new branch B. It's possible to use stashing with a few tricks, explained step by step.

Add script to .git/config

Inside the .git folder there is a config file. Open it up and you will see something like this:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = https://github.com/...
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
    remote = origin
    merge = refs/heads/main

change the config file to:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[alias]
    stash-untracked = "!f() {    \
                git stash;               \
                git stash -u;            \
                git stash pop stash@{1}; \
            }; f"
[remote "origin"]
    url = https://github.com/...
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
    remote = origin
    merge = refs/heads/main

Now you will be able to use the following command while you are on branch A.

git stash-untracked

You will see that the new file disappeared, if you are using a editor like VSCode (it's now stashed)

While still on branch A stage and commit the changes to the existing files:

git add .
git commit -m "committing tracked changes to current branch"

Next step is creating a new branch B (with checkout -b you visit it immediately)

git checkout -b newBranchName

When using stash pop the stashed changes get added to your current branch.

git stash pop

The only thing left is to stage and commit the changes on the new branch B

git add .
git commit -m "created new file"