0

Is it possible to get the branch name from where a particular file is pushed into remote master branch?

Accidentally I am in such condition where I am supposed to identify from which branch an unexpected file is pushed into master.

Since there are thousands of commits it won't be possible to make an exhaustive look up through the commits.

Any help is much appreciated.

Anonymous One
  • 411
  • 2
  • 6
  • 16

2 Answers2

0

Because branches are really just pointers to particular commits in the repo, git doesn't really keep track of branches like that.

That being said, it would be possible to find the commit that caused the problem and very likely the branch that was pushed (though this will depend on your workflow).

First use git log -1 -- <file>, this will show the last commit involving the file. From your question, there should really be only one.

Knowing this all we need to do is find the branches that contain this particular commit. From this answer the command we want is git branch -a --contains <commit> This list should contain the remote master and the culprit branch that you are looking for. The -a will add all the remote branches that your repo is aware of.

Depending on how much merging happened, this list should be limited to master and the branch that you are looking for. Otherwise it will contain all the branches that were also merged with the branch that you are looking for.

If that is the case, you can examine the history to find the merge commit. git log --graph will 'show' the branches and merges that occur. Enter this command and then type / <sha of file commit> which should take you to the commit that created the file. Then trace the line for the commit forward until you get the merge commit which should contain the name of the branch. This method can be messy depending on what you history looks like.

Community
  • 1
  • 1
Schleis
  • 41,516
  • 7
  • 68
  • 87
  • I've gone through the steps you mentioned. Got stuck to run this command `/ `. – Anonymous One Jun 08 '16 at 03:48
  • You are going to search the log for the sha of the commit and then trace it forward in history until the branch is merged into master. The merge commit should contain the name of the branch that you are looking for. – Schleis Jun 08 '16 at 12:15
0

To find which user added it, which might give a clue to the branch.

 git blame FILENAME | head

to find the commit where it was added and it's SHA1 value

git log --oneline --decorate  --diff-filter=A -- FILENAME

to find the branch which added it (if ref log goes back that far!!)

git ref-log | grep SHA1VALUE
Gregg
  • 2,444
  • 1
  • 12
  • 21