3

I'm working on a master branch on two different machines and pushed code to a remote repository on one of the machines.

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.

I'm not interested in a complete list of modified files.

user784637
  • 15,392
  • 32
  • 93
  • 156
  • You want the list of files that both you and the remote have modified on this branch? – Etan Reisner Aug 20 '13 at 12:46
  • Would the accepted answer here help? http://stackoverflow.com/questions/9113280/diff-current-working-copy-of-a-file-with-another-branchs-committed-copy I may have misunderstood. – Chris Aug 20 '13 at 13:12
  • Hmm not quite sure, see my comment to the current answer. I want to know what local changes would be overwritten by a git pull. – user784637 Aug 20 '13 at 13:25

3 Answers3

4

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.

  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.

dyng
  • 2,854
  • 21
  • 24
  • I want to know what local changes would be overwritten by the pull. 1) lists all modifications, added and deleted. So I want only a list of modified files which were also changed locally. – user784637 Aug 20 '13 at 13:38
  • @user784637 Then you can try my second answer :) – dyng Aug 20 '13 at 13:42
0

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).

# 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).

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • I think it may be possible in a more concise manner, when I try to pull from the remote repo, it tells me what local changes would be overwritten by the pull. `* branch master -> FETCH_HEAD` `error: Your local changes to the following files would be overwritten by merge:` `public_html/index.php` – user784637 Aug 20 '13 at 13:06
  • That's why I asked what you wanted. Knowing what committed changes you made that touch the same files is different than knowing what uncommitted changes you have locally that touch the same files. I answered the committed version. – Etan Reisner Aug 20 '13 at 13:36
0

First make sure that you do a git fetch so that you know that your remote branch is up to date. 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. 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.

Schleis
  • 41,516
  • 7
  • 68
  • 87