0

Currently I'm using this alias to fetch master in the background and then switch to it. This way Visual Studio works the fastest for me:

[alias]
  fetch-checkout = !git fetch -p && git fetch origin master:master && git checkout master

What I'd like to do on top of that is to capture current branch name and delete it afterwards.

Is it possible on Windows?

abatishchev
  • 92,232
  • 78
  • 284
  • 421

2 Answers2

3

https://stackoverflow.com/a/12142066/7976758
Q: How to get the current branch name in Git?
A: git rev-parse --abbrev-ref HEAD

[alias]
  fetch-checkout = !curbr=$(git rev-parse --abbrev-ref HEAD) && git fetch -p && git fetch origin master:master && git checkout master && git branch -D $curbr
phd
  • 57,284
  • 10
  • 68
  • 103
  • I haven't tested but it should work. `git` calls `bash` anyway even on w32. There are also other unixisms both here and in the other answer — bacticks, `$()`. – phd Dec 14 '18 at 11:02
  • this is a better answer than using a file to store the branch name as in the other answer. – Chris Thompson Jul 17 '20 at 16:43
2

Since you can't delete a branch you're currently on, you would need to use some kind of temporary storage for the old branch's name. Something like:

git rev-parse --abbrev-ref HEAD > tmp.txt && git checkout master && git branch -d `cat tmp.txt` && rm tmp.txt

would work, but you'd need to make sure you're not overwriting anything with the > tmp.txt

ack_inc
  • 886
  • 4
  • 10