Git -获取未推送分支中未推送更改的已修改文件的名称

brqmpdu1  于 2023-04-10  发布在  Git
关注(0)|答案(2)|浏览(213)

我正在尝试编写一个git hook,它将对尚未推送的已提交更改进行一些处理,并且在这种特定情况下,我很难获得已修改文件的列表:我有一个本地分支,还没有在远程。这个分支不是基于主(例如,想象我有一个特定版本的生产分支)。
如果这个分支是基于master的,那么这很容易,因为我可以这样做:
git diff origin/master..HEAD --name-only
如果分支已经在remote中,那么这很容易,因为我可以这样做:
git diff origin/<Name of My Branch>..HEAD --name-only
但是,在我的例子中,我不能假设任何一个。有什么好的方法可以做到这一点?我想做这样的事情,但我不知道如何轻松地指定本地分支,因为它不是主分支,而是其他分支。
git diff <Local branch>..HEAD --name-only

djmepvbi

djmepvbi1#

最简单的方法是默认使用master作为引用:

# if your current branch has no registered remote:
git diff --name-only origin/master..<commit>

根据我的经验,这往往是绰绰有余的。
如果你真的想更精确地知道应该推送什么,你可以尝试一些更有趣的方法来确定什么是“离你的远程仓库最近的提交”,并与之进行比较:

# step by step:
# the following command will print the name of all known remote branches:
git branch -r --format="%(refname:short)"

# the following command will print a list of shas for commits in your branch
# and not yet on the remote:
git rev-list HEAD --not $(git branch -r --format="%(refname:short)")

# get the last of these commits:
last=$(git rev-list HEAD --not $(git branch -r --format="%(refname:short)") | tail -1)

# and take its parent:
git diff --name-only ${last}^..HEAD
  • [更新]*

你是对的,正如你在回答中提到的,在git log/git rev-list中有快捷方式可以更直接地列出远程:

last=$(git rev-list HEAD --not --remotes | tail -1)
git diff --name-only ${last}^..HEAD
2mbi3lxu

2mbi3lxu2#

我在最后想出了这样的东西。(注:这是在PowerShell中。

# Use git log to find out the list of commits that are not in remote yet. Then, pick the oldest commit.
$oldestCommitId = git log --branches --not --remotes --format=format:%H | Select-Object -Last 1

# Do a diff from one before that commit (using ^) until the current HEAD.
$files = git diff "$oldestCommitId^..HEAD" --diff-filter=d --name-only

相关问题