Git: Parse branch name from console output?

試著忘記壹切 提交于 2021-01-29 12:30:28

问题


I hope to reference a branch instead of typing its name. I anticipate 'reference by number' is the most likely route.

But I can't figure out how to parse the branch name from the branch command output in the Windows CMD shell.

Background: In my Git for Windows workflow, I frequently need to checkout or merge the branches of several others. I know autocomplete is possible, but it only works in some shells, and is still slower than the job could be. I would like to type these types of things, where $ is a special reference, not an absolute branch name:

git checkout $1       #BIGGEST WISH#   # check out the most recent check-in
git merge $moderation                  # merge a frequently used "origin/team_moderation"
git merge $latest                      # merge the latest branch committed

So far, I am able to get a list of branches that are sorted by commit date:

cmd_prompt> git branch -r --sort=-committerdate --no-color
.gitconfig> br = !"f() { git fetch -p  &&  git branch -r --sort=-committerdate $@; }; f"  # shows remote branches, -a would show all

But my efforts to extract branch names all hit dead-ends.

  1. I tried "for /F": Parse the branch list, skipping to line 2, grab 2nd tokenset after this token: >
cmd_prompt> @for /f "skip=2 tokens=2 delims=>" %a in ('git branch -r --sort=-committerdate --no-color') do @echo %a
cmd_output> origin/master

As shown, that command works in the CMD window. But I can't get it to work in my .gitconfig file:

.gitconfig> checkoutnumber = !"f() { 'for /f "skip=2 tokens=2 delims=>" %%a in ('git branch -r --sort=-committerdate --no-color') do @echo %%a'|git checkout ; }; f"

cmd_prompt> git checkoutnumber 2
cmd_output> environment: for /f skip=2 tokens=2 delims=> %%a in (git: No such file or directory

I tried for.. |git checkout and ` vs. ' and \"... can't make it happy or see what it is seeing.

...

  1. I tried findstr: print the list, add line numbers to the front, and find the line number. Maybe strip the line number afterwards, if I can get it working...
cmd_prompt> git branch -r --sort=-committerdate --no-color|findstr /n ^^|findstr "^[2]:"
cmd_output> 2:  origin/master

As you can see, this sort of works in the command line to return the 2nd line (though it has an line number in the front). But I can't seem to massage the special characters enough to please .gitconfig:

.gitconfig> checkoutnumber = !"f() { git branch -r --sort=-committerdate --no-color|findstr /n |findstr \"^[$1]:\"|git checkout   &&  git st| xargs echo $1; }; f"

cmd_prompt> checkoutnumber 2
cmd_output> FINDSTR: Cannot open ^^
cmd_output> Your branch is up to date...

...

  1. Other options: I considered git-bash, or making batch scripts, or using other languages like Python/Perl/etc. These would be valuable too, but would be my second choice... my preference would be "minimal setup". I am using git version 2.19.1.windows.1 in the Windows 10 CMD window, which shell I currently prefer to Git-bash (until I change my workflow some day). When I change work flows & tools, maybe I can use the git-bash shell and try to use sed or grep or xargs or something. Is it any easier to dump the branch list to a file and run a cmd to parse it, then delete the file?

  2. Modify Git: I looked a bit for how to make an enhancement request to Git, but it sounded like I would have either have to write it myself or persuade enough others to do so, neither of which I have time for today.

Does anybody have a better way to get or use Git references like these?

来源:https://stackoverflow.com/questions/60675577/git-parse-branch-name-from-console-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!