Git and GitHub Essentials: A Guide to Effective Version Control for Personal Projects
Photo by Roman Synkevych on Unsplash
In this article, you will learn how to get started using Git and GitHub for version control.
Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.
— https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control
In short, you do not need to make uncountable copies of your work to maintain the revision history of the work. Git can do that for you!
Git is a version control tool. GitHub is a cloud platform that stores repositories on the internet. Git/GitHub makes working with version control and collaboration easy.
By the end of this article, you will have learned most of the basic Git commands that you will use when working with Git as a version control tool. You will also create a remote repo in GitHub.
Prerequisites
Git is a command line tool. Before you learn how to use Git, it is essential that you have the following:
Some familiarity with the command line (or terminal) to run Git commands.
A directory in your machine for storing some code. You can name it whatever you like. (But we shall pretend that we are building a Todo app. Maybe name the directory
todo
).An installation of Git in the machine you will use. Check whether Git is installed by running:
git --version
If the version number is printed, you have Git installed. Otherwise, follow instructions in this guide for your OS to install Git.
Create a repo
You tell Git to be ready to keep a record of the changes happening in your code by initializing a directory as a repo. This is done by calling the init
command in the directory.
Create a parent directory named todo
in a location of your choice. Open your terminal and change into the todo
directory. Once you are in the right directory, type the init
command and hit enter:
git init
This command will create an empty Git repo ready to record/track changes that happen in your code. This repo is known as a local repo.
Add files to the staging area
Create a file in the todo
directory and call it todo.py
where you shall type the code for the Todo app.
To tell Git to keep track of changes in the todo.py
file, you call the add
command. The add
command takes a snapshot of the current state of the working tree and adds it to the staging area.
You call add
with the file name you want to add to the staging area.
git add todo.py
When you want to stage several files at once, modify the add
command as below (instead of typing out long file names):
git add .
Be sure to call the add
command whenever you make changes to your code for Git to add them to its index. Otherwise, Git will never know what changes you want to record in history.
Check the status of a repo
You can check the content of the staging area (the changes Git is keeping track of) by calling the status
command.
git status
This command will tell you whether a file is staged for commit, unstaged, or staged but modified, and more.
Commit changes
Changes in your code will be recorded when you call the commit
command. This command takes the content of the staging area (index) and adds it to the history of other changes.
The commit
command requires a message describing the changes you want to record. The commit message comes in handy when browsing the commit log
, the history of changes recorded.
git commit -m "Initial commit"
The message that goes with this commit is given by passing the -m
option and typing the message in quotes.
An alternative is git commit
with no options. This will open a text editor (usually Vim) where you can type in your message. This is useful when you want to give more details about the changes you are committing. Check this article for more information on Git commit messages.
Create branches
One thing I like about using Git for my workflow is that I can develop a small feature, test it out then integrate the feature into the main code when I am satisfied.
Using Git branches makes this workflow possible. To create a branch, call the branch
command passing the name of your branch.
git branch add-todo
This creates the branch add-todo
but does not switch to it. To switch to the new branch, call the switch command.
git switch add-todo
Once you switch to another branch, the changes you make to your code will be committed to that branch.
By default, when you initialize a Git repo, a main branch is created. You then create other branches for your other work as needed.
Merge branches
Once you have tested the code in a branch and are satisfied, you can merge that code back to the main branch. Merging is incorporating the changes from one branch into another branch.
To merge changes:
- Commit the changes you want to merge.
- Switch to the branch you want to merge into.
- Call the
merge
command with the name of the branch you want to merge.
Let's look at an example.
You have made some changes to the Todo app code in the add-todo
branch. You now wish to merge the changes to the main branch.
- Commit the changes in the
add-todo
branch:git add .
git commit -m "message describing the changes you made"
- Switch to the main branch
git switch main
- Merge the
add-todo
branch intomain
branchgit merge add-todo
After this step, the main
branch has all the changes in the code so far.
Commit log
You can see the history of the changes you have made by calling the log
command.
git log
There are many options you can pass to the log
command to format how the log is displayed. See here for an overview.
Push changes to GitHub
When you make some changes to your local repo and have a working app, you may choose to host your repo on GitHub for storage or to collaborate with other developers. You do this by pushing your local repo to GitHub (remote repo). Follow these steps to push your repo to GitHub:
- Head over to GitHub: https://github.com
- Follow this guide to create a repo: https://docs.github.com/en/get-started/quickstart/create-a-repo
- Get the URL of the remote repo
- Add the URL to your local repo
- Push your local repo to GitHub
Then head over to GitHub to see a copy of your local repo.
Ignore files and directories
One last thing, you can tell Git to ignore files and directories that you don't want to track. To do this, create a file with the name .gitignore
and list all the files and directories that you want ignored by Git.
touch .gitignore
echo file-to-ignore > .gitignore
Any time you call the add
command, the file-to-ignore
will never be tracked.
Summary
You now know the most basic Git commands to start using Git in your workflow. Integrating Git into your workflow changes how you work in a great way. You do not need to make copies of files with different names to maintain a revision history of your work. Nor comment on the code that you are testing out. You only need to use the power of one tool: Git.
Any time you are stuck on how to call a command, you can quickly check the Git manpages for help.
git help
(with no options) lists the most common commands that you might need.
For help on a specific command, add the name of that command:
git help [command]
For instance, to get help on the add
command, call:
git help add
What to do next
For you to practice the commands you learned in this article, here is a simple task for you:
- Create a Readme.md file.
- Add a description of the Todo app, and what features it supports.
- Add a License file.
- Commit the changes.
- Push the changes to GitHub.
Happy coding!