2024-09-09 web, development, javascript
The 'Git Run'
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.
Create a Markdown file
Let's create a markdown file named README.md
.
View the contents of README.md
.
Check status
You can view the changes that have been made using the git status
command.
Stage and Commit changes
To track the file in Git, you need to stage and then commit it.
Make further modifications
You can make further changes to the same file.
View changes via 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.
Branching
Create a new branch, switch to it, make a change, and then commit.
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.
View branches
You can view the branches in your repository using the following command:
Delete branches
Once you're done with a branch (e.g., after merging), you can delete it to keep your repository clean.
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.
If you view the contents of README.md
, you'll now see the undesired change.
View the Commit History
Before we revert, let's check our commit history to see our last commits.
You will see the most recent commit (the undesired one) at the top.
Revert the Undesired Change
Now, let's revert the last commit:
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.
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.
First, identify the commit hash (or ID) from the commit log:
Note down the hash of the commit you wish to revert to. Now, use the git revert
command with a range:
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.
If you only want to revert a single specific commit and not a range, you can do:
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.
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.
View commit history
You can view the commit history using the following command:
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.