如何在Git中执行以下操作?

我当前的分支是branch1,我已经做了一些本地更改。然而,我现在意识到我实际上是想将这些更改应用到branch2。是否有一种方法可以应用/合并这些更改,使它们成为branch2上的本地更改,而无需在branch1上提交它们?


当前回答

如果它是关于提交的更改,你应该看看git-rebase,但正如VonC在评论中指出的,当你谈论本地更改时,git-stash肯定是做到这一点的好方法。

其他回答

我发现这个答案很有用。

然而,由于该线程已关闭,无法作出评论,我有一个问题的答案。

当我应用git checkout other_branch时,我得到了以下错误

error: pathspec 'other_branch' did not match any file(s) known to git

因此,我没有应用这个命令,而是使用它来解决我的问题

git branch other_branch
git checkout other_branch

如果它是关于提交的更改,你应该看看git-rebase,但正如VonC在评论中指出的,当你谈论本地更改时,git-stash肯定是做到这一点的好方法。

因为你的文件还没有在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

上面是rbento回答的更长的更明确的版本:

git stash
git stash branch branch2

它使用:

git stash branch <branchname> [<stash>] Creates and checks out a new branch named <branchname> starting from the commit at which the <stash> was originally created, applies the changes recorded in <stash> to the new working tree and index. If that succeeds, and <stash> is a reference of the form stash@{<revision>}, it then drops the <stash>. 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.


正如benjohn所评论的(见git stash手册页):

要保存当前未跟踪的(新添加的)文件,添加参数-u,这样:

git stash -u

存储,临时提交和重基可能都是多余的。如果您还没有将更改后的文件添加到索引中,那么您可以只签出另一个分支。

git checkout branch2

只要你编辑的文件在branch1和branch2之间没有不同,这就可以工作。它将把您留在branch2上,并保留您的工作更改。如果它们是不同的,那么您可以指定您希望合并您的本地更改与通过切换分支引入的更改,并使用-m选项进行签出。

git checkout -m branch2

如果您已经向索引添加了更改,那么您将希望首先通过重置来撤销这些更改。(这将保留您的工作副本,它将删除阶段性更改。)

git reset

如果未提交的更改是未跟踪的更改和跟踪的更改的混合

什么是未跟踪的更改?

当你创建一个新文件时。例如,VSCode在文件资源管理器的文件旁边显示一个U。

什么是被跟踪的变更?

当您更改之前(在以前的提交中)提交给repo的文件时。

程序快速讲解

因此,假设您在分支A上,但是您只想将对现有文件的更改提交到分支A,而新创建的文件(未跟踪的)应该提交到新的分支b。可以使用一些技巧来使用存储,并逐步解释。

添加脚本到.git/config

在.git文件夹中有一个配置文件。打开它,你会看到这样的东西:

[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

将配置文件更改为:

[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

现在您可以在分支A上使用以下命令。

git stash-untracked

如果你使用的是VSCode这样的编辑器,你会看到新文件消失了(现在它已经被保存了)

当仍然在分支A阶段时,将更改提交到现有文件:

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

下一步是创建一个新的分支B(签出-b你立即访问它)

git checkout -b newBranchName

当使用隐藏弹出隐藏的变化被添加到您的当前分支。

git stash pop

剩下的唯一事情就是在新的分支B上执行并提交更改

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