git 在不影响当前分支的情况下从另一个分支获取更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40253526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get changes from another branch without affecting current branch at all
提问by Andrii Zymohliad
Is there a simple way to get changes from another branch without merge or rebase. And keep those changes as untracked (for new files) or not staged for commit (for existing files)?
有没有一种简单的方法可以在不合并或变基的情况下从另一个分支获取更改。并将这些更改保持为未跟踪(对于新文件)或不暂存以进行提交(对于现有文件)?
回答by ?rel
do a merge to get the change then cancel the merge but keep modification:
进行合并以获得更改然后取消合并但保留修改:
git merge feature
git reset HEAD~1
回答by 1615903
You could use git cherry-pick -n <commit>...
. It takes the changes from one or more commits and applies them to your current working tree without making a commit.
你可以使用git cherry-pick -n <commit>...
. 它从一个或多个提交中获取更改并将它们应用于您当前的工作树,而无需提交。
Documentation for -n
flag:
-n
标志的文档:
-n
--no-commit
Usually the command automatically creates a sequence of commits. This flag applies the changes necessary to cherry-pick each named commit to your working tree and the index, without making any commit. In addition, when this option is used, your index does not have to match the HEAD commit. The cherry-pick is done against the beginning state of your index.
This is useful when cherry-picking more than one commits' effect to your index in a row.
-n
--不提交
通常该命令会自动创建一系列提交。此标志应用必要的更改来挑选每个命名提交到您的工作树和索引,而不进行任何提交。此外,使用此选项时,您的索引不必与 HEAD 提交匹配。樱桃挑选是针对索引的开始状态完成的。
当连续挑选多个提交对索引的影响时,这很有用。
回答by njam
You can use git diff <another-branch> ^HEAD
to print a diff of the changes that are in "another-branch", but notin your current branch (HEAD). And then apply those changes to the current index by passing them to git apply -
.
您可以使用git diff <another-branch> ^HEAD
打印“another-branch”中但不在当前分支(HEAD)中的更改的差异。然后通过将这些更改传递给git apply -
.
git diff <another-branch> ^HEAD | git apply -
回答by njam
This requires your working tree to be clean (no modifications from the HEAD commit)1.
这要求您的工作树是干净的(没有来自 HEAD 提交的修改)1。
git cherry-pick <commit>
git reset --soft HEAD~1
git reset .
Will apply changes from another branch to your current branch if commit exists keeping the new files untracked and existing files unstaged.
如果存在提交,则将更改从另一个分支应用到当前分支,保持新文件未跟踪和现有文件未暂存。
If you are interested to know how to apply changes from an another branch in an another repository to your current repository. This can be done here.
如果您有兴趣知道如何将另一个存储库中另一个分支的更改应用到您当前的存储库。这可以在这里完成。
回答by Kelsey Hannan
To grab changes without a merge, you can use:
要在不合并的情况下获取更改,您可以使用:
git fetch
git fetch
to grab changes without changing your current branch.
在不更改当前分支的情况下获取更改。
回答by Ganeshrajhan
Do a checkout from your current branch and pull from another branch. This pulls all the commits from the other branch into the current branch. You can work on all the changes without changes being committed to actual branch. Optionally you can commit and push if these changes needs to be tracked.
从您当前的分支结帐并从另一个分支拉取。这会将所有提交从另一个分支拉入当前分支。您可以处理所有更改,而无需将更改提交到实际分支。如果需要跟踪这些更改,您可以选择提交和推送。
git checkout <current branch>
git pull origin <another branch>
git commit
git push HEAD