转移到另一个分支并“保存”当前更改

6
这是一个场景:我正在A分支上工作,但出现了一些紧急情况需要修复,我该如何创建一个新的分支(称为B分支),而不会带来当前的更改?
我不想检出A分支中的所有文件,因为如果在B分支的工作完成后,我仍然想要继续A分支的工作。
我不想推送A分支,因为它还没有完成。

只需从主分支创建一个名为B的分支即可。 - Daniel A. White
@DanielA.White 我不想检出分支A中的所有文件,因为我希望在分支B的工作完成后能够继续。 - Jamie Anderson
在 Git 中,您可以一次检出所有文件,但是您可以轻松地在分支之间切换。 - Daniel A. White
@DanielA.White checkout -- . 我能之后恢复它们吗? - Jamie Anderson
是的,您可以提交并在分支之间切换。您甚至可以使用“存储”功能。 - Daniel A. White
3个回答

9
选项1:你可以将你的更改暂存起来
如果你在A分支中有更改: 在分支A中,输入以下命令:
git stash save "hint_to_what_stash_contains" 

为了获取更改内容,

git stash apply

如果您有多个暂存区,您可以列出它们并选择要应用的暂存区。
git stash list
git stash apply stash@\{<STASH_NUMBER>\}

选项2:提交您的更改。

首先,在branchA中提交您所有的更改,但不要将其推送到远程仓库,然后创建您的分支b来进行工作。 完成branchB后,您可以检出branchA以继续工作。

要在branchA中恢复工作

git checkout brancha
edit files
git add <file(s)>
git commit --amend # You can also change the commit message if you want.

选项3:我不建议使用的是补丁(patch)方法。我看到有些人使用它。你可以运行git diff并将其保存在一个文件中(如"branch_a_changes.patch"),当你想要恢复工作时,只需运行 git apply branch_a_changes.patch


4
您可以在A中提交您的更改。然后切换到B。当完成B后,返回到A并继续。或者,如果您不想提交'A'中的更改,可以存储(保存到其他地方)更改。然后切换到B,当您回到A时,只需从stash中取出(stash pop)更改即可。Stashing会将您工作目录的脏状态(即已修改的跟踪文件和暂存的更改)保存在未完成更改的堆栈上,您可以随时重新应用它们。
  • Options-1: Commit

    $ git checkout A                      # chekcout A
    $ git commit -am 'tmp commit'         # add & commit
    
    $ git checkout B                      # checkout B
    // fix your urgent changes here
    
    $ git checkout A
    // continue A
    
  • Options-2: Stash

    $ git checkout A    
    $ git add .
    $ git stash                           # save unfinished changes
    
    $ git checkout B                      # checkout B
    // fix your urgent changes here
    
    $ git checkout A                      # checkout A
    $ git stash apply                     # retrieve the changes from stash
    
    // you can clear the stash
    $ git stash drop                      # remove the changes from stash 
    

提交单个分支;而stash则使更改在多个分支中可用? - Jamie Anderson
@Jamie Anderson 是的,在任何分支上,您只需使用 git stash apply 命令即可恢复更改。 - Sajib Khan

3

git worktree --helphttps://git-scm.com/docs/git-worktree)中得知:

You are in the middle of a refactoring session and your boss comes in and demands that you fix something immediately. You might typically use git-stash(1) to store your changes away temporarily, however, your working tree is in such a state of disarray (with new, moved, and removed files, and other bits and pieces strewn around) that you don't want to risk disturbing any of it. Instead, you create a temporary linked working tree to make the emergency fix, remove it when done, and then resume your earlier refactoring session.

       $ git worktree add -b emergency-fix ../temp master
       $ pushd ../temp
       # ... hack hack hack ...
       $ git commit -a -m 'emergency fix for boss'
       $ popd
       $ rm -rf ../temp
       $ git worktree prune

git worktree add 在 Git 2.5 版本中首次出现,并在 2.6 版本中进行了一些重要修复,在此后的版本中也有了一些小的改进。我从 2.9 版本开始使用它,并且现在强烈推荐使用。 - torek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接