An Introduction To Git
A primer on source control using Git
Git is an open source distributed version control system created by Linus Torvalds in 2005 for collaborating with other developers contributing to the Linux kernel.
Although Git is used primarily by programmers for tracking changes in source code during software development, it can be used for tracking changes in any set of files.
This post is a quick introduction to Git, covering the most important commands needed to use Git in your projects.
A Git project will have the following three main sections:
- Git directory
- Working directory (or working tree)
- Staging area
The Git directory (located in YOUR-PROJECT-PATH/.git/) is where Git stores everything it needs to accurately track the project. This includes metadata and an object database which includes compressed versions of the project files.
The working directory is where a user makes local changes to a project. The working directory pulls the project’s files from the Git directory’s object database and places them on the user’s local machine.
The staging area is a file (also called the “index”, “stage”, or “cache”) that stores information about what will go into your next commit. Staging is therefore the process of adding or removing files to the staging area to be committed. A commit is when you tell Git to save these staged changes. Git takes a snapshot of the files as they are and permanently stores that snapshot in the Git directory.
Getting Started
To use Git in your projects, you need to first install it (if not already installed) in your system by following the instructions here.
Verify if Git is installed by using the following command in the terminal:
$ git --version
Next, configure your Git environment by setting certain configuration variable using git config
.
There are three levels where these configuration variables are stored:
- System: located in /etc/gitconfig, applies default settings to every user of the computer. To make changes to this file, use the –system option with the git config command.
- User: located in ~/.gitconfig or ~/.config/git/config, applies settings to a single user. To make changes to this file, use the –global option with the git config command.
- Project: located in YOUR-PROJECT-PATH/.git/config, applies settings to the project only. To make changes to this file, use the git config command.
To see all your configuration settings, use the command:
$ git config --list
Initialize Git in a Project
Once Git is installed and configured on your computer, you need to initialize it in your project to start using it for version control.
In the command line, use the cd
command to navigate to the root level directory of your project, and run the command:
$ git init
This installs a Git directory (.git/ folder with all the files and objects Git needs to track your project.
Adding and Committing your work
Once initialized, Git will start tracking the changes you make within the working directory of your project. You can check the status of your working tree with the command git status
which shows important details about your project like current branch, and all the changes, staged or unstaged for committing.
To add the changes to the staging area, use the git add
command followed by the files and directories to be added for commit.
For example:
$ git add file1 file2 dir1/file3 dir2/
Once addedd for staging, the changes can be committed with a commit message like below:
$ git commit -m "Initial commit"
Branches
A branch is nothing but a pointer to the latest commit in the Git repository referenced by HEAD. So currently, you are in the master branch which is a pointer to the commit with the message “Initial commit”. Whenever you make a commit, HEAD is updated with the latest commit.
To work on any new task/issue on a project, it is always advised to create a new branch to avoid breaking the committed state so far.
Create and switch to a new branch using the git checkout
command with the -b
flag. For example:
$ git checkout -b mylocalbranch
You can view all the branches in your repository with the git branch
command, and switch between branches using the git checkout
followed by the branch name.
Once you’re done with your changes, you can commit and merge them to the master branch:
git commit -m "Commit changes to mylocalbranch"
git checkout master
git merge mylocalbranch
After this, you can safely delete that branch with the git branch
command along with the -D
flag like so:
$ git branch -D mylocalbranch
You can run git log
to see the commit history of your repository.
Working with Remotes
When working on a collaborative project, each collaborator works on a local copy of the project that sits on a remote repository, and periodically updates the remote with their local changes.
For that, we use a hosted Git service, a popular one being Github.
You can start working on a local copy of an existing remote repository using the git clone
command with the URL of the remote repository as argument or create a new remote repository and set it to track your local repository using the git remote add
command with name you want to give to the remote (eg. remote) followed by the URL of the remote repository as argument.
To incorporate the changes from the remote into your local repository, use the git pull
command which is the same as a git fetch
followed by a git merge
.
To update the remote with your saved or committed local changes, use the git push
command.
Below is a visualization of a basic Git workflow:
That’s pretty much all the fundamentals needed to get started with Git. For a list of some of the most commonly used Git commands, please refer to this Gist. I shall try to post some updates in future posts covering some advanced workflows in Git like resolving merge conflicts, resetting or reverting changes, rebasing, working with submodules, etc.
See you in another one!