A Guide to Git & GitHub

In this course we will be learning about version control especially Git and GitHub which provides hosting for Git version control.

Undoing work

Many times when you are working on a task you feel you made a mistake and you do undo the work. You have used the undo command quite often working with MS Word.

The same situation might happen while working on software project.

Undoing local changes - before staging

Make sure you are on the latest commit in master before proceeding.

Sometimes you have modified a file in your local working directory and you wish to just revert to what has already been committed. The git checkout command will handle that.

Change index.html and add a bad line into the file like below.

echo "<p>This is my bad line and I want to revert it.</p>" >> index.html

On linux you can use,

$ echo "echo "<p>This is my bad line and I want to revert it.</p>" >> index.html" >> index.html

Now, check the status of the working directory.

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")

We see that the index.html file has been modified, but hasn’t been staged yet.

Use the checkout command to checkout the repository’s version of the index.html file.

$ git checkout index.html

You should see,

Updated 1 path from the index

If you now run status command you should see,

On branch master
nothing to commit, working tree clean

Your changes are undone, you can verify it by opening up file and check if the last changes are gone or not.

Undoing staged changes - before committing

Modify the index.html file like before.

Change index.html and add a bad line into the file like below.

echo "<p>This is my bad line and I want to revert it.</p>" >> index.html

On linux you can use,

$ echo "echo "<p>This is my bad line and I want to revert it.</p>" >> index.html" >> index.html

Now, go ahead and stage it.

$ git add index.html


Check the status of your unwanted change.

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   index.html

The status output shows that the change has been staged and is ready to be committed.

Fortunately the status output tells us exactly what we need to do to unstage the change.

$ git restore --staged index.html
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
 modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")

And then run 

$ git restore index.html

To clean up your working directory.
Previous Next

Unlock Your Potential with Engineeous

Get Started