Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git - changes to branch since created?

Tags:

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 branch?
  2. What are the actually changes (diff) i made to the branch?
  3. What is the log of commits that i made to the branch?

Now i think if figured out how to get the hashes of all the commits that were 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'm not wanting to compare to 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.

like image 574
coding4fun Avatar asked Apr 11 '13 16:04

coding4fun


People also ask

How do I create a branch without losing changes?

The git checkout -b <BranchName> command will create a new branch and switch to it. Moreover, this command will leave the current branch as it is and bring all uncommitted changes to the new branch. There is no local change on the master branch, as we can see in the output.

How do you commit changes to a new branch?

First, checkout to your new branch. Then, add all the files you want to commit to staging. Lastly, commit all the files you just added. You might want to do a git push origin your-new-branch afterwards, so your changes show up on the remote.

Can I create a new branch with current changes?

You can do a checkout and create a new branch with all local and current changes transferred over.


3 Answers

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..

like image 124
jthill Avatar answered Oct 06 '22 21:10

jthill


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:

  • ... double-dot “..” and triple-dot “…” in Git commit ranges?
  • ... double-dot “..” and triple-dot “…” in Git diff commit ranges?
  • Pro Git - Commit Ranges
like image 27
anishpatel Avatar answered Oct 06 '22 20:10

anishpatel


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
like image 40
Tuxdude Avatar answered Oct 06 '22 20:10

Tuxdude