最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I ask Git for the difference between my current working tree and the commit from which I based my current branch? - Stac

programmeradmin3浏览0评论

In my workflow I create a feature branch from a particular release branch, make a few commits, and eventually make a PR to merge my feature branch back to the release branch. This seems pretty standard.

           A - B - C (HEAD; branch "feature") ...plus local changes
          /
   ... - V - W - X - Y - Z (branch "release_a")
  /
 ? ... - ... (branch "main")

I often want to see the all the changes I've made for the feature I'm working on, which is the difference between my current working tree (the HEAD of branch "feature" plus any uncommitted changes) and the commit from which I branched ("V", which is on branch "release_a" but likely NOT the head of "release_a").

Generally I use a git log command to find the SHA of commit "V" and then use git diff <SHA of V> to see all changes I've made. I'd love to build this into a single command, but have not found a way to do it.

Note that...

  1. I want to include local, uncommitted changes
  2. I want to automatically find the commit from which I branched ("V")
  3. there may not be ANY branch currently pointed at "V" (all branches have moved on)
  4. I did not branch from "main" or "master" or any specially-named branch
  5. I don't want to have to mention "release_a" in my command (it should figure it out, if necessary)
  6. I'm currently using git version 2.48.1

Things I've tried:

  • git merge-base --fork-point HEAD returns the SHA of commit "A" (the first commit on my branch). [CORRECTION: This does NOT do this!]
  • git merge-base --fork-point release_a returns the SHA of commit "V". This is the one I want, but I have to use the name of the branch from which I branched in order to get it.
  • git diff <SHA of "V"> does what I want, but I have to first look up the SHA of "V" with a separate command.

Logically, I think I want to:

  • traverse backward from HEAD to the first commit that has two children (This would work as long as I don't have another branch off my "feature" branch... which I generally don't)
  • show the diff between that commit and my current working tree

Hopefully there's a simple (or even not-so-simple) git command that I can bake into a single git alias and continue on my way!


Update:

It turns out this is wrong. It doesn't do this.

git merge-base --fork-point HEAD returns the SHA of commit "A" (the first commit on my branch).

In my workflow I create a feature branch from a particular release branch, make a few commits, and eventually make a PR to merge my feature branch back to the release branch. This seems pretty standard.

           A - B - C (HEAD; branch "feature") ...plus local changes
          /
   ... - V - W - X - Y - Z (branch "release_a")
  /
 ? ... - ... (branch "main")

I often want to see the all the changes I've made for the feature I'm working on, which is the difference between my current working tree (the HEAD of branch "feature" plus any uncommitted changes) and the commit from which I branched ("V", which is on branch "release_a" but likely NOT the head of "release_a").

Generally I use a git log command to find the SHA of commit "V" and then use git diff <SHA of V> to see all changes I've made. I'd love to build this into a single command, but have not found a way to do it.

Note that...

  1. I want to include local, uncommitted changes
  2. I want to automatically find the commit from which I branched ("V")
  3. there may not be ANY branch currently pointed at "V" (all branches have moved on)
  4. I did not branch from "main" or "master" or any specially-named branch
  5. I don't want to have to mention "release_a" in my command (it should figure it out, if necessary)
  6. I'm currently using git version 2.48.1

Things I've tried:

  • git merge-base --fork-point HEAD returns the SHA of commit "A" (the first commit on my branch). [CORRECTION: This does NOT do this!]
  • git merge-base --fork-point release_a returns the SHA of commit "V". This is the one I want, but I have to use the name of the branch from which I branched in order to get it.
  • git diff <SHA of "V"> does what I want, but I have to first look up the SHA of "V" with a separate command.

Logically, I think I want to:

  • traverse backward from HEAD to the first commit that has two children (This would work as long as I don't have another branch off my "feature" branch... which I generally don't)
  • show the diff between that commit and my current working tree

Hopefully there's a simple (or even not-so-simple) git command that I can bake into a single git alias and continue on my way!


Update:

It turns out this is wrong. It doesn't do this.

git merge-base --fork-point HEAD returns the SHA of commit "A" (the first commit on my branch).

Share Improve this question edited Mar 6 at 20:05 aldo asked Mar 6 at 18:39 aldoaldo 2,98524 silver badges36 bronze badges 1
  • if git merge-base --fork-point HEAD returns the SHA of commit "A" (the first commit on my branch), then $(git merge-base --fork-point HEAD)~ with that trailing ~ produces the sha of its parent, commit V. – jthill Commented Mar 6 at 18:47
Add a comment  | 

3 Answers 3

Reset to default 2

I think it would be :

git diff @{u}... # triple dots

That is against the upstream..... Which is not exactly the branch that you forked from.... If you want that specifically, you need to provide it, given that it's not something git saves somewhere

git diff <branch-I-forked-from>... # triple dots, again

You'll need to use the ~ symbol which refers to previous commit.

git config --global alias.fdiff '!git diff $(git merge-base --fork-point HEAD)~'
git fdiff

Until anyone has a better answer, I've decided to write a script that takes the name of the "reference branch" as an argument ("release_a" in the question).

I'm using git merge-base --fork-point $referenceBranch to get the SHA of the "base commit" ("V" in the question).

Then I'm using git log HEAD...$baseCommit~ to log all commits on the current branch back to - and including - the base commit (including it just for reference).

Finally, git difftool $baseCommit to display the diff between my current work tree and the "base commit" in my difftool.

"git_diff_branch.csh"

#!/bin/csh

set currentBranch=`git branch --show-current`
set referenceBranch=$1
set logCommand="log --graph --name-status"
set diffCommand="difftool"
set baseCommit=`git merge-base --fork-point $referenceBranch`

echo ""
echo "================================================================================"
echo "Current branch:   $currentBranch"
echo "Reference branch: $referenceBranch"
echo "Base commit:      $baseCommit"
echo "================================================================================"

echo ""
echo "Current status..."
echo ""
git status

echo ""
echo "--------------------------------------------------------------------------------"
echo ""
echo "Commits on this branch..."
echo ""
# Log back to (and including) the base commit (just to confirm where we branched from)
git $logCommand HEAD...$baseCommit~

echo ""
echo "--------------------------------------------------------------------------------"
echo ""
echo "Displaying diff..."
echo ""
git $diffCommand $baseCommit

(It could use some error checking... but it works for now!)

Since there aren't many release branches and they don't change often, I defined some aliases to call this script with each of those branch names as the argument.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论