Git allows you not only to view the history, but also to navigate through it by uploading the state of the code to the working directory at the time of any commit. Let's have a look:
# Shows abbreviated output git log --oneline fc74e2d update README.md 65a8ef7 Revert "remove PEOPLE.md" 5120bea add new content e6f625c add INFO.md 273f81c remove NEW.md aa600a4 remove PEOPLE.md fe9893b add NEW.md 3ce3c02 add PEOPLE.md 3c5d976 add README.md
Let's switch to the moment when the commit with the add INFO.md message was executed. To do this, use the
git checkout <commit hash> command:
git checkout e6f625c Note: switching to 'e6f625c'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch. Or undo this operation with: git switch -
Run the command above (your commit hash may be different) and examine the working directory. You will see that some of the changes are missing since we're going back in time. The changes themselves have not gone anywhere, and we can go back to the last commit again with the next command:
# What "main" means is something we'll talk about later git checkout main
By switching to the desired commit, you can not only explore the contents of the repository, but also pick up any changes that were deleted but are needed again for work. To do this, just copy them, switch to the last commit, and paste them into the desired file.
Switching between different commits only affects the contents of the working directory. Where we are is not visible anywhere else. Because of this, quite a few programmers forget where they are and start working and, as a result, are very surprised when they can't get a commit done.
The easiest way to find out your position is to call the
git branch command. Normally, when we are on the last commit, git will show this output:
git branch # We'll talk about what "main" is later * main
But if a commit from the past is loaded right now, the output will be this:
* (HEAD detached at e6f625c) main
This way of checking your current position requires constant attention. You have to remember to use it and, of course, everyone forgets to. It is much more reliable and convenient to display the current position directly on the command line. For example, like this:
# If on the last commit hexlet-git git:(main) # If on a commit from the past hexlet-git git:(e6f625c)
That's what most professional developers do. How do you achieve that output? The answer to this question depends on the shell used. In Bash, you can display position in prompt line by editing the
$PS1 environment variable, you can read more about this by following the link in the additional resources.
The Hexlet support team or other students will answer you.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
From zero to a developer. Refunds in case you won't get a job
Sign up or sign in
Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.