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 <
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:
Stash your modifications (you can pop them later):
$ git stash
Discard the changes reset-ing the detached branch:
$ git reset --hard
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
To return to the latest version:
git checkout <branch-name>
For example, git checkout master
or git checkout dev
When you go back to a previous version,
$ git checkout HEAD~2
Previous HEAD position was 363a8d7... Fixed a bug #32
You can see your feature log(hash) with this command even in this situation;
$ git log master --oneline -5
4b5f9c2 Fixed a bug #34
9820632 Fixed a bug #33
...
master
can be replaced with another branch name.
Then checkout it, you'll be able to get back to the feature.
$ git checkout 4b5f9c2
HEAD is now at 4b5f9c2... Fixed a bug #34
git checkout master
should do the trick. To go back two versions, you could say something like git checkout HEAD~2
, but better to create a temporary branch based on that time, so git checkout -b temp_branch HEAD~2
I am just beginning to dig deeper into git, so not sure if I understand correctly, but I think the correct answer to the OP's question is that you can run git log --all
with a format specification like this: git log --all --pretty=format:'%h: %s %d'
. This marks the current checked out version as (HEAD)
and you can just grab the next one from the list.
BTW, add an alias like this to your .gitconfig
with a slightly better format and you can run git hist --all
:
hist = log --pretty=format:\"%h %ai | %s%d [%an]\" --graph
Regarding the relative versions, I found this post, but it only talks about older versions, there is probably nothing to refer to the newer versions.
With Git 2.23+ (August 2019), the best practice would be to use git switch instead of the confusing git checkout command.
To create a new branch based on an older version:
git switch -c temp_branch HEAD~2
To go back to the current master branch:
git switch master