coding4fun
coding4fun

Reputation: 8199

git - changes to branch since created?

I'm always asking myself a set of questions about branches I've created that I've yet to determine how to accomplish:

  1. What files did I change in a branch?
  2. What are the actual changes (diff) I made to the branch?
  3. What is the log of commits I made to the branch?

Now I think I figured out how to get the hashes of all the commits committed to the branch but not merged into master using git cherry master <branchname>. With this, I could go hash by hash and figure out everything but if you have a lot of commits this could be time-consuming.

Notice I don't want to compare to the current state of master. I think the key is knowing the hash of master that you created your branch off of but I'm not exactly sure how to determine this.

Upvotes: 27

Views: 16173

Answers (4)

Brambor
Brambor

Reputation: 664

Since I want to use it often, I made @jthill's answer into an alias.

Add the alias with

git config --global alias.branch-diff "!f(){ git diff $(git merge-base $1 $2) $2 "${@:3}"; }; f"

With syntax git branch-diff <arg1> <arg2> <more args>. The 1st argument represents the master branch, which the branch in the 2nd argument split off at some point. Further arguments are passed to the diff.

Usage:

  1. What files did I change in a branch?
git branch-diff master other-branch --name-only

The alias expands this into this and executes it:

git diff $(git merge-base master other-branch) other-branch --name-only
  1. What are the actual changes (diff) I made to the branch?
git branch-diff master other-branch

The alias expands this into this and executes it:

git diff $(git merge-base master other-branch) other-branch

Bonus:

# get all file names changed
git branch-diff master other-branch --name-only
# get changes of my_file.cpp
git branch-diff master other-branch -- my_file.cpp

Upvotes: 0

anishpatel
anishpatel

Reputation: 1522

Assuming you branched from master,

1. What files have changed since branching?

git diff master...<branchname> --name-only

2. What is the full diff since branching?

git diff master...<branchname>

3. What is the commit log since branching?

git log master..<branchname>

You do not need to specify <branchname> if you have it checked out (e.g., git diff master...).

Note: git diff requires triple dot ... and git log requires double dot .. to get the behavior that you are asking for.


For explanations on dot syntax, see:

Upvotes: 18

jthill
jthill

Reputation: 60393

To find where your current checkout diverged from the master branch,

base=`git merge-base master HEAD`
echo $base

to find out what files have been changed since then,

git diff --name-only $base HEAD

to show the accumulated difference

git diff $base HEAD

When you want to describe an entire series of commits you can use the syntax in the answer Gabriele Petronella linked above, master..HEAD is shorthand for HEAD ^master which means "all commits reachable from HEAD, but not including commits reachable from master". Missing endpoints default to HEAD, so you can say git log --oneline master..

Upvotes: 36

Tuxdude
Tuxdude

Reputation: 49543

For the list of files changed and the actual diff, it makes more sense if you know 2 commits between which you wanna compare.

If you want to see the diff between START_SHA1 and END_SHA1, you could do:

git diff START_SHA1 END_SHA1

If you want just the list of files, you could do:

git diff --name-only START_SHA1 END_SHA1

If you also want to know what type of change went into the file (like A, M, D, C, R, U), you could do:

git diff --name-status START_SHA1 END_SHA1

Upvotes: 2

Related Questions