Skip to main content

How to Backtrack- Learn different ways to undo changes made to a Git project and when to use them.



  • Head commit:


In Git, the commit you are currently on is known as the HEADcommit. 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:

What if you decide to change the ghost's line in the working directory, but then decide you wanted to discard that change?


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.

  • Creating a project is like hiking in a dark forest. Sometimes you take a wrong turn, then another wrong turn. Before you know it, you're surrounded by bears.

Git enables you to rewind to the part before you made the wrong turn and create a new destiny for the project. You can do this with:
git reset SHA
This command works by using the first 7 characters of the SHA of a previous commit. For example, if the SHA of the previous commit is5d692065cf51a2f50ea8e7b19b5a7ae512f633ba, use:
git reset 5d69206
Test Steps:

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:


Let's take a moment to review the new commands:
  • git checkout HEAD filename: Discards changes in the working directory.
  • git reset HEAD filename: Unstages file changes in the staging area.
  • git reset SHA: Can be used to reset to a previous commit in your commit history.
Additionally, you learned a way to add multiple files to the staging area with a single command:
git add filename_1 filename_2













Comments

Popular posts from this blog

Git Branching- Learn how to manage multiple versions of a project with Branching.

Git Branch: Up to this point, you've worked in a single Git branch called  master . Git allows us to create  branches  to experiment with versions of a project. Imagine you want to create version of a story with a happy ending. You can create a new branch and make the happy ending changes to that branch only. It will have no effect on the  master branch until you're ready to merge the happy ending to the master branch. In this lesson, we'll be using Git branching to develop multiple versions of a resumé. You can use the command below to answer the question: “which branch am I on?” Command : git branch Check what branch you are currently on.  In the output, the * (asterisk) is showing you what branch you’re on. The diagram above illustrates branching. 1. The circles are commits, and together form the Git project's commit history. 2. New Branch  is a different  version  of the Git project. It contains commits...

Basic git commands to handle codes

Creating a Repository on GitHub: ------------------------------------------ 1. Sign in to GitHub 2. Click the "+" sign beside your profile and click "New Repository" 3. Provide repository name and other necessary things if needed. Pushing Code/project folder into GitHub Repository: ------------------------------------------------- 1. Go to the project location on your pc/laptop. 2. Enter into the Project. 3. Right click on mouse and select "Git Bash Here" 4. Run the following commands SEQUENTIALLY -            git init            git add -A ( add your files )            git status            git commit -m "Your Message" ( commit locally )            git remote add origin GITHUB HTTPS PROJECT LINK                                   ...

Basic Git Workflow- how it's work

Basic Git Workflow: Git is a software that allows you to keep track of changes made to a project over time. Git works by recording the changes you make to a project, storing those changes, then allowing you to reference them as needed. Turning a directory into a project>>  command: git init The word  init  means  initialize . The command sets up all the tools Git needs to begin tracking changes made to the project. Output: would be like-  Initalized an empty g it repository in /home/ccuser/workspace/sorcerers-code/.git/ By the above output, we could say that  The Git project was created. Git workflow: A Git project can be thought of as having three parts: 1. A  Working Directory : where you'll be doing all the work: creating, editing, deleting and organizing files 2. A  Staging Area : where you'll list changes you make to the working directory 3. A  Repository : where Git permanently stores those chan...