Git is a distributed version control system used to track changes made to a set of files by individuals or groups. It is a FOSS (free and open-source) tool that is widely utilized by companies due to its efficiency and speed.
Git allows the creation of parallel development branches, enabling you and your team members to work simultaneously on different project features and merge them later. By keeping track of the changes made to the project, you can easily revert to a previous version in case any issues arise.
$ sudo apt-get update
$ sudo apt-get install git-all$ sudo dnf install git-allUsing Homebrew
$ brew install gitAfter installing git, you need to configure it by setting your global username and email:
$ git config --global user.name "Your username"
$ git config --global user.email "[email protected]"If you want to configure a different username and email for a specific repository, you can run the same commands inside the folder of the repository without adding the --global option.
In Git you have three main states: the working directory, the staging area, and the Git directory (or local repository). You can also have a remote repository connected to your Git directory.
The working directory is, in essence, a copy of your project version. Git stores a compressed copy of all your versions inside a hidden folder, and they are decompressed when you need to access them.
The staging area stores information about the changes that are about to be committed to the repository.
The Git directory (or local repository) stores all the important metadata for your project: commits, remote repositories, logs, etcetera.
When you begin a new project, you can create a new folder to store your Git repository, which is created with git init
$ mkdir projectfolder
$ cd projectfolder
$ git initThis command will show you which files have been recently modified inside the project folder.
$ git statusAfter creating and/or modifying the files from your working directory, you can add them all at once to the staging area using this command:
$ git add .If you want to add a single file:
$ git add yourfilenameTo commit all the changes to the local repository, you can use this command:
$ git commit -m "Your comment"The comment should indicate what changes have been made to the repository. It's a good practice to write detailed messages that accurately represent the changes that have been made. If you type git commit without adding anything else, a text editor window will pop-up allowing you to write a message. The changes will commit to the local repository after you close the window.
$ git logAfter you modify a file, you will see it classified as "modified" after running git status. To see the changes that have been made since the last commit:
$ git diff$ git branchThis command will print the branch you're currently working on.
Verify that you do not have pending commits:
$ git statusCreate new branch:
$ git checkout -b yournewbranchnameIt's common practice to create new branches when you want to add a new feature to the project. You can name them as feature/your-feature where "your-feature" corresponds to the name of the feature you want to add.
To move from a branch to another:
$ git checkout branchnameor
$ git switch branchnameWhere "branchname" is the branch you want to move to.
NOTE: you can also use git-checkout to restore changed files to their latest version.
Let's say you have a branch that is several commits ahead from another. To merge that branch with your current branch:
$ git merge branchnameWhere "branchname" corresponds to the branch you want to merge.
You can communicate with external repositories using Git.
If you want to push to a remote repository, like one from Github:
$ git remote add origin [email protected]:username/repositoryname.git
$ git branch -M main
$ git push -u origin mainWhere "username" corresponds to the owner and "repositoryname" is the name of the remote repository.
TIP: you can replace "origin" with any other word you want, it's just an identifier.
$ git clone repository-urlWhere repository-url is the URL of the remote repository you want to clone inside your current directory.
$ git remote -vTo download changes from a single remote location:
$ git fetch originTo download changes from all remote locations:
$ git fetch --allIMPORTANT NOTE: It is not recommended to use this command to download and integrate all changes from remote at once, unless you know there will be no conflicts. It's best to use git-fetch first.
$ git pull originTo integrate changes from all remote locations:
$ git pull --allSome free external resources for learning and applying Git best practices:
- GitLab: What are Git version control best practices?
- Atlassian: Source code management
- Grant Weatherston: Git Best Practices - How to Write Meaningful Commits, Effective Pull Requests, and Code Reviews
This article was created by Facundo Martínez © 2023. It is licensed under CC BY-NC-SA 4.0
I love creating free content for everyone. If my articles and repos were useful to you, please consider supporting me on Github Sponsors, Ko-Fi or PayPal. It would be of great help and I would really appreciate it!
If you want to provide feedback on this guide (including possible additions and/or corrections), click here

