ChatGPT解决这个技术问题 Extra ChatGPT

Git checkout - switching back to HEAD

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

couple times while going back to point from which I can't go to HEAD, the output is 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.
Aborting

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?

If you don't want to keep those changes git clean -f will clean all the untracked files. Then checkout to master
The error: ... message means that you—or something you did, perhaps indirectly— did change the files. You can tell Git to discard or overwrite these changes, or use git status and/or git diff to see what, precisely, changed. It could be as simple as some kind of line-ending modification, or some command that runs in the background that wrote to a file. (Aside: "go back to HEAD" makes no sense in Git, as you're always on HEAD: HEAD literally means "current commit". You can change HEAD, after which you're still on HEAD, just a different HEAD. This is ... difficult to phrase. :-) )
Git is loaded with mechanisms, so here are two: git reset --hard HEAD means "reset the index and work-tree to match HEAD", i.e., throw away changes. Or: git checkout -f master means "change HEAD to be master, even if that means throwing away some work": -f means "force".

S
Sajib Khan

You can stash (save the changes in temporary box) then, back to master branch HEAD.

$ git add .
$ git stash
$ git checkout master

Jump Over Commits Back and Forth:

Go to a specific commit-sha. $ git checkout

If you have uncommitted changes here then, you can checkout to a new branch | Add | Commit | Push the current branch to the remote. # checkout a new branch, add, commit, push $ git checkout -b $ git add . $ git commit -m 'Commit message' $ git push origin HEAD # push the current branch to remote $ git checkout master # back to master branch now

If you have changes in the specific commit and don't want to keep the changes, you can do stash or reset then checkout to master (or, any other branch). # stash $ git add -A $ git stash $ git checkout master # reset $ git reset --hard HEAD $ git checkout master

After checking out a specific commit if you have no uncommitted change(s) then, just back to master or other branch. $ git status # see the changes $ git checkout master # or, shortcut $ git checkout - # back to the previous state


That solved my problem - partially. Still I need to know how to jump over commits back and forth without consequences.
@rbraun, I have edited my answer showing how you can jump over commits back and forth.
Whew. I was wondering why I can see the changes I made in a branch in the master. It was due to I have not add, commit, and push the branch to origin. Your solution worked.
A time saver, why Git don't create itself as more human-like things, I see there is tons of questions about it, and it is just a version control software=.=!
with git 2.17.1, git checkout master => "error: pathspec 'master' did not match any file(s) known to git" but git checkout - is fine.