I am trying to use git log to find when a particular file was brought into master.
The (simplified) version history looks like this:
* f77cac3 Merge branch 'branch2'
|\
| * da35250 Merge branch 'branch1' into branch2
| |\
| | * 90ab3e3 adding foo on branch1
| * | 42a0367 adding bar in branch2
| |/
* | 853b691 changes to baz on master
* | c7b6c72 baz file on master
|/
* e195580 first commit
I would like to find which commit introduced the file 'foo' into master (should be f77cac3). However, running git log --foo gives 90ab3e3. In more complex cases, where multiple changes were made to file foo on branch1 and branch2, all those commits will show up, but not the merge commit. I've read through the git help, as well as googled and searched on Stack Overflow, but I can't find a way to do this.
Here are the commands you can use to recreate the below scenario:
mkdir testgit
cd testgit
git init
vim README
git add README
git commit
git checkout -b branch1
vim foo
git add foo
git commit
git checkout master
git checkout -b branch2
vim bar
git add bar
git commit
git checkout master
vim baz
git add baz
git commit
vim baz
git add baz
git commit
git checkout branch2
git merge --no-ff branch1
git checkout master
git merge --no-ff branch2
I am truly stumped at this point, would really appreciate any pointers.
The best you can do here is to usegit blameorgit bisectto find the first commit that contains the change, and then follow the chain of child commits to find the first merge commit.Git doesn't really have the concept of certain commits belonging to branches. Yes, commits are contained in branches, and may or may not be contained in ONLY one branch. What you are trying to do would require recording which branch every commit was in. This is something that Mercurial does, but not Git.
Reading into your question a little bit, you may not need to do this in an automated fashion. In that case, you can just look at the list of commits that are provided by
git log --sparse -- fooThe difference is that
--sparsewill list all parents walked, and it will walk parents who have the EXACT same contents first (in this case, the right side of the merges).