Git, the free and open-source distributed version control system, is at the heart of countless software projects — both large and small. Its functionality and flexibility allow developers to track changes, collaborate seamlessly, and manage complex projects with ease. This guide will provide a comprehensive overview of Git, from basic commands that every beginner should know to advanced features that can boost the productivity of even seasoned experts.
Getting Started
First things first, you need to install Git. Most Linux distributions include Git in their standard repositories. To install Git on a Debian-based system like Ubuntu, use:
1
sudo apt-get install git
For Fedora, CentOS, or RHEL:
1
sudo dnf install git
Basic Git Commands
Every journey starts with the first step. Here are some of the most common commands you need to get started with Git.
1. git init
This command initializes a new Git repository in the current directory.
1
git init
2. git clone
To work with a repository that already exists, you can clone it to your local machine. Replace URL
with the URL of the remote repository.
1
git clone URL
3. git add
This command adds files from your working directory to your staging area.
1
2
git add filename # adds a specific file
git add . # adds all files in the current directory
4. git commit
To save your changes, you need to commit them. This command saves your changes to the local repository.
1
git commit -m "commit message"
5. git push
The git push
command sends your committed changes to a remote repository.
1
git push origin master # pushes changes to the master branch of the remote repository named "origin"
6. git pull
The git pull
command fetches changes from a remote repository and merges them into your current branch.
1
git pull origin master # pulls changes from the master branch of the remote repository named "origin"
Intermediate Git Commands
Once you’ve mastered the basics, you can use these intermediate commands to manage your repositories more effectively.
1. git branch
This command lists all the branches in your repository, and highlights the one you’re currently on.
1
git branch
You can also create a new branch using the same command.
1
git branch new-branch-name
2. git checkout
The git checkout
command allows you to switch between different branches and files in your Git repository.
1
git checkout branch-name # switch to an existing branch
You can create a new branch and switch to it using the -b
option.
1
git checkout -b new-branch-name
3. git merge
To combine the changes made in one branch with another, use the git merge
command.
1
git merge branch-name
4. git diff
This command displays conflicts between branches. Before a merge, you can preview the changes that will be made.
1
git diff source-branch target-branch
5. git stash
You can use the git stash
command to temporarily save changes that you don’t want to commit immediately.
1
git stash
You can apply the stashed changes later using git stash apply
.
1
git stash apply
Advanced Git Commands
For those well-versed in the basics of Git, here are some advanced commands to boost your productivity.
1. git rebase
Rebasing is the process of moving or combining a sequence of commits to a new base commit. It’s an alternative to merge.
1
git rebase branch-name
2. git bisect
Bisect helps in finding the commit that introduced a bug by using binary search.
1
2
3
git bisect start
git bisect bad # Current version is bad
git bisect good version-number # This is the last 'good' commit
3. git cherry-pick
This command applies the changes introduced by some existing commits.
1
git cherry-pick commit-hash
4. git reflog
Reflog stands for reference logs. This command will show you when the tips of branches and other references were updated in the local repository.
1
git reflog
5. git revert
This command creates a new commit that undoes the changes made in a previous commit.
1
git revert commit-hash
Mastering Git is a journey of continuous learning and exploration. The commands discussed in this guide form the foundation of this journey. As you gain proficiency, you’ll realize that Git is more than just a version control system—it’s a powerful tool to streamline your software development process. Keep experimenting, keep exploring, and let Git and its myriad features guide you on your path to becoming a Git Guru.