The 'Git Run'

2023-08-21
By: O. Wolfson

Here is a git run tutorial, using a simple markdown file as an example. Repetition is key to understanding and remembering these commands. This tutorial is short and can be rerun multiple times. After a few runs, you'll likely feel more comfortable with the basic Git commands!

I assume you have Git installed on your machine. If not, you can download it here.

The Git Run

Initialize a new repository

Create a new directory, navigate into it, and initialize a Git repository.

bash
mkdir git-run-tutorial
cd git-run-tutorial
git init

Create a Markdown file

Let's create a markdown file named README.md.

bash
echo "# My Git Run Tutorial" > README.md

View the contents of README.md.

bash
cat README.md

Check status

You can view the changes that have been made using the git status command.

bash
git status

Stage and Commit changes

To track the file in Git, you need to stage and then commit it.

bash
git add README.md
git commit -m "Initial commit: Added README.md"

Make further modifications

You can make further changes to the same file.

bash
echo "This is my quick tutorial to get familiar with Git." >> README.md

View changes via git diff

bash
git diff

Git diff is a command in Git that allows users to see differences between the content in the working directory, staged changes, and committed changes. Essentially, it shows the changes that have been made but have not yet been committed.

Commit the changes.

bash
git add .
git commit -m "Updated README.md with more information"

Branching

Create a new branch, switch to it, make a change, and then commit.

bash
git branch feature-branch
git checkout feature-branch
echo "Here is a new feature for our tutorial." >> README.md
git commit -am "Added a feature in feature-branch"

Merging branches

To integrate the changes made in the feature-branch into the main branch, you first switch back to the main branch and then perform a merge.

bash
git checkout main
git merge feature-branch

View branches

You can view the branches in your repository using the following command:

bash
git branch

Delete branches

Once you're done with a branch (e.g., after merging), you can delete it to keep your repository clean.

bash
git branch -d feature-branch

Reverting to an Older Commit

Sometimes, after making a change and committing it, you might realize that the change was not needed or incorrect. In such cases, Git provides ways to revert to a previous state.

Make an Undesired Change

First, let's simulate making a change that we later decide we don't want.

bash
echo "This is an undesired change to our tutorial." >> README.md
git commit -am "Added an undesired change"

If you view the contents of README.md, you'll now see the undesired change.

bash
cat README.md

View the Commit History

Before we revert, let's check our commit history to see our last commits.

bash
git log --oneline

You will see the most recent commit (the undesired one) at the top.

Revert the Undesired Change

Now, let's revert the last commit:

bash
git revert HEAD

This command will create a new commit that undoes the changes made in the last commit. After running the revert, Git will prompt you to enter a commit message for the revert action. Save and close the editor to finalize the revert.

View the contents of README.md again to see that the undesired change has been removed.

bash
cat README.md

Revert to a Specific Commit

In some cases, you may want to revert back to a specific commit from the past, not just the most recent one.

Let's simulate making a few more changes to our file, including an unintentional mistake.

bash
echo "## Git Basics" >> README.md
echo "Git is a distributed version control system." >> README.md
git commit -am "Added a section on Git Basics"

echo "This is an accidental duplication." >> README.md
echo "This is an accidental duplication." >> README.md
git commit -am "Accidentally duplicated a line"

First, identify the commit hash (or ID) from the commit log:

bash
git log --oneline

Note down the hash of the commit you wish to revert to. Now, use the git revert command with a range:

bash
git revert [older-commit-hash]^..[newer-commit-hash]

This will revert all commits from the specified older commit to the newer one. Remember that this will generate a new commit for each change being reverted.

Now, check the commit log again.

bash
git log --oneline

If you only want to revert a single specific commit and not a range, you can do:

bash
git revert [commit-hash]

As always, after the revert action, Git will prompt you for a commit message. Save and close the editor to finalize.

Now, check the commit log again.

bash
git log --oneline

Resetting changes

Resetting allows you to undo changes in your working directory or staging area. There are three modes:

  • --soft: undoes the last commit but keeps changes in the staging area.
  • --mixed: undoes the last commit and unstages the changes (default).
  • --hard: discards all changes in your working directory.
bash
# Example of a soft reset:
git reset --soft HEAD~1

# Example of a mixed reset:
git reset --mixed HEAD~1

# Example of a hard reset:
git reset --hard HEAD~1

View commit history

You can view the commit history using the following command:

bash
git log --oneline

And there you have it! With these commands under your belt, you're well on your way to mastering the basics of Git. Practice makes perfect, so don't hesitate to run through this tutorial multiple times to solidify your understanding.