ItGo.me Focus on IT Recommend

Home > Using Git, show all commits that are in one branch, but not the other(s)

Using Git, show all commits that are in one branch, but not the other(s)

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
入口地址https://www.aliyun.com/minisite/goods

I have an old branch, which I would like to delete. However, before doing so, I want to check that all commits made to this branch were at some point merged into some other branch. Thus, I'd like to see all commits made to my current branch which have not been applied to any other branch [or, if this is not possible without some scripting, how does one see all commits in one branch which have not been applied to another given branch?].

git branch git-branch
|
  this question
edited Apr 22 '16 at 10:40 Alex 10.7k 7 37 58 asked Nov 10 '09 at 20:17 sircolinton 1,697 3 10 11      Just use git branch -d - that will delete a branch if it has been merged. –  Zaz Feb 10 at 0:33

 | 

9 Answers
9

---Accepted---Accepted---Accepted---

You probably just want

git branch --contains branch-to-delete

This will list all branches which contain the commits from "branch-to-delete". If it reports more than just "branch-to-delete", the branch has been merged.

Your alternatives are really just rev-list syntax things. e.g. git log one-branch..another-branch shows everything that one-branch needs to have everything another-branch has.

You may also be interested in git show-branch as a way to see what's where.


|
  this answer
edited Sep 7 '12 at 14:12 sleske 47.7k 21 115 159 answered Nov 10 '09 at 20:26 Dustin 55k 14 92 116 2   +1. See also stackoverflow.com/questions/850607/… –  VonC Nov 10 '09 at 20:30 1   The line 'If it reports something, the branch has merged' can be misinterpreted: if git branch --contains some-branch only returns some-branch, then it does return something, but it has not been merged. –  Confusion Mar 21 '12 at 16:26      @Confusion: True. I've taken the liberty to edit this into the answer. –  sleske Sep 7 '12 at 14:12 1   git show-branch is very informative –  Tony Lang May 20 '13 at 13:27 2   Note that git log foo..bar will show the commits between bar's latest and foo's latest, but not other commits missing from further back in time. To see everything in bar but not in foo, you should use @jimmyorr's solution. –  Paul A Jungwirth Jul 10 '14 at 16:34  |  show more comment

To see a list of which commits are on one branch but not another, use git log:

git log oldbranch ^newbranch --no-merges

...that is, show commit logs for all commits on oldbranch that are not on newbranch. You can list multiple branches to include and exclude, e.g.

git log oldbranch1 oldbranch2 ^newbranch1 ^newbranch2 --no-merges

Note: on Windows ^ is an escape key, so it needs to be escaped with another ^:

git log oldbranch ^^newbranch --no-merges

|
  this answer
edited Jun 18 '15 at 13:13 Nick Volynkin 4,425 1 19 40 answered Nov 17 '10 at 17:15 jimmyorr 5,789 3 23 20 2   I found it looking for git compare commits of two branches. –  User May 6 '13 at 8:53 13   This is exactly what I was looking for. But, using ^ as a prefix here confused me. In this context it means exclude that branch. Using ^ as a suffix would be a relative reference to the parent commit of that branch. –  joeshmo Jul 18 '13 at 16:38 1   very useful thanks. I'm curious, why is the --no-merges flag necessary? Surely one wants to see those commits also? –  Max MacLeod Aug 1 '13 at 8:17 2   Would like to use gitk with this? Simply use gitk oldbranch ^newbranch --no-merges (Tested with git 1.8.1.1). Side note, for me ^ means inclusive HEAD commit of branch newbranch. –  Matt Dec 24 '13 at 16:53 2   @NazariiGudzovatyi - yes, there is: "-cherry-pick". There is a HUGE number of options for log on the documentation page –  romeara May 22 '15 at 15:30  |  show more comments

While some of the answers posted here will help find what you seek, the following sub-command of git branch is a more suitable solution for your task.

--merged is used to find all branches which can be safely deleted, since those branches are fully contained by HEAD.

While in master one could run the command to enumerate the branches one could safely remove, like so:

git branch --merged
  develop
  fpg_download_links
* master
  master_merge_static

# Delete local and remote tracking branches you don't want
git branch -d fpg_download_links
git push origin :fpg_download_links
git branch -d master_merge_static
git push origin :master_merge_static

# There is also a flag to specify remote branches in the output
git branch --remotes --merged

|
  this answer
edited Apr 14 '16 at 15:41 answered Jan 5 '11 at 7:53 Freddie 864 8 10 1   A very useful command for cleanup. –  Kzqai Jan 28 '13 at 21:31      Very useful command indeed. The ---Accepted---Accepted---Accepted--- answer is more pertinent to the question, but I'll definitely use in the future what you
 d, so thanks –  Cec Mar 21 '16 at 10:14

 | 

To show the commits in oldbranch but not in newbranch:

git log newbranch..oldbranch

To show the diff by these commits (note there are three dots):

git diff newbranch...oldbranch

Here is the doc with a diagram illustration https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#Commit-Ranges


|
  this answer
