Register to get access to free programming courses with interactive exercises

Moving through history Git fundamentals

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
65a8ef7 Revert "remove"
5120bea add new content
e6f625c add
273f81c remove
aa600a4 remove
fe9893b add
3ce3c02 add
3c5d976 add

Let's switch to the moment when the commit with the add 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.

Where am I

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)

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.

Do it yourself

  1. Execute all the commands from the lesson
  2. Make the terminal display your current position

Hexlet Experts

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing:">Bookmate</span>
<span class="translation_missing" title="translation missing:">Healthsamurai</span>
<span class="translation_missing" title="translation missing:">Dualboot</span>
<span class="translation_missing" title="translation missing:">Abbyy</span>
Suggested learning programs

From a novice to a developer. Get a job or your money back!

Frontend Developer icon
Development of front-end components for web applications
start anytime 10 months
Layout Designer icon
New Under development beginner
Layout with the latest CSS standards
start anytime 5 months

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions