Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 234
bessarabov Avatar asked Jan 12 '12 06:01

bessarabov


People also ask

How do I see my local git history?

On GitHub.com, you can access your project history by selecting the commit button from the code tab on your project. Locally, you can use git log . The git log command enables you to display a list of all of the commits on your current branch.

Does git push push all local commits?

git push uploads all local branch commits to the corresponding remote branch.


1 Answers

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)

like image 102
Trevor Powell Avatar answered Sep 27 '22 22:09

Trevor Powell