Explore Courses Blog Tutorials Interview Questions
+3 votes
in DevOps and Agile by (29.3k points)

I've been doing my project while at some point I discovered that one thing stopped working. I needed to look up the state of my code when it was working correctly, so I've decided to use git checkout (because I wanted to check-something-out). And so I've done

git checkout SHA

a couple of times while going back to the point from which I can't go to HEAD, the output is the following:

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.


I am pretty much sure I have NOT changed anything. The command

git checkout master

gives the same output.

Is there a way to go back to HEAD?

What is the safe way of "jumping over" history commits?

1 Answer

+3 votes
by (19.4k points)
edited by

For this, you could stash the changes and then come back to master branch using:

git add .

git stash

git checkout master

if you need to go to a specific commit then use:

git checkout <commit-sha>

If you have uncommitted changes here then, 

# 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 don't want to keep the changes in a specific commit then you could do:

# stash

 git add -A

 git stash

 git checkout master

# reset

 git reset --hard HEAD

 git checkout master

For more commands like this please go through the following tutorial that will help you understand the git


Browse Categories