Git - how to find first commit of specific branch

Git

Git Problem Overview


In following example tree:

A-B-C-D-E (master branch)
    \
     F-G-H (xxx branch)

I'm looking for F - the first commit in xxx branch. I think that it is possible with:

git log xxx --not master

and the last listed commit should be F. Is it correct solution or maybe there are some disadvantages of it?

I know that there were similar questions on stackoverflow, but nobody proposed such solution, and I'm not sure if I do it right.

Git Solutions


Solution 1 - Git

git log master..branch --oneline | tail -1

Where "branch" is your specific branch name. The dot-dot gives you all of the commits that the branch has that master doesn't have. tail -1 returns the last line from the previous output.

Solution 2 - Git

You should use the merge-base functionality which is designed to solve exactly this:

git merge-base remotes/origin/<branch> develop 

Solution 3 - Git

git cherry master -v | head -n 1

cherry - Shows the commits on your current branch that aren't present on a branch upstream (docs). An upstream (master in this case) is a point your current branch derives from.

-v - Shows commit names (instead of just SHA's) - verbose.

head - Unix command which prints n number of lines from a text

Solution 4 - Git

If your branch (old one) once again merged back to master doesn't give expected result.I have using python script to find initial branch commit Id.

git rev-list --first-parent changeset

--first-parent follow only the first parent commit upon seeing a merge commit.

Iterate changeset's from above command until parent branch found.

def status_check(exec_command, exec_dir=None, background=False):
    if exec_dir:
        os.chdir(exec_dir)
    res = subprocess.Popen(exec_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if not background:
        result = res.communicate()
    return result



def findNewBranchCommits(changeset=None):
	cmd = "git rev-list --first-parent "+ changeset
	rev_list = status_check(cmd,self.module_dir)
	rev_list = str(rev_list[0]).split('\n')
	rev_list = list(filter(None, rev_list))
	for x in rev_list:	 					# Iterate until branch base point
		rev_cmd = "git branch --contains " + x
		rev_cmd = status_check(rev_cmd,self.module_dir)
		rev_cmd = str(rev_cmd[0]).split('\n')
		if(len(rev_cmd) > 2): 
			print "First Commit in xxx branch",x
			break

findNewBranchCommits(changeset)

Solution 5 - Git

git cherry master -v | tail -1   

however this will only give you the first commit on branch xxx that is not in master, not the first commit on branch xxx ever. the latter would be difficult if branch xxx has been deleted and/or re-created one or more times. it that case you could try the following:

git reflog | grep checkout | grep xxx | tail -1   

Solution 6 - Git

git rev-list --ancestry-path $(git merge-base master xxx)..xxx | tail -1

Solution 7 - Git

I tried this command and it worked:

git log <source_branch> <feature_branch> --oneline | tail -1

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2699113View Question on Stackoverflow
Solution 1 - GitkonyakView Answer on Stackoverflow
Solution 2 - GitFabrizio StellatoView Answer on Stackoverflow
Solution 3 - GitNeluView Answer on Stackoverflow
Solution 4 - GitNayagamView Answer on Stackoverflow
Solution 5 - GitDoc UnidView Answer on Stackoverflow
Solution 6 - GitwedensView Answer on Stackoverflow
Solution 7 - GitAbderrahmenView Answer on Stackoverflow