Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I keep track of where commits came from once they've been merged?

Tags:

git

branch

commit

My company does not maintain the repository with git (we effectively use CVS), but I keep a repo locally for my own sanity. In the past, I've wanted to bring up commits related to, say, bug-report-abcde. I can grep through the commit messages to find bug-report-abcde and browse them. Right now, I have an Emacs function hooked to provide the current branch name as a 'header' in the commit:

Title

Summary summary summary

Branch: bug-report-abcde

Thus my question is: is this the best way to find which branch was the original source of a commit, after the commit has been merged to another branch? Or is there a way to extract this information from git itself?

like image 861
Sean Allred Avatar asked Dec 08 '14 18:12

Sean Allred


People also ask

Does git merge keep history?

In the Conceptual Overview section, we saw how a feature branch can incorporate upstream changes from main using either git merge or git rebase . Merging is a safe option that preserves the entire history of your repository, while rebasing creates a linear history by moving your feature branch onto the tip of main .

Does git merge bring all commits?

Git merging combines sequences of commits into one unified history of commits. There are two main ways Git will merge: Fast Forward and Three way. Git can automatically merge commits unless there are changes that conflict in both commit sequences.

How do I clean up commit history?

If you have been lazily writing multiple vague commits, you can use git reset --soft <old-commit> to make your branch point to that old commit. And as we learned, Git will start by moving the branch pointer to it and stops right there. It won't modify the index or working directory.

Should you keep merge commits?

There is no "best practice" here. The choice is yours. Some people keep a merge commit, even for a fast forward, because it keeps a very good record of when a branch was completed. These people prefer "complete and accurate" history over a "clean" history.


2 Answers

Git doesn't track this information by design.

http://markmail.org/message/yfb5ihwddjmrstz6

So don't think of it as "git throws away branch identity" as much as
"git never cared about branch identity in the first place, and doesn't
think it's relevant."

Your branch name is just that, your branch name. It can be literally anything you want. Or you can not use a branch name, and be in detached head state. Long term, only commit reachability is stored.

If you have information you want to track (like a Defect Number), then you need to include it in the commit message.

If you are using a central server you can setup a receive hook which logs the remote branch name information somehow (git notes for instance). My company uses this. I find it only quasi-useful (mainly if you want to yell at a program manager instead of the guy who wrote or pushed the code).

like image 25
Andrew C Avatar answered Sep 24 '22 05:09

Andrew C


merges-introducing() {
    # merges-introducing $introducing [$descendant]
    local introducing;
    if introducing=`git rev-parse $1`; then 
        shift
        git rev-list --ancestry-path --parents --reverse ^$introducing ${@-HEAD} \
        | awk '{seen[$1]=1} NR>1 && !seen[$2] {print $1}' \
        | xargs -r git show --oneline --no-patch
     fi
}

Finds the merges incorporating a commit from a merged history.

git rev-list's --ancestry-path lists only the commits on the line of descent from the bottom commit (^$introducing here) to the top (default HEAD, your current checkout), --parents gives the parents for each of them, --reverse lists the commits oldest-first (so $introducing comes first), and the post-processing, the awk|xargs, prints only merges whose first parent isn't on that ancestry path.

Unless someone's gone in and hand-edited the merge messages the subject lines for those commits will say the branch name and any source url of the relevant (and non-fastforward) merges, oldest first.

like image 196
jthill Avatar answered Sep 21 '22 05:09

jthill