Check if local git repo is ahead/behind remote

Vittorio Romeo

I'm developing a git plug-in, and I need to know when a local repo is changed (can commit changes), ahead (can push to remote) or behind (can pull from remote) using the command line.

This is what I am doing so far:

  • Can commit?

    If git diff-index --name-only --ignore-submodules HEAD -- returns something, then yes, there are changes to commit.

  • Can push?

    If git status -sb contains the word ahead in it's output, then yes, there are commits to push.

  • Can pull?

    Nothing implemented yet.

The can commit? part seems to work properly. Can push? only works for the master branch, and this is a huge problem.

How can I safely check if, on every branch, a git repo has changes to commit, commits to push, or needs a git pull?

Vittorio Romeo

In the end, I implemented this in my C++11 git-ws plugin.

string currentBranch = run("git rev-parse --abbrev-ref HEAD"); 
bool canCommit = run("git diff-index --name-only --ignore-submodules HEAD --").empty();
bool canPush = stoi(run("git rev-list HEAD...origin/" + currentBranch + " --ignore-submodules --count")[0]) > 0;

Seems to work so far. canPull still needs to be tested and implemented.

Explanation:

  • currentBranch gets the console output, which is a string of the current branch name
  • canCommit gets whether the console outputs something (difference between current changes and HEAD, ignoring submodules)
  • canPush gets the count of changes between origin/currentBranch and the local repo - if > 0, the local repo can be pushed

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to compare local with remote git repo in PhpStorm?

From Dev

Merge local git repo with remote one

From Dev

How to compare GIT Remote Repo and local in Netbeans

From Dev

Bring a local folder to remote git repo

From Dev

git not overwriting remote repo with local files?

From Dev

Push local new Git repo to existing remote repo as branch?

From Dev

Git / GitHub: How to get new local repo into empty remote repo

From Dev

unable to push my local git repo to the remote repo

From Dev

Remote Repo name changed in Github, but not reflecting in Local Repo with Git fetch

From Dev

check in local workspace into new git repo

From Dev

check in local workspace into new git repo

From Dev

Drop remote branch and reset local repo to match remote using GIT

From Dev

Check If Local Branch Exists On Remote Git

From Dev

How to remove a file from git repo on local and remote

From Java

Push local Git repo to new remote including all branches and tags

From Java

Delete local Git branches after deleting them on the remote repo

From Dev

How to disconnect local git repo from remote master

From Dev

Creating a git repo on a remote server from a local machine

From Dev

Going remote from local repo: Git and forgetting large files

From Dev

Can I add a file to my local Git repo but not push it to remote

From Dev

Going remote from local repo: Git and forgetting large files

From Dev

Git - Removing multiple previous commits from both local and remote repo

From Dev

Working with remote repo git

From Dev

Git clone remote repo into another remote repo

From Dev

Why does Git show my local "pull from remote" when I push my commit to the remote repo?

From Dev

reassign an existing remote repo to my local repo

From Dev

How can I convert all the remote branches in a local git repo into local tracking branches

From Dev

Sync local git repo with remote in one shot discarding local changes/commits

From Dev

Sync local git repo with remote in one shot discarding local changes/commits

Related Related

  1. 1

    How to compare local with remote git repo in PhpStorm?

  2. 2

    Merge local git repo with remote one

  3. 3

    How to compare GIT Remote Repo and local in Netbeans

  4. 4

    Bring a local folder to remote git repo

  5. 5

    git not overwriting remote repo with local files?

  6. 6

    Push local new Git repo to existing remote repo as branch?

  7. 7

    Git / GitHub: How to get new local repo into empty remote repo

  8. 8

    unable to push my local git repo to the remote repo

  9. 9

    Remote Repo name changed in Github, but not reflecting in Local Repo with Git fetch

  10. 10

    check in local workspace into new git repo

  11. 11

    check in local workspace into new git repo

  12. 12

    Drop remote branch and reset local repo to match remote using GIT

  13. 13

    Check If Local Branch Exists On Remote Git

  14. 14

    How to remove a file from git repo on local and remote

  15. 15

    Push local Git repo to new remote including all branches and tags

  16. 16

    Delete local Git branches after deleting them on the remote repo

  17. 17

    How to disconnect local git repo from remote master

  18. 18

    Creating a git repo on a remote server from a local machine

  19. 19

    Going remote from local repo: Git and forgetting large files

  20. 20

    Can I add a file to my local Git repo but not push it to remote

  21. 21

    Going remote from local repo: Git and forgetting large files

  22. 22

    Git - Removing multiple previous commits from both local and remote repo

  23. 23

    Working with remote repo git

  24. 24

    Git clone remote repo into another remote repo

  25. 25

    Why does Git show my local "pull from remote" when I push my commit to the remote repo?

  26. 26

    reassign an existing remote repo to my local repo

  27. 27

    How can I convert all the remote branches in a local git repo into local tracking branches

  28. 28

    Sync local git repo with remote in one shot discarding local changes/commits

  29. 29

    Sync local git repo with remote in one shot discarding local changes/commits

HotTag

Archive