How to get back to most recent version in Git?

前端 未结 10 1213

I have recently moved from SVN to Git and am a bit confused about something. I needed to run the previous version of a script through a debugger, so I did git checkout <

10条回答
  •  爱一瞬间的悲伤
    2021-01-29 18:05

    When you checkout to a specific commit, git creates a detached branch. So, if you call:

    $ git branch 
    

    You will see something like:

    * (detached from 3i4j25)
      master
      other_branch
    

    To come back to the master branch head you just need to checkout to your master branch again:

    $ git checkout master
    

    This command will automatically delete the detached branch.

    If git checkout doesn't work you probably have modified files conflicting between branches. To prevent you to lose code git requires you to deal with these files. You have three options:

    1. Stash your modifications (you can pop them later):

      $ git stash
      
    2. Discard the changes reset-ing the detached branch:

      $ git reset --hard
      
    3. Create a new branch with the previous modifications and commit them:

      $ git checkout -b my_new_branch
      $ git add my_file.ext
      $ git commit -m "My cool msg"
      

    After this you can go back to your master branch (most recent version):

    $ git checkout master
    

提交回复
热议问题