This post assumes you are comfortable using the command line in whatever environment you are running in. So if you do not have the basics down for navigating your computer via the terminal I highly recommend you do that first.
If you do not already have git installed it will be helpful to have so that you can follow along.
If you are on linux you can install it with your distro's package manager. If you are unsure which package manager to use go here
If you have Homebrew installed you can simply run this command in your terminal
brew install git
If you are having trouble you can go here
To install it on windows you can use their standalone installer located here
Before learning git as a tool you need to have some things preconfigured.
Firstly, you are going to want to tell git who you are.
Set your username:
git config --global user.name YOUR_NAME
Set your email:
git config --global user.email YOUR_EMAIL
Check the information you just provided:
git config --global --list
What these commands do is add lines to your .gitconfig file which is usually located in your home directory. So if you were
to print out the contents your config file by running cat ~/.gitconfig
you should see something
like this:
Now that you have git installed and configured you are ready to begin using the tool. The first thing you are going to do is navigate to you're projects folder.
cd path/to/your/project/folder/
If you do not already know, repos is short for repository. A git repository allows you to perform various operations on it to create different versions of your project.
Now to initialize your git repos you need to run the following command:
git init
It is also good practice to provide some type of README
file in your projects root directory. This can
either be a regular text file called README
or you can use markdown styling with READM.md
.
A readme file can be the first thing you edit, track, and commit so create one by doing:
touch README
Now Add the files to the Staging Area for a commit:
git add . # This adds all the files in your local repository and stages them for a commit
# If you want to add a specific file you can run:
git add PATH/TO/FILE
Before commiting the changes we added to the staging area we can check the status to make sure we have added everything we wanted to by running the command:
git status
Now that we checked the status of our commit and made sure all the files we wanted are added we can commit them
git commit -m My First Commit
It does not matter what you put as your commit message, it helps if it's meaningful to the changes you committed though.
Now that you've created a commit you should be able to see it in the log. To look at the git log simply run the command:
git log
If you noticed you made a mistake in your commit or if you want to commit changes later you can reset it using:
git reset HEAD~1
# this will remove the most recent commit
What has been covered in this post will be enough to track changes in a project locally. What's missing and a bit more complicated is connecting to remote repositories and cloning them, as well as making branches so that you can roll in features or work simultaneously with other people. This might be covered at a later date in a intermediate-git blog post.