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

  • 本文关键字:文件名 修改 分支 获取 Git git
  • 更新时间 :
  • 英文 :


我正在尝试编写一个git钩子,它将对尚未推送的已提交更改进行一些处理,并且我正在努力获得在此特定情况下修改的文件列表:我有一个尚未远程的本地分支。这个分支不是基于master的(例如,假设我有一个特定版本的生产分支)。

如果这个分支是基于master的,那么我可以这样做:

git diff origin/master..HEAD --name-only

如果分支已经在远程,那么这很容易,因为我可以这样做:

git diff origin/<Name of My Branch>..HEAD --name-only

但是,在我的情况下,我不能假设这些。有什么好的方法可以做到这一点?我想做这样的事情,但是我不知道如何在那里轻松地指定本地分支,因为它不是master,它是其他分支。

git diff <Local branch>..HEAD --name-only

最简单的方法是默认将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

最后我想出了这样的东西。(注意:这是在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

最新更新