edited Oct 5 '16 at 11:35 answered Feb 21 '14 at 12:25 Xuan 2,753 23 25      See Paul A Jungwirth's comment above. It seems this will miss out on some old commits? –  Miserable Variable Jan 9 '15 at 5:22 1   I'm not sure what old commits mean. The double dots basically asks Git to resolve a range of commits that are reachable from one commit but aren’t reachable from another. Here is the doc with a digram illustration git-scm.com/book/en/v2/… –  Xuan Jan 9 '15 at 9:22

 | 

For those still looking for a simple answer, check out git cherry. It compares actual diffs instead of commit hashes. That means it accommodates commits that have been cherry picked or rebased.

First checkout the branch you want to delete:

git checkout [branch-to-delete]

then use git cherry to compare it to your main development branch:

Recommend:git - Revert commits unique to a branch

o the wrong branch! I merged to dev-majorversion when I meant to merge to dev-majorversion.minorversion. Is there any way I can undo the commits that exist on majorversion but do not exist on minorversion for branch to undo these commits, o

git cherry -v master

Example output:

+ 8a14709d08c99c36e907e47f9c4dacebeff46ecb Commit message
+ b30ccc3fb38d3d64c5fef079a761c7e0a5c7da81 Another commit message
- 85867e38712de930864c5edb7856342e1358b2a0 Yet another message

Note: The -v flag is to include the commit message along with the SHA hash.

Lines with the '+' in front are in the branch-to-delete, but not the master branch. Those with a '-' in front have an equivalent commit in master.

For JUST the commits that aren't in master, combine cherry pick with grep:

git cherry -v master | grep "^\+"

Example output:

+ 8a14709d08c99c36e907e47f9c4dacebeff46ecb Commit message
+ b30ccc3fb38d3d64c5fef079a761c7e0a5c7da81 Another commit message

|
  this answer
answered Oct 14 '15 at 23:38 Tim S 3,066 1 21 23      I've tried this, but it still reports that there are many commits in the fb (feature branch) that aren't in the mb (main branch). However, if I'm in the fb and do a git diff mb, I see no differences. I did use rebase and squashed everything. I'm nearly certain this is why, but I just want to be be certain. If this is the case, then I'm going to avoid squashing if at all possible; I'm in the "no lost info camp." I wonder if it would be possible to add a log display mode that can display merges as if they were rebases to keep the history clean and yet lose no info. –  Outis Von Nemo Jun 29 '16 at 14:57      Not sure of your exact scenario here, but if you've squashed multiple commits together into one and are comparing that to another branch where the commits are separate, this definitely won't work. In that case, you may just want to use the unix diff utility to compare the various files. Or, you could create a temporary branch and squash all the commits in that similar to what you did with the original branch, and then use this, which I think would work. –  Tim S Jul 1 '16 at 0:04

 | 

jimmyorr's answer does not work on Windows. it helps to use --not instead of ^ like so:

git log oldbranch --not newbranch --no-merges

|
  this answer
edited Jun 18 '15 at 13:35 Nick Volynkin 4,425 1 19 40 answered Jun 18 '15 at 8:59 sebeck 140 1 4 3   That is correct, +1. Note though that ^ is supported on Windows, but needs to be escaped, which, in Windows, is (another) ^: git log oldbranch ^^newbranch --no-merges. –  VonC Jun 18 '15 at 9:42      @VonC fixed that answer with your comment. stackoverflow.com/a/4207176/2790048 –  Nick Volynkin Jun 18 '15 at 13:14      @NickVolynkin Well done! –  VonC Jun 18 '15 at 13:32 1   @sebeck first action on stackoverflow being a good and new answer to a popular queston. That's well done ) –  Nick Volynkin Jun 18 '15 at 16:26 3   To be specific, it does work in Windows in Powershell console, but require extra "^" in CMD. –  Rod Sep 7 '15 at 5:39

 | 

If it is one (single) branch that you need to check, for example if you want that branch 'B' is fully merged into branch 'A', you can simply do the following:

$ git checkout A
$ git branch -d B

git branch -d <branchname> has the safety that "The branch must be fully merged in HEAD."

Note that this actually delete the branch if it is merged in,


|
  this answer
edited Feb 26 '16 at 21:20 answered Nov 10 '09 at 22:00 Jakub Narębski 160k 44 181 220 3   Caution: git branch -d <branchname> actually deletes the branch. I just did it! –  Haywire Feb 26 '16 at 7:29

 | 

You can use this simple script to see commits that are not merged

#!/bin/bash
# Show commits that exists only on branch and not in current
# Usage:
#   git branch-notmerge <branchname>
#
# Setup git alias
#   git config alias.branch-notmerge [path/to/this/script]
grep -Fvf <(git log --pretty=format:'%H - %s') <(git log $1 --pretty=format:'%H - %s')

You can use also tool git-wtf that will display state of branches


|
  this answer
edited May 31 '12 at 15:29 answered May 30 '12 at 13:22 manRo 1,152 9 14

 | 

To list missing commits between two branches you may use compare-branches.py

https://bitbucket.org/aakef/compare-git-branches


|
  this answer
answered Mar 19 '14 at 17:03 Bernd Schubert 1

 | 

Recommend:git - Show all commits in one branch that aren't from merges

nged and aren't the same as the corresponding commits on the master branch (I think this is because at some point there were some merge conflicts). So, I have a few questions. Is there still a way to show only commits not from merges git l

------splitte line----------------------------