简体   繁体   English

如何判断自上次git pull或git push之后我更改过的git pull将覆盖哪些文件?

[英]How to tell what files would be over written by a git pull that I've changed since the last git pull or git push?

I'm working on a master branch on two different machines and pushed code to a remote repository on one of the machines. 我正在两台不同计算机上的master分支上工作,并将代码推送到其中一台计算机上的远程存储库中。

I'm trying to push code to the remote repo from the other machine but I get the error 我正在尝试将代码从另一台计算机推送到远程存储库,但出现错误

 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to  'https://github.com/'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

I'm planning on doing a $ git pull but wanted to know if there was a way I could tell what files would be over written that I've made changes to locally since the last git pull from the remote repo or git push from the remote repo. 我打算进行$ git pull但是想知道是否有办法告诉我自从上次从远程repo进行git pull以来,还是在本地进行了git push之后,我对本地文件进行了更改。远程回购。

I'm not interested in a complete list of modified files. 我对已修改文件的完整列表不感兴趣。

I'm not sure if I really understand what you want, so I will name some possible situations and resolutions. 我不确定我是否真的了解您想要什么,所以我会列举一些可能的情况和解决方案。

  1. If you want know files your local branch different from remote counterpart: 如果您想知道文件,则本地分支与远程分支不同:

     git diff --name-status master origin/master 
  2. If you plan to pull, but want to know what will be modified: 如果您打算拉,但想知道将被修改的内容:

     git pull --no-commit 

    git will leave a merged working tree to you and you can check modified files by command like git status . git会给您留下合并的工作树,您可以通过git status类的命令检查修改后的文件。

  3. If you want to make a clean history, I recommend (maybe not you planned to) 如果您想清除历史记录,我建议(也许您不打算这样做)

     git pull --rebase 

    it will fetch the latest code from remote and apply your local changes on it. 它将从远程获取最新代码并在其上应用本地更改。

I don't know of any git internal way to do this (though I wouldn't be surprised to find out that there was one) but I believe the following should do what you want (after you have fetched the remote changes at least). 我不知道执行此操作的任何git内部方法(尽管我不会惊讶地发现有这种方法),但是我相信以下内容应该可以满足您的要求(至少在您获取了远程更改之后) 。

# Find merge base between the branch heads.
mb=$(git merge-base master origin/master)

# List of files changed by local master.
git diff --name-only $mb master

# List of files changed by origin/master.
git diff --name-only $mb origin/master

# List of files changed by both heads.
comm -1 -2 <(git diff --name-only $mb master) <(git diff --name-only $mb origin/master)

To find out which (locally changed and uncommitted files) overlap with files changed on the remote branch try: 要找出哪些(本地更改和未提交的文件)与远程分支上更改的文件重叠,请尝试:

# List of files locally changes (uncommitted).
git diff --name-only

# List of files changed in working directory and on remote branch.
comm -1 -2 <(git diff --name-only) <(git diff --name-only $mb origin/master)

It should be noted that the locally changed version will not flag locally added files (unless you have used git add -N to tell git about them). 应当注意,本地更改的版本不会标记本地添加的文件(除非您已经使用git add -N告诉git有关它们的信息)。

First make sure that you do a git fetch so that you know that your remote branch is up to date. 首先,请确保您执行git fetch以便您知道远程分支是最新的。 Then you can use this one-liner to get the differences in your uncommitted files and the remote. 然后,您可以使用此一线来获取未提交的文件和远程文件之间的差异。

git status | grep "(modified|deleted)" | awk '{print $3}' | git diff <remote branch name>

This command takes the git status and finds the modified files, parses them so that it only has the file names. 此命令采用git status并查找修改后的文件,对其进行解析,使其仅具有文件名。 Finally it creates a diff from the origin with those file names. 最后,它使用这些文件名从源创建差异。

You are going to have to examine the diff for the specific changes but this gets you the information about the uncommitted changes. 您将需要检查差异的特定更改,但这会为您提供有关未提交的更改的信息。

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

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