簡體   English   中英

Git - 如何找到特定分支的第一次提交

[英]Git - how to find first commit of specific branch

在以下示例樹中:

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

我正在尋找 F - xxx 分支中的第一個提交。 我認為有可能:

git log xxx --not master

最后列出的提交應該是 F。它是正確的解決方案還是有一些缺點?

我知道在stackoverflow上有類似的問題,但是沒有人提出這樣的解決方案,我不確定我是否做對了。

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

其中“分支”是您的特定分支名稱。 點-點為您提供了該分支具有而 master 沒有的所有提交。 tail -1返回前一個輸出的最后一行。

您應該使用旨在解決此問題的merge-base功能:

git merge-base remotes/origin/<branch> develop 
git cherry master -v | head -n 1

cherry - 顯示當前分支上不存在於上游分支 ( docs ) 上的提交。 上游(在本例中為 master)是您當前分支的派生點。

-v - 顯示提交名稱(而不僅僅是 SHA) - 詳細。

head - 從文本打印n行的 Unix 命令

如果您的分支(舊分支)再次合並回 master 並沒有給出預期的結果。我使用 python 腳本來查找初始分支提交 ID。

git rev-list --first-parent 變更集

--first-parent在看到合並提交時只關注第一個父提交。

從上面的命令迭代變更集,直到找到父分支。

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)

我試過這個命令,它起作用了:

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

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

git cherry master -v | tail -1   

然而,這只會給你不在 master 的分支 xxx 上的第一次提交,而不是分支 xxx 上的第一次提交。 如果分支 xxx 已被刪除和/或重新創建一次或多次,后者將很困難。 在這種情況下,您可以嘗試以下操作:

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

非常簡單:您可以使用它直接獲取功能分支中第一次提交的哈希值:

git log <source_branch>..<feature_branch> --pretty=format:%h

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM