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.
Git is decentralized, which means that every developer has a full copy of the repository and the complete history of the project.
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:
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:
git add <filename>command).
.gitdirectory, which contains all of your project's commits, branches, etc. (files added with
git commit -m "message"command).
.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> # Note: Merge creates a "Merge commit" when the base branch and the branch to merge have diverged (they have different commits). # To avoid creating a "Merge commit", we can use rebase instead of merge. git rebase --interactive <branch-to-rebase-on> # Combine multiple commits of a branch into one for a merge git merge --squash <branch> # Change several past commits (interactive rebase) # HEAD points to the current consulted commit. git rebase --interactive HEAD~<number-of-commits> # 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> # 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 # (by first being on the branch where you want to apply the commit) git cherry-pick <commit> # To list all commits that differ between two branches git log <branch1>..<branch2> # commits in branch2 that are not in branch1 (branch2 ahead of branch1, branch2 behind branch1) git log <branch2>..<branch1> # commits in branch1 that are not in branch2 (branch1 ahead of branch2, branch1 behind branch2) # Summary of commit authors across all branches, excluding merge commits. git shortlog --summary --numbered --all --no-merges
.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.
There are many other services, you can also self-host your own Git server.
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 authentication is the recommended way to authenticate to a remote repository.
You can find more information about SSH authentication in the official documentation.
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 GNU Privacy Guard (gpg) key.
You can find more information about signing commits in the official documentation.
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.
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.