🗓️ Git version control: Ultimate Guide
Welcome to the Ultimate Guide to master
git in your daily workflow, we will see what are the most used commands, what are the best practices, and tips and tricks.
This guide is a summary of the most important things to know when working with
git, and in general, will link to the official documentation of
git or other resources for more details, it is on purpose to not go in depth in each topic, it allows to summarize
git and vocabulary about it (you can use it as a
Note: Sources used to write this blog post are available at the end of this post.
Git is a free and open-source distributed version control system for keeping track of changes across a set of files.
- to be able to work with several people on the same codebase.
- track changes to know who did what and when.
- revert changes.
Git is decentralized, which means that every developer has a full copy of the repository and the complete history of the project.
Get started with
.gitconfig config file
The first thing you should do when you install Git is to set your user name and email address.
git config --global user.name "Username" git config --global user.email "email@example.com"
These configurations are stored in the
.gitconfig file in your home directory (e.g:
~/.gitconfig) with this format:
[user] name = Username email = firstname.lastname@example.org
You can find more information and useful
git configurations in the official documentation.
git project is called a repository (or repo for short) and it contains all the files and folders for a project, as well as each file's revision history (commits) stored in the
The history of a repository is represented by a graph.
Each node is called commit and contains:
- an instantaneous view (snapshot) of the state of the repository at a specific moment
- metadata: message, author, creation date, etc.
Commits are snapshots (not diffs on each file) of the project at specific moments in time.
There are several areas where the files in your project will live in Git:
- Working directory: the files that you see in your computer's file system.
- Staging area: the files that will go into your next commit (files added with
git add <filename>command).
- Local repository: the
.gitdirectory, which contains all of your project's commits, branches, etc. (files added with
git commit -m "message"command).
- Remote repository: the
.gitdirectory in a remote server (files added with
You can find the official documentation of
git commands at git-scm.com/docs.
# Initialize a new git repository git init # Clone a repository git clone <url> # Add all the files to staging area git add . # Add specific file to staging area git add <file> # Commit changes git commit -m "chore: initial commit" # Add remote repository git remote add <remote> <url> # The main <remote> is often called `origin` # Add forked repository git remote add <remote> <url> # The forked <remote> is often called `upstream` # List all the remotes git remote # Sync forked repository git fetch <remote> git merge <remote>/<branch> # Push changes to remote repository git push <remote> # Pull changes from remote repository git pull <remote> # Show the status of the working tree git status # Show the commit history git log # Create a new branch git checkout -b <branch> # Switch to a branch (or tag or commit) git checkout <branch> # Merge a branch into the current branch git merge <branch> # Delete a branch git branch --delete <branch> git push <remote> --delete <branch> # Fetch branches from remote repository and prune git fetch --prune # Revert a commit git revert <commit> # Change several past commits (interactive rebase) # HEAD points to the current consulted commit. git rebase --interactive HEAD~<number-of-commits> # Reset the current branch, delete all commits since <branch> (without removing the changes) git reset --soft <branch> # Apply the changes introduced by some existing commits git cherry-pick <commit>
.gitignore file is a text file that tells
git which files (or patterns) it should ignore.
.gitignore file is usually placed in the root directory of the repository.
We usually ignore files that are generated by the build process or files that contain sensitive information.
.env build *.exe
.gitkeep file is a file that is used to keep an empty directory in a Git repository.
This is useful when you want to keep an empty directory in your repository but you don't want to commit any file inside it.
Git remote repositories (GitHub/GitLab)
There are many other services, you can also self-host your own Git server.
SSH vs HTTPS authentication
Once you have created a remote repository, you will need to authenticate to push and pull changes.
There are two main ways to authenticate:
- SSH: you will need to generate an SSH key pair and add the public key to your remote repository.
- HTTPS: you will need to provide your username and password each time you push or pull changes.
SSH authentication is the recommended way to authenticate to a remote repository.
You can find more information about SSH authentication in the official documentation.
git commits with
As we have seen in the Get started with
.gitconfig config file section, we can configure
git with a name and email address with a value of our choice.
That means that anyone can create a commit with any name and email address and claim to be whoever they want when they create a commit.
To avoid this, you can sign your commits with a GPG key.
You can find more information about signing commits in the official documentation.
Continous Integration/Continuous Delivery (CI/CD)
Once you have your code in a remote repository, everyone (with access) can potentially start contributing to the project. This is great, but it also means that you need to have a way to ensure that your code is working as expected for each change in the project.
You could do it manually, depending on the size and the complexity of the project, but it could be a tedious task.
Instead, you can use a Continuous Integration (CI) service to automate the process of testing your code, running linting, unit tests, e2e tests, etc.
Then, once your code is ready, tested and working as expected, you can use a Continuous Delivery (CD) service to automate the process of deploying your code.
CI/CD services are usually integrated with remote repositories, so you can configure them to run automatically when you push changes to the remote repository.
Best practices and
Commit messages are very important, they are a way to easily know what has changed in the project.
There are many conventions for commit messages, but the most popular one is the Conventional Commits specification.
Then, we can use the commit messages to automatically determine a semantic version for the next release of the project.
When multiple developers are working on the same project, it is important to organize the work in a way that everyone can work on different features without conflicts (changes in the same files).
There are many ways to organize the work, but the most popular ones are:
They are called Git workflows, or Git branching strategies.
git is the tool that every programmer should know to do collaborative work (not only,
git is also very powerful even when working alone) and keep track of changes across a set of files.