Home > Software engineering >  How to differentiate between files that need to be pulled in Git, and files that are committed local
How to differentiate between files that need to be pulled in Git, and files that are committed local

Time:07-05

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.

CodePudding user response:

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.

CodePudding user response:

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.

  • Related