-1

How do I determine which files in my local repository are committed and ready to be pushed, and which files have been pushed by someone else in the meantime and need to be pulled, once you've already done a 'git fetch'?

For example:

git fetch
...

$ git diff --stat --staged origin/Release_Candidate

 AP4Configuration/ChangeLog.txt    | 3 +--
 AP4Configuration/appsettings.json | 3 +--
 Local5.txt                        | 1 -
 3 files changed, 2 insertions(+), 5 deletions(-)

I staged and committed appsettings.json and local5.txt and they are ready to be pushed as part of the local commit(s), but Changelog.txt needs to be pulled from the repository on the server.

How can I tell that Changelog.txt is in a commit on the server that needs to be pulled, and appsettings.json and local5.txt are from a commit on my local machine that needs to be pushed?

I get the same result from:

git diff --stat --cached origin/Release_Candiate
git diff --stat --HEAD origin/Release_Candiate

Is there any way I can determine which files need to be pulled, and which ones are ready to push?

For example, I would like to see the files in the local commit(s) that will be pushed, without seeing the ones that are in the latest commit on the server ready to be pulled.

I am updating a GUI Git interface for my workplace, and the users need to see this information. Thanks.

  • 3
    Please consider rethinking how you see git. Git doesn't push or pull files – evolutionxbox Jul 01 '22 at 11:49
  • Staged files are not ready to be pushed. – phd Jul 01 '22 at 12:19
  • OK, I've edited the question. I meant that my files were staged and committed to local repository. And I want to see the files in the commit that will be pushed, without seeing the ones that are in the latest commit on the server ready to be pulled. – Derek Mason Jul 01 '22 at 14:14
  • @DerekMason Please provide some context on **why** you want to see this information. – Daniel Mann Jul 01 '22 at 14:33
  • I wrote a bespoke Git GUI system for my workplace to aid the staff work together using Git source-control (It needed to be bespoke because the off-the-shelf packages were not suitable). As part of the system we need to be able to see the files to pull, the files to push (obviously, we're pushing and pulling commits). But since we moved to a Release_Candidate branch (instead of master), the system is having difficulty differentiating from files in commits that should be pulled, and files in commits that will be pushed. So I am trying to break it down into Git commands that I can use. – Derek Mason Jul 01 '22 at 14:43
  • 1
    "which files have been pushed by someone else in the meantime and need to be pulled (after a 'git fetch')" Without doing a `git fetch` first, how would it be possible to know this (even if the question were meaningful)? – matt Jul 01 '22 at 15:13
  • I have done a 'git fetch'. Once this is done, the list in my example shows 'ChangeLog.txt', which is part of the repository that is to be pulled. All I want to know is if there is any way to determine that this file is not part of my local repository changes that will be pushed. – Derek Mason Jul 04 '22 at 06:53

2 Answers2

0

There are no changes in your local repository that have been pushed by someone else unless you have done a git fetch (directly or via git pull). Those files are in a remote repository.

When you fetch the upstream changes with git fetch, the remote branch is updated to the most recent commit.

Your local changes are on the local branch which tracks the remote branch, and therefore you don't see those changes on this branch.

For instance, if the remote repository is called origin and the branch is master, then the remote branch is origin/master, and master refers to the local branch.

You can inspect the remote changes with git log and other tools. E.g. if the remote branch is called origin/master, you can git log origin/master to list the new changes you've picked up after git fetch.

To combine the two, you must either rebase or merge.

Under git rebase, all your local changes that you have committed are first stowed away somewhere, and then your branch is updated to match remote branch. Your changes are then cherry-picked back into your local branch, one by one. Automatic merging takes place, but conflicts may arise that you have to manually repair; there is a workflow for that whereby the rebase stops, leaving you with a situation where all the cherry-picked changes are staged, except for the conflicted files, which you must edit to remove conflict markers and then add to the stage with git add; then git rebase --continue.

Your local branch cannot be pushed (at least not without --force) to the remote repository unless it is based on the current remote branch (i.e. has the current commit of the remote branch as an ancestor). To push your change you have fetch, rebase and push. If the push is rejected because someone else pushed something new while you were rebasing, you must repeat fetch, rebase and push.

Kaz
  • 55,781
  • 9
  • 100
  • 149
  • Thank you for your detailed answer. However, I think I am having trouble explaining my question. In my example, I have staged and committed some files locally. Also, I have done a 'git fetch'. Once this is done, the list in my example also shows 'ChangeLog.txt', which is part of the repository that is to be pulled. All I want to know is if there is any way to determine that this file is not part of my local repository changes that will be pushed, i.e. it is from the repository on the server that first needs to be pulled. Thanks. – Derek Mason Jul 04 '22 at 07:08
  • `Derek Mason` To answer the question "do I have changes to `ChangeLog.txt` in one of my unpublished commits", I would do any one of these thiings: do a `git diff origin/master ChangeLog.txt`. Or perhaps open up the graphical `gitk` and browse my branch. View the log: `git log -p`. – Kaz Jul 04 '22 at 16:25
0

I've finally figured out what I need to do.

After a 'git fetch', the following command shows both local and remote changes:

$ git diff --stat --cached origin/Release_Candidate

 AP4Configuration/ChangeLog.txt    | 3 ++-
 AP4Configuration/appsettings.json | 3 ++-
 Local5.txt                        | 1 +
 3 files changed, 5 insertions(+), 2 deletions(-)

But to see the file(s) that I myself modified, and that will be pushed as part of my local commits:

$ git diff --stat --merge-base origin/Release_Candidate

 AP4Configuration/appsettings.json | 3 ++-
 Local5.txt                        | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)

This list does not show Changelog.txt. It now only shows the files I committed locally. Therefore I can see which files in the commits will be pushed, and which ones will need pulling from the server-side commits.