如何在Git中执行以下操作?
我当前的分支是branch1,我已经做了一些本地更改。然而,我现在意识到我实际上是想将这些更改应用到branch2。是否有一种方法可以应用/合并这些更改,使它们成为branch2上的本地更改,而无需在branch1上提交它们?
如何在Git中执行以下操作?
我当前的分支是branch1,我已经做了一些本地更改。然而,我现在意识到我实际上是想将这些更改应用到branch2。是否有一种方法可以应用/合并这些更改,使它们成为branch2上的本地更改,而无需在branch1上提交它们?
当前回答
如果未提交的更改是未跟踪的更改和跟踪的更改的混合
什么是未跟踪的更改?
当你创建一个新文件时。例如,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"
其他回答
如果它是关于提交的更改,你应该看看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克隆{link} CD(回收文件夹)
你可以检查状态和你正在使用的分支:
git状态 git分支 Git分支-a
注意:在这里,如果您在迁移到新分支之前对本地回购进行了更改,那么以下步骤仍然有效。
如果“git branch”显示master,你想创建+移动到另一个分支:
Git checkout -b{分支名称}
使用“git branch”再次检查分支 它现在应该显示您在新的分支中。
现在添加、提交和推送:
Git添加。 Git commit -m "添加新分支" Git推送源{分支名称}
上述步骤适用于我在移动到新的本地分支之前进行更改或移动到新分支之后进行更改的情况。 我希望它能帮助遇到类似情况的人。
存储,临时提交和重基可能都是多余的。如果您还没有将更改后的文件添加到索引中,那么您可以只签出另一个分支。
git checkout branch2
只要你编辑的文件在branch1和branch2之间没有不同,这就可以工作。它将把您留在branch2上,并保留您的工作更改。如果它们是不同的,那么您可以指定您希望合并您的本地更改与通过切换分支引入的更改,并使用-m选项进行签出。
git checkout -m branch2
如果您已经向索引添加了更改,那么您将希望首先通过重置来撤销这些更改。(这将保留您的工作副本,它将删除阶段性更改。)
git reset
相对于公认的答案,一个简短的替代方案是:
临时将更改移动到存储区。
git 存储
创建和切换到一个新的分支,然后弹出隐藏到它在仅仅一个步骤。
Git存储分支new_branch_name
然后只需将更改添加并提交到这个新分支。