git 커밋이 어떤 브랜치에서 왔는지 찾기
sha1이 주어지면 커밋이 어떤 브랜치에서 오는지 알아내는 방법이 있습니까?
Ruby Grit을 사용하여이 작업을 수행하는 방법을 알려 주시면 보너스 포인트를드립니다.
Dav는 정보가 직접 저장되지 않는다는 것이 맞지만, 그것이 당신이 알 수 없다는 것을 의미하지는 않습니다. 다음은 수행 할 수있는 몇 가지 작업입니다.
커밋이있는 분기 찾기
git branch --contains <commit>
이것은 그들의 역사에서 주어진 커밋을 가진 모든 지점을 알려줄 것입니다. 커밋이 이미 병합 된 경우 분명히 유용하지 않습니다.
리플 로그 검색
커밋이 생성 된 리포지토리에서 작업하는 경우 해당 커밋 행에 대한 reflog를 검색 할 수 있습니다. 90 일이 지난 리플 로그는 git-gc에 의해 정리되므로 커밋이 너무 오래되면 찾을 수 없습니다. 즉, 다음과 같이 할 수 있습니다.
git reflog show --all | grep a871742
커밋 a871742를 찾으려면. 출력은 다음과 같아야합니다.
a871742 refs/heads/completion@{0}: commit (amend): mpc-completion: total rewrite
"completion"분기에서 커밋이 이루어 졌음을 나타냅니다. 기본 출력에는 축약 된 커밋 해시가 표시되므로 전체 해시를 검색하지 마십시오. 그렇지 않으면 아무것도 찾을 수 없습니다.
git reflog show
실제로는의 별칭 git log -g --abbrev-commit --pretty=oneline
일 뿐이 므로 출력 형식을 조작하여 grep에 대해 다른 것을 사용할 수 있도록하려면 시작점입니다!
커밋이 생성 된 리포지토리에서 작업하지 않는 경우이 경우에 할 수있는 최선의 방법은 리플 로그를 검사하고 커밋이 리포지토리에 처음 도입 된시기를 찾는 것입니다. 운 좋게도 해당 지점을 가져 왔습니다. 커밋 트리와 리플 로그를 동시에 걸을 수 없기 때문에 좀 더 복잡합니다. reflog 출력을 구문 분석하여 각 해시에 원하는 커밋이 포함되어 있는지 여부를 확인합니다.
후속 병합 커밋 찾기
이것은 워크 플로에 따라 다르지만 좋은 워크 플로를 사용하면 개발 분기에 커밋이 만들어지고 병합됩니다. 다음과 같이 할 수 있습니다.
git log --merges <commit>..
조상으로 주어진 커밋을 가진 병합 커밋을 확인합니다. (커밋이 한 번만 병합 된 경우 첫 번째 병합은 다음 병합이어야합니다. 그렇지 않으면 몇 가지를 검토해야합니다.) 병합 커밋 메시지에는 병합 된 브랜치 이름이 포함되어야합니다.
이 작업을 수행 할 수 있기를 원한다면 빨리 감기의 경우에도 병합 커밋 생성을 강제 하는 --no-ff
옵션을 사용할 수 있습니다 git merge
. (너무 열심하지 마십시오. 너무 많이 사용하면 난독 화 될 수 있습니다.) 관련 질문에 대한 VonC의 답변 은이 주제에 대해 도움이되는 정보를 제공합니다.
이 간단한 명령은 매력처럼 작동합니다.
git name-rev <SHA>
예를 들어 (여기서 test-branch 는 브랜치 이름) :
git name-rev 651ad3a
251ad3a remotes/origin/test-branch
이것은 다음과 같은 복잡한 시나리오에서도 작동합니다.
origin/branchA/
/branchB
/commit<SHA1>
/commit<SHA2>
여기서 branchB를git name-rev commit<SHA2>
반환합니다 .
2013 년 12 월 업데이트 :
git-what-branch
(Perl 스크립트, 아래 참조) 더 이상 유지되지 않는 것 같습니다.
git-when-merged
저에게 매우 잘 작동하는 Python으로 작성된 대안입니다.
" 특정 커밋을 포함하는 병합 커밋 찾기 "를 기반으로 합니다.
git when-merged [OPTIONS] COMMIT [BRANCH...]
커밋이 하나 이상의 분기로 병합 된시기를 찾습니다. 지정된 BRANCH (들)로
가져온 병합 커밋을 찾습니다COMMIT
.구체적 으로를 조상으로
BRANCH
포함하는 첫 번째 부모 기록에서 가장 오래된 커밋을 찾습니다COMMIT
.
2010 년 9 월 원래 답변 :
Sebastien Douche just twitted (16 minutes before this SO answer):
git-what-branch: Discover what branch a commit is on, or how it got to a named branch
This is a Perl script from Seth Robertson that seems very interesting:
SYNOPSIS
git-what-branch [--allref] [--all] [--topo-order | --date-order ]
[--quiet] [--reference-branch=branchname] [--reference=reference]
<commit-hash/tag>...
OVERVIEW
Tell us (by default) the earliest causal path of commits and merges to cause the requested commit got onto a named branch.
If a commit was made directly on a named branch, that obviously is the earliest path.By earliest causal path, we mean the path which merged into a named branch the earliest, by commit time (unless
--topo-order
is specified).PERFORMANCE
If many branches (e.g. hundreds) contain the commit, the system may take a long time (for a particular commit in the linux tree, it took 8 second to explore a branch, but there were over 200 candidate branches) to track down the path to each commit.
Selection of a particular--reference-branch --reference tag
to examine will be hundreds of times faster (if you have hundreds of candidate branches).EXAMPLES
# git-what-branch --all 1f9c381fa3e0b9b9042e310c69df87eaf9b46ea4
1f9c381fa3e0b9b9042e310c69df87eaf9b46ea4 first merged onto master using the following minimal temporal path:
v2.6.12-rc3-450-g1f9c381 merged up at v2.6.12-rc3-590-gbfd4bda (Thu May 5 08:59:37 2005)
v2.6.12-rc3-590-gbfd4bda merged up at v2.6.12-rc3-461-g84e48b6 (Tue May 3 18:27:24 2005)
v2.6.12-rc3-461-g84e48b6 is on master
v2.6.12-rc3-461-g84e48b6 is on v2.6.12-n
[...]
This program does not take into account the effects of cherry-picking the commit of interest, only merge operations.
For example to find thatc0118fa
commit came from redesign_interactions
* ccfd449 (HEAD -> develop) Require to return undef if no digits found
* 93dd5ff Merge pull request #4 from KES777/clean_api
|\
| * 39d82d1 Fix tc0118faests for debugging debugger internals
| * ed67179 Move &push_frame out of core
| * 2fd84b5 Do not lose info about call point
| * 3ab09a2 Improve debugger output: Show info about emitted events
| * a435005 Merge branch 'redesign_interactions' into clean_api
| |\
| | * a06cc29 Code comments
| | * d5d6266 Remove copy/paste code
| | * c0118fa Allow command to choose how continue interaction
| | * 19cb534 Emit &interact event
You should run:
git log c0118fa..HEAD --ancestry-path --merges
And scroll down to find last merge commit. Which is:
commit a435005445a6752dfe788b8d994e155b3cd9778f
Merge: 0953cac a06cc29
Author: Eugen Konkov
Date: Sat Oct 1 00:54:18 2016 +0300
Merge branch 'redesign_interactions' into clean_api
UPD
Or just one command:
git log c0118fa..HEAD --ancestry-path --merges --oneline --color | tail -n 1
git branch --contains <ref>
is the most obvious "porcelain" command to do this. If you want to do something similar with only "plumbing" commands:
COMMIT=$(git rev-parse <ref>) # expands hash if needed
for BRANCH in $(git for-each-ref --format "%(refname)" refs/heads); do
if $(git rev-list $BRANCH | fgrep -q $COMMIT); then
echo $BRANCH
fi
done
(crosspost from this SO answer)
Poor man's option is to use the tool tig
1 on HEAD
, search for the commit, and then visually follow the line from that commit back up until a merge commit is seen. The default merge message should specify what branch is getting merged to where :)
1 Tig is an ncurses-based text-mode interface for git. It functions mainly as a Git repository browser, but can also assist in staging changes for commit at chunk level and act as a pager for output from various Git commands.
As an experiment, I made a post-commit hook that stores information about the currently checked out branch in the commit metadata. I also slightly modified gitk to show that information.
You can check it out here: https://github.com/pajp/branch-info-commits
I deal with the same problem (jenkins multibranch pipeline) - having only commit information and trying to find a branch name where this commit originally came from. It must work for remote branches, no local copies are available.
This is what I work with:
git rev-parse HEAD | xargs git name-rev
Optionally you can strip the output:
git rev-parse HEAD | xargs git name-rev | cut -d' ' -f2 | sed 's/remotes\/origin\///g'
If the OP is trying to determine the history that was traversed by a branch when a particular commit was created ("find out what branch a commit comes from given its sha1"), then without the reflog there's no records in the git object database that shows what named branch was bound to what commit history.
(I posted this as an answer in reply to a comment)
Hopefully this script illustrates my point:
rm -rf /tmp/r1 /tmp/r2; mkdir /tmp/r1; cd /tmp/r1
git init; git config user.name n; git config user.email e@x.io
git commit -m"empty" --allow-empty; git branch -m b1; git branch b2
git checkout b1; touch f1; git add f1; git commit -m"Add f1"
git checkout b2; touch f2; git add f2; git commit -m"Add f2"
git merge -m"merge branches" b1; git checkout b1; git merge b2
git clone /tmp/r1 /tmp/r2; cd /tmp/r2; git fetch origin b2:b2
set -x;
cd /tmp/r1; git log --oneline --graph --decorate; git reflog b1; git reflog b2;
cd /tmp/r2; git log --oneline --graph --decorate; git reflog b1; git reflog b2;
The output shows the lack of any way to know whether the commit with 'Add f1' came from branch b1 or b2 from the remote clone /tmp/r2
(last lines of the output here)
+ cd /tmp/r1
+ git log --oneline --graph --decorate
* f0c707d (HEAD, b2, b1) merge branches
|\
| * 086c9ce Add f1
* | 80c10e5 Add f2
|/
* 18feb84 empty
+ git reflog b1
f0c707d b1@{0}: merge b2: Fast-forward
086c9ce b1@{1}: commit: Add f1
18feb84 b1@{2}: Branch: renamed refs/heads/master to refs/heads/b1
18feb84 b1@{3}: commit (initial): empty
+ git reflog b2
f0c707d b2@{0}: merge b1: Merge made by the 'recursive' strategy.
80c10e5 b2@{1}: commit: Add f2
18feb84 b2@{2}: branch: Created from b1
+ cd /tmp/r2
+ git log --oneline --graph --decorate
* f0c707d (HEAD, origin/b2, origin/b1, origin/HEAD, b2, b1) merge branches
|\
| * 086c9ce Add f1
* | 80c10e5 Add f2
|/
* 18feb84 empty
+ git reflog b1
f0c707d b1@{0}: clone: from /tmp/r1
+ git reflog b2
f0c707d b2@{0}: fetch origin b2:b2: storing head
TL;DR:
Use the below if you care about shell exit statuses:
branch-current
- the current branch's namebranch-names
- clean branch names (one per line)branch-name
- Ensure that only one branch is returned frombranch-names
Both branch-name
and branch-names
accept a commit as argument, and default to HEAD
if none given.
Aliases useful in scripting
branch-current = "symbolic-ref --short HEAD" # https://stackoverflow.com/a/19585361/5353461
branch-names = !"[ -z \"$1\" ] && git branch-current 2>/dev/null || git branch --format='%(refname:short)' --contains \"${1:-HEAD}\" #" # https://stackoverflow.com/a/19585361/5353461
branch-name = !"br=$(git branch-names \"$1\") && case \"$br\" in *$'\\n'*) printf \"Multiple branches:\\n%s\" \"$br\">&2; exit 1;; esac; echo \"$br\" #"
Commit only reachable from only one branch
% git branch-name eae13ea
master
% echo $?
0
- Output is to STDOUT
- Exit value is
0
.
Commit reachable from multiple branches
% git branch-name 4bc6188
Multiple branches:
attempt-extract
master%
% echo $?
1
- Output is to STDERR
- Exit value is
1
.
Because of the exit status, these can be safely built upon. Eg to get the remote used for fetching:
remote-fetch = !"branch=$(git branch-name \"$1\") && git config branch.\"$branch\".remote || echo origin #"
I think someone should face the same problem that can't find out the branch, although it actually exists in one branch.
You'd better pull all first:
git pull --all
Then do the branch search:
git name-rev <SHA>
or:
git branch --contains <SHA>
@khichar.anil covered most of this in his answer.
Just adding the flag to remove the tags gives us:
git name-rev --name-only --exclude=tags/* $SHA
To find local branch
grep -lR YOUR_COMMIT .git/refs/heads | sed 's/.git\/refs\/heads\///g'
To find remote branch
grep -lR $commit .git/refs/remotes | sed 's/.git\/refs\/remotes\///g'
Aside from searching through all of the tree until you find a matching hash, no.
참고URL : https://stackoverflow.com/questions/2706797/finding-what-branch-a-git-commit-came-from
'developer tip' 카테고리의 다른 글
HTML5에 minlength 유효성 검사 속성이 있습니까? (0) | 2020.10.03 |
---|---|
Argparse 선택적 위치 인수? (0) | 2020.10.03 |
localStorage에 어레이를 어떻게 저장합니까? (0) | 2020.10.03 |
PHP에서 문자열을 숫자로 어떻게 변환합니까? (0) | 2020.10.03 |
jquery를 사용하여 확인란을 선택 / 선택 취소 하시겠습니까? (0) | 2020.10.03 |