Git - Find which commits are local

120 views Asked by At

How do I know which of local commits are not in remote?

By, git log I see commit history, but I want to find if the last commit is already pushed into server or just a local one. Is there any way to know that without going to server and matching with local history?

4

There are 4 answers

0
Sazzad Hissain Khan On BEST ANSWER

Finally I got a clue,

git status

On branch dev
Your branch is ahead of 'origin/dev' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean

Git status also shows the number of commits your current branch ahead from remote.

Thanks to @Danh for comment

1
Sajib Khan On

Copy the last commit hash, and see the list of branch(es) exists the commit.
If you see the remote/origin/<branch-name> then you have pushed the last commit.

$ git fetch
$ git log                                    # copy last commit-hash
$ git branch -a --contains <last-commit-hash>

# Or, (skip copying commit-hash)
$ git fetch && git branch -a --contains $(git rev-list -n 1 HEAD)
0
Marina Liu On

Yes, you can check by git log origin/master..master.

If there has output, then the showing log is which you didn’t push to remote. If there is no output, that means your current branch is same as master.

Note: if the git remote also used by others, you’d better fetch from origin and then compare.

0
Dave On

Git has a number of shortcuts, for example HEAD refers to the tip of the current branch, while @{upstream} refers to the head of remote branch (or the last pushed commit). These shortcuts are invaluable for using in aliases for example, so you aren't locked into using branch names like master and fixed remote names like origin.

Using git log;

git log @{upstream}..HEAD

I use the following

git config alias.changes "log --graph --abbrev-commit --decorate --date=relative --pretty=terse @{upstream}...HEAD"

I then simply use

git changes

to list the un-pushed changes in my current branch.

Note: if @{upstream} doesn't seem to work, pay attention to the -u or --set-upstream option in git push. Using git push -u origin master will ensure that origin/master is considered the upstream repository for branch master. Then the @{upstream} shortcuts should work properly.