Git checkout - go back to HEAD

I was engaged in my project, and at some point I discovered that one thing stopped working. I needed to see the status of my code when it worked correctly, so I decided to use git checkout (because I wanted to check something). And so I did

git checkout SHA 

a couple of times, returning to the point from which I cannot go to HEAD, the output is as follows:

 git checkout SHA-HEAD error: Your local changes to the following files would be overwritten by checkout: [list of files] Please, commit your changes or stash them before you can switch branches. Aborting 

I am pretty sure I haven't changed anything. Team

 git checkout master 

gives the same result.

Is there any way to return to HEAD?

What is the safest way to leap history?

+7
git git-checkout
source share
1 answer

You can stash (save the changes in the time window) and then return to the master branch of HEAD.

 $ git add . $ git stash $ git checkout master 

Jump back out of bounds and back:

  • Go to the specific commit-sha .

     $ git checkout <commit-sha> 
  • If there are uncommitted changes here, you can check the new branch, Add, Commit, Direct the current branch to the remote computer.

     # checkout a new branch, add, commit, push $ git checkout -b <branch-name> $ git add . $ git commit -m 'Changes in the commit' $ git push origin HEAD # push the current branch to remote $ git checkout master # back to master branch now 
  • If you have changes in a specific commit and you do not want to save the changes, you can do stash or reset , and then check for master (or any other branch).

     # stash $ git add -A $ git stash $ git checkout master # reset $ git reset --hard HEAD $ git checkout master 
  • After checking for a specific commit, if you don't have uncommitted changes, return to the master or other branch.

     $ git status # see the changes $ git checkout master # or, shortcut $ git checkout - # back to the previous state 
+11
source share

All Articles