22

I was checking the X commits using the following command:

git log --author=<my-name> -<X>

But the problem is that I accidentally pulled code from another repository and added the commits from the repository to my local git repository.

So I cannot use the above command since the new commits contains some other authors.

Mohammed H
  • 6,880
  • 16
  • 81
  • 127
  • 1
    if your working copy is clean (no uncommitted changes) you can go back to the state before pull with http://stackoverflow.com/q/1223354/11343 – CharlesB Jan 04 '13 at 09:39
  • @CharlesB "git reset --hard" won't take to the state before pull. The message still comming. – Mohammed H Jan 04 '13 at 10:25
  • 1
    you need to find the commit hash of the commit checked-out before pull and pass it to `reset --hard`, please read the question I referred – CharlesB Jan 04 '13 at 10:34
  • What do you mean with "X commits" the amount of commits that exist locally but not remotely? The amount of commits created by you? The question is a bit ambiguous about what you seek. – Maic López Sáenz Jan 10 '13 at 01:55
  • @LopSae the amount of commits that exist locally but not remotely. – Mohammed H Jan 10 '13 at 04:20

5 Answers5

38

The command

git log origin/master..master

shows the commits that are on master but not on origin/master.

tobiasbayer
  • 10,269
  • 4
  • 46
  • 64
3

I made an alias for this command that lists the commits that have not been pushed.

git log --branches --not --remotes --decorate --oneline

which is a variation of a command cxreg posted in Viewing Unpushed Git Commits.

Lots of other useful ways to parse the commit tree in that post as well.

Community
  • 1
  • 1
David Culp
  • 5,354
  • 3
  • 24
  • 32
1

The treeish..treeish notation works exactly to see the commits that are present in the second reference, but not in the first one. From the git log help:

A regular D..M computes the set of commits that are ancestors of M, but excludes the ones that are ancestors of D. This is useful to see what happened to the history leading to M since D, in the sense that "what does M have that did not exist in D".

Using this with either git log or git show you can output a list that contains a single line for each commit pressent in the D..M difference:

git show -s --oneline branch..HEAD

or

git log --oneline branch..HEAD

Pair that with a word count and you can output exactly the number of commits you are looking for:

git log --oneline branch..HEAD | wc -l
Maic López Sáenz
  • 10,385
  • 4
  • 44
  • 57
  • I tried the above commands. I am not getting any output at all. – Mohammed H Jan 11 '13 at 04:24
  • If you are not getting any errors and the `show/log` like the above print nothing, then it means that, assuming `A..B`, `B` already contains all the commits in `A`, and this there are no commits to print. Try inverting it, or try it with a couple commits of which you are certain of the difference. For example, if you try it with `HEAD^..HEAD` you must get a single commit displayed. – Maic López Sáenz Jan 11 '13 at 06:06
  • The above all comments comparing with the local repo only. how to compare with remote repo? – Mohammed H Jan 11 '13 at 06:10
  • Use a reference with the format `remote/branch`, just like `origin/master`. If your local and remote branch are named the same and your remote is `origin`, then you could do `origin/branch..branch`. Following what you describe in the question, you might want `origin/master..HEAD`. – Maic López Sáenz Jan 11 '13 at 08:12
0

This question has be answered already in another post:

git log origin/master..HEAD

(see Viewing Unpushed Git Commits)

Community
  • 1
  • 1
user1834095
  • 5,332
  • 2
  • 20
  • 38
-1

I use this:

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative

It shows commits as a graph, with all the branches and their names.

My advice is to create alias for it in ~/.gitconfig

amorfis
  • 15,390
  • 15
  • 77
  • 125