简体   繁体   中英

Check that the local git repo has everything committed and pushed to master

I have some shell script where I want to check one of my git repos. I want to know if that repos has everything committed and if it is pushed to master. Before this test I make git fetch to make sure I have the latest changes.

I have found the way to check if the repo has some uncommitted changes:

if ! git --work-tree=$HOME/git/project --git-dir=$HOME/git/project/.git diff-index --quiet HEAD --; then
    echo "Has some changes";
fi

But this is not the only thing I need. I also want to make sure that all my local commits are pushed to master.

What is the easiest way to do so?

A very easy way to do it would be to simply call

git push -n

(the "-n" is short for "--dry-run", which means that instead of doing the push, it will instead tell you what it would have pushed)

If it says "Everything up-to-date", then you've already pushed everything to origin.

Alternately, it will give you a list of all the commits that have not yet been pushed to origin.

Or if there are changes on origin that you haven't yet pulled down, then it might complain about potential merges which would be caused by pushing (this duplicates the "has some changes" checking you're already doing)

You can check that everything is committed with:

git diff --exit-code && git diff --cached --exit-code

In a typical configuration, on a successful push to a master in origin , the remote-tracking branch origin/master will be updated. So, to check if you've pushed all your changes, you can test if:

git rev-parse --verify master

... is the same as:

git rev-parse --verify origin/master

After a git fetch , to check if there are local commits in your branch that have not been pushed to a remote, try this:

git diff --exit-code <remote>/<branch>..<branch>

This will also tell you if there are commits in the remote that you do not have locally. Note that this is just checking for changes, so if there were to be different commits with identical changes in the remote and the local branch, it is possible that this command would not detect that. In a script, I typically pipe this to /dev/null and just check the return status. So, assuming your remote is origin and your branch is master , the command would look like this:

git diff --exit-code origin/master..master > /dev/null

As others have suggested, I also use git diff --exit-code and git diff --cached --exit-code to check for local uncommitted changes.

From Mark Longair's great answer I understood that is is possible to check that local git repo has everything commited and pushed but you need to run several commands to do so.

I've written a small script that does all that and write in a frienly way what has happened.

$ is_git_synced ~/git/* --only_errors
Error: path '/home/bessarabov/git/Dancer' has staged changes
Error: path '/home/bessarabov/git/Ubic' has some divergences with remote 'origin'

And you can also use exit code to find out if everything is commited and pushed or not.

It is at Github: https://github.com/bessarabov/App-IsGitSynced and on CPAN: https://metacpan.org/module/App::IsGitSynced

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM