Make sure the local git repository has everything that has been committed and pushed to master

I have a shell script where I want to check out one of my git repos. I want to know that this REPO has everything that has been committed, and if it is pushed towards mastery. Before this test, I do git fetch to make sure I have the latest changes.

I found a way to check if the repo has any 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 placed on the master.

What is the easiest way to do this?

+6
source share
4 answers

There is an excellent answer from Mark Longair, I realized that you can check that in the local git repository there is everything that is done and pressed, but you need to run several commands for this.

I wrote a small script that does all this and writes in pure form what 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 the exit code to find out if everything is done and pressed or not.

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

-1
source

A very easy way to do this is to simply call

git push -n

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

If it says "Everything relevant", you have already thrown everything back to its original position.

As an alternative, it will provide you with a list of all the commits that have not yet been rescheduled.

Or, if there are changes in the origin that you have not yet removed, then he may complain about possible mergers that will be triggered by clicking (this duplicates โ€œsome changesโ€ that you already make)

+8
source

You can verify that everything is done with:

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

In a typical configuration, if you successfully click on master in origin , the remote branch origin/master will be updated. So, to check if you pushed all your changes, you can check if there are:

 git rev-parse --verify master 

... is the same as:

 git rev-parse --verify origin/master 
+5
source

After git fetch , to check if there are local commits in your branch that were not pushed to the remote, try the following:

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

It will also tell you if there are commits on the remote computer that you do not have on the local computer. Please note that this is just a change check, so if there were different commits with identical changes in the remote and local branch, this command might not detect this. In a script, I usually associate this with /dev/null and just check the return status. So, if your remote is origin and your master branch, the command will look like this:

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

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

+1
source

Source: https://habr.com/ru/post/905794/


All Articles