Git pushd & popd? I.e., checkout last state

后端 未结 3 1269
旧时难觅i
旧时难觅i 2021-02-04 07:28

I\'m writing a Bash script, and I want to checkout a tag and then checkout back to where I started.

I tried git co HEAD@{1}, but when starting at master, th

3条回答
  •  情歌与酒
    2021-02-04 08:13

    In your script, first save the current branch (like written in this answer):

    branch_name="$(git symbolic-ref HEAD 2>/dev/null)" ||
    branch_name="(unnamed branch)"     # detached HEAD
    branch_name=${branch_name##refs/heads/}
    

    then go and checkout the tag you want to

    git checkout -b tag_branch tag_name
    

    Do what you want to do on that branch, then checkout the old branch again:

    git checkout $branch_name
    

    That's it.

提交回复
热议问题