Git is a Version Control System (VCS). That means it helps you keep track of changes you make to your files over time. It's like a magical "undo" button that lets you go back to an earlier version if something goes wrong.
Git is especially popular among developers, but anyone working with files can use it — writers, designers, researchers, and more!
A Version Control System allows you to:
There are other version control tools like SVN (Subversion), Mercurial, and Perforce, but Git is by far the most popular and widely used today.
In this blog, we'll focus on Git for tracking changes.
Want to dive deeper? Check out the official Git Documentation.
To check if Git is already on your computer, type this in your terminal:
git -v
If you don't see a response like this in the image below, you need to install Git and set it up.
To install Git, click the link below:
Before you start using Git, you need to tell it who you are. This helps track who made which changes. Use the following commands to set your name and email address:
git config --global user.name "Your Name"git config --global user.email "you@example.com"
The --global option means this name and email will be used for all your Git projects.
To confirm if you have successfully set your username and email run the following
to check for name run git config user.name
to check for email run git config user.email
Create a folder for your work let say gitblog. Inside that
folder, make a file named index.html. You can use any code
editor like VS Code or any tool you like.
A Git repository is like a storage box where Git keeps track of your project's history. It allows you to save versions of your code, go back to earlier stages, and collaborate with others.
To check if you're already in a Git repository, type:
git status
If Git is not active yet, you'll see a message saying the folder is not a repository.
To turn your current folder into a Git repository, run:
git init
This command tells Git to start watching your folder. It creates a
hidden .git folder that stores all the history and
settings.
Now you've got a Git repository! You're ready to start tracking your work.
Once you've added files to the staging area, it's time to save your work in Git. This saved snapshot is called a commit.
Use the following command to commit your staged changes:
git commit -m "Write a short message here"
The message inside the quotes should briefly describe the purpose of the changes. A good commit message helps both you and your teammates understand the history of the project.
Fix bug not
Fixed bug
Add login form validation
Example:
git commit -m "initial commit"
Instead of using the -m option, you can simply type:
git commit
This will open your default text editor (such as Vim, Nano, or VS Code). Inside the editor:
This method is preferred in larger teams and open-source projects because it improves the clarity and professionalism of your commit history.
By default, Git might open editors like Vim or Nano when writing commit messages. If you prefer using Visual Studio Code, you can set it as your default Git editor with:
git config --global core.editor "code --wait"
The --wait flag tells Git to pause until you're done
writing and saving the commit message in VS Code.
After running git commit, VS Code will launch, allowing you
to write:
Example commit message inside VS Code:
Fix crash when user submits empty form The crash occurred because input validation wasn't applied on empty fields. Added client-side and server-side validation logic and updated the error messaging system.
To see a history of all your commits, use:
git log
This command shows a list of all commits, their authors, timestamps, and unique commit hashes.
Each commit in Git has a unique ID (called a commit hash). You can temporarily go back to that point using:
git checkout [commit-hash]
Example:
git checkout a1b2c3d4
This puts you in a detached HEAD state — meaning you're no longer on a branch. If you make changes here and want to keep them, create a new branch:
git checkout -b new-branch-name
To return to the latest commit on your main branch, use:
git checkout main
Instead of using the -m option, you can simply type:
git commit
This will open your default text editor (such as Vim, Nano, or VS Code). Inside the editor:
This method is preferred in larger teams and open-source projects because it improves the clarity and professionalism of your commit history.
By default, Git might open editors like Vim or Nano when writing commit messages. If you prefer using Visual Studio Code, you can set it as your default Git editor with:
git config --global core.editor "code --wait"
The --wait flag tells Git to pause until you're done
writing and saving the commit message in VS Code.
After running git commit, VS Code will launch, allowing you
to write:
Example commit message inside VS Code:
Fix crash when user submits empty form The crash occurred because input validation wasn't applied on empty fields. Added client-side and server-side validation logic and updated the error messaging system.
To see a history of all your commits, use:
git log
This command shows a list of all commits, their authors, timestamps, and unique commit hashes.
Each commit in Git has a unique ID (called a commit hash). You can temporarily go back to that point using:
git checkout [commit-hash]
Example:
git checkout a1b2c3d4
This puts you in a detached HEAD state — meaning you're no longer on a branch. If you make changes here and want to keep them, create a new branch:
git checkout -b new-branch-name
To return to the latest commit on your main branch, use:
git checkout main
Branches let you work on new ideas or features without changing the main version of your project.
This way, you can try things out safely. The default branch is usually called main.
Once your work on a branch is done and tested, you can combine it with the main branch using a merge.
To create a new branch called newBranch, run:
git branch newBranch
To see all branches in your repository, run:
git branch
The branch with the * symbol is your current working branch.
To move to another branch, use this command:
git checkout branch-name
For example, to switch to newBranch, you would type:
git checkout newBranch
First, switch to the branch you want to keep (usually main):
git checkout main
Then, merge the other branch into it:
git merge newBranch
This brings the work from newBranch into main.
Once you're done with a branch and have merged it, you can delete it with:
git branch -d branch-name
For example:
git branch -d newBranch
Now that you understand the basics of Git, here are some recommended next steps to grow your skills further:
git remote add,
git push
git cloneContinue your journey with this official guide: GitHub Quickstart