- Head commit:
In Git, the commit you are currently on is known as the
HEAD
commit. In many cases, the most recently made commit is theHEAD
commit.
To see the
HEAD
commit, enter:
git show HEAD
The output of this command will display everything the git log command displays for the
HEAD
commit, plus all the file changes that were committed.
For example: Enter the command to show the
HEAD
commit.
Notice the output. The ghost's most recently added line is in green text.
- Git Checkout:
You could rewrite the line how it was originally, but what if you forgot the exact wording? The command
git checkout HEAD filename
will restore the file in your working directory to look exactly as it did when you last made a commit.
1. Make changes as you want on your coding/comment.
2. From the terminal, use
git diff
to see the difference between file_name as it appears in the working directory vs. how it appears in your last commit.
You may need to press
q
on your keyboard to restore the terminal.
3. Use the new Git command (git checkout HEAD filename) to restore the file in your working directory to look as it did when you last made a commit.
Notice that the changes you made to the ghost's line have been discarded.
- In Git, it's common to change many files, add those files to the staging area, and commit them to Git in a single commit.
For example, say you want to change the character "LARRY" to "LAERTES" in the script. The name currently appears in two files. After you change the name in both files, you could add the changed files to the staging area with:
git add filename_1 filename_2
Note the word
filename
above refers to the name of the file you are adding to the staging area.- Git Reset:
What if, before you commit, you accidentally delete an important line from file_1.txt (for example)? Unthinkingly, you add file_1.txt to the staging area. The file change is unrelated to the previous step's swap (change) and you don't want to include it in the commit.
We can unstage that file from the staging area using
git reset HEAD filename
This command resets the file in the staging area to be the same as the
HEAD
commit. It does not discard file changes from the working directory, it just removes them from the staging area.
Test steps:
1. Make changes into file_1.txt file.
2. Add the above file to the Git staging area.
3. Now check the
status
of the Git project.
In the output, notice file_1.txt under “Changes to be committed”.
4. Use the new Git command (git reset HEAD filename) to unstage file_1.txt from the staging area.
Notice in the test output, "Unstaged changes after reset":
M scene-2.txt
M
is short for "modification"
5. Now that changes made to file_1.txt have been booted out of the staging area, you're ready to commit. Make a commit to save the previous swap in Project.
1. Print out your Git commit log.
2. From the terminal, enter the command to
reset
to a previous commit, using the first 7 characters of one of the past commit SHAs in your Git log.
Next, print the Git commit log again.
Notice anything interesting? The commits that came after the one you reset to are gone. The
HEAD
commit has been reassigned. You just changed history.- Better understanding of RESET action:
To better understand
git reset commit_SHA
, notice the diagram above. Each circle represents a commit.
Before reset:
HEAD
is at the most recent commit
After resetting:
HEAD
goes to a previously made commit of your choice- The gray commits are no longer part of your project
- You have in essence rewinded the project's history
- Let's take a recap:
Comments
Post a Comment