Home > Enterprise >  How to check with git if a file is committed but not pushed
How to check with git if a file is committed but not pushed

Time:07-01

With git status --porcelain <file> I can check if a file is modified, deleted, added, ...

https://git-scm.com/docs/git-status#_short_format

If the file was modified and committed locally, git status --porcelain <file> doesn't return any value.

With git status <file>, I get that the whole repo is x commits ahead, but that's for the whole repo and not for a single file only.

How can I check a single file if it's committed but not pushed.

CodePudding user response:

A pretty close thing I can think of :

git fetch

# check if there are modifications on said file in your current branch
# versus its "fork point" from its upstream branch
#  - '@{u}' is short for "the upstream of the current branch"
#  - 'git diff a...b' (3 dots) is short for "the diff between b and
#    the  fork point between a and b"

git diff --name-only @{u}...HEAD -- path/to/file

If no output is printed, this means that all the modifications you have on that file in your current commit have been uploaded to the upstream.
If there are other differences in the upstream branch, this means they come from some other source (someone else pushed changes to that branch, or a pull request was merged, or ...)

You still have to use git status path/to/file or git diff HEAD -- path/to/file if you want to see whether there are uncommitted modifications on that file.


use --quiet to use it as a condition in a script :

if ! git diff --quiet @{u}...HEAD -- path/to/file; then
    echo "there are some differences in path/to/file"
fi

CodePudding user response:

You can use the git diff --cached and the git status --verbose as well.

The git diff command displays the changes between the working directory and the staging area.

The git status command is run to show the state of the working directory and the staging area.

The --verbose option not only shows the names of changed files but also displays the textual changes staged to be committed.

CodePudding user response:

When you commonly push something toward a repository, you usually work on a branch configured to "track" an homologous one located in the remote repository. The state of this branch is maintained locally each time you fetch the branch, either using fetch, push', 'pull or when you do git remote update.

So what you simply need to do once you have committed is to compare you local branch and its remote counterpart. This will tell you if a commit has been pushed or not yet, then you can specify a file name to restrict the comparison to this sole file.

Assuming you're working on master, this will tell you if there are discrepancies in general:

git diff origin/master master [your_file]

And this one will tell you which commits are not pushed yet:

git log origin/master..master [your_file]

Here, origin/master is the remote branch ans is on the left because it's supposed to represent the original state, master is the local branch and immediately follows the former one because it's supposed to be the state we're reaching.

  •  Tags:  
  • git
  • Related