简体   繁体   English

Git 在 2 个分支上的一个文件之间显示差异以使它们相等

[英]Git show diff between one file on 2 branches to make them equal

I have the same file on development_branch and on master branch.我在 development_branch 和 master 分支上有相同的文件。 The correct version of the file is on development_branch and the version on master branch is very outdated.文件的正确版本在 development_branch 上,而 master 分支上的版本非常过时。

I know for sure that the version on development_branch is 100% accurate and I could simply replace the file on master branch to be 1:1 equal, but I want to keep the commit history, so is there any way I could get the commits from development_branch that are not on master branch to easily make it equal?我确信 development_branch 上的版本是 100% 准确的,我可以简单地将 master 分支上的文件替换为 1:1 相等,但我想保留提交历史,所以有什么办法可以从不在 master 分支上的 development_branch 可以轻松使其相等?

There were some conflicts during merging and some of them might have been resolved incorrectly, in an incorrect order, there were many modifications etc. I want to get the exact file I have on the development_branch as an output.合并过程中出现了一些冲突,其中一些可能没有正确解决,顺序不正确,有很多修改等。我想获得 development_branch 上的确切文件作为输出。

Any hints?任何提示?

Merge develop into master, assuming you want all content from develop, pause the commit and checkout develop version of file将 develop 合并到 master 中,假设您希望所有内容来自 develop,暂停提交并检出文件的开发版本

git merge  --no-commit develop
git checkout develop -- PATH_TO_FILE
git commit

I know for sure that the version on development_branch is 100% accurate and I could simply replace the file on master branch to be 1:1 equal, but I want to keep the commit history ...我确信 development_branch 上的版本是 100% 准确的,我可以简单地将 master 分支上的文件替换为 1:1 相等,但我想保留提交历史...

There is only one kind of history in a Git repository, and that history is the set of commits in the repository. Git 存储库中只有一种历史记录,该历史记录是存储库中的一组提交。

Adding new commits to the repository does not change the existing commits in the repository.向存储库添加新提交不会更改存储库中的现有提交。 It literally cannot change them.它实际上无法改变它们。

Hence all you need to do in this case is extract the desired contents from the commit that has them, write them to your index (which is where Git builds the next commit to make), and make a new commit.因此,在这种情况下,您需要做的就是从包含它们的提交中提取所需的内容,将它们写入您的索引(这是 Git 构建下一个要进行的提交的地方),然后进行新的提交。 To do that:要做到这一点:

# git checkout master  # if needed - but you're already there
git checkout development_branch -- path/to/file

and you should now be ready to git commit the updated file (use git status , git diff --cached , etc., to make sure).并且您现在应该准备好git commit更新的文件(使用git statusgit diff --cached等来确保)。

(In Git 2.23 or later, you can use git restore instead of git checkout -for-a-single-file. The command line you end up typing in is longer, but the operation is clearer: the goal is to extract the given file from the given commit, writing it to both index and work-tree.) (在 Git 2.23 或更高版本中,您可以使用git restore而不是git checkout -for-a-single-file。您最终输入的命令行更长,但操作更清晰:目标是提取给定文件从给定的提交,将其写入索引和工作树。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM