Git: How to check whether local branch is ahead of remote branch when working directory is modified?

StackOverflow https://stackoverflow.com/questions/17571979

  •  02-06-2022
  •  | 
  •  

Question

Normally, when the working directory is clean, I can use "git status". The output looks like:

# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)

However, if the working directory is modified, "git status" outputs:

# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   body/chap1.tex
#
no changes added to commit (use "git add" and/or "git commit -a")

The second result doesn't show if I have pushed all changes to remote branches. Sometimes, I still want the first result when the working directory is modified. How to get it in such condition?

Was it helpful?

Solution

This is the purpose of the staging tree. You can stage the work you want to commit and then send to the repo as followed:

git add path/to/your/file.php

This will stage a file so that it will then be stored in the staging tree (separate from your committed work or your uncommited work), then future git status calls will show you the work you have staged as separate from the work you have uncommitted. This is standard practice in git and lets you keep track of what you are going to commit. Staging is a method for allowing selective commits, but it should server your purposes.

Here is a link that better explains the staging area: http://git-scm.com/book/ch1-3.html

OTHER TIPS

Perhaps the simplest would be to stash the current changes, check and then pop the stash. As such,

git stash
git status
git stash pop

This won't work for all changes, such as new files (because git stash won't stash them). However, I have to say that I am not reproducing your issue:

ebg@taiyo(14)$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)
ebg@taiyo(15)$ ed .gitignore 
1165
...
q
ebg@taiyo(16)$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   .gitignore
#
no changes added to commit (use "git add" and/or "git commit -a")

In the above, the status relative to origin still appears. Still, my suggestion works:

ebg@taiyo(17)$ git stash
Saved working directory and index state WIP on master: b541ae8 Ignore 'SPOT Lang*' files
HEAD is now at b541ae8 Ignore 'SPOT Lang*' files
ebg@taiyo(18)$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)
ebg@taiyo(19)$ git stash pop
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   .gitignore
#
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (8ff02f7a27053ca7680b0a98048542fcbe2bb440)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top