There are few technologies in IT that achieve such dominance that they become virtually unrivaled. Git is one of them. It plays an essential role in software development, so much so that it has rendered nearly all alternatives obsolete (RIP SVN, and Mercurial). Today, we can hardly imagine software development without Git.
Mastering Git is one of the first skills every software developer should acquire. Although it might seem complex at first, once you get a handle on the basics, it becomes a powerful and intuitive tool for version control. In this post, we’ll cover 10 essential Git commands that will help you take control of your code and workflow, moving you one step closer to Git mastery.
Before we start, I want to invite you to visit 0dev, an open-source data platform that works with natural language. Use 0dev to access your data without complex SQL queries, visualize it without any coding and generate insights without any data science experience.
Repository: https://github.com/0dev-hq/0dev
I assume you have git installed on your machine. If you're not sure about it, simply run this command in your terminal or shell:
git -v
If you see a version it shows you're ready to use it, otherwise you need to install git or fix your installation.
Commands
Now let's start looking at the 10 first git commands you need to learn. These are the commands you use most of the times and also are the foundation of anything else you do with git.
1. git init
The git init
command initializes a new Git repository, meaning it sets up everything Git needs to start tracking your project. When you run this command, Git creates a hidden .git
directory in your project. This directory is where Git stores all the tracking data, including information about the project’s history, branches, and configuration.
Example
$ mkdir my_project
$ cd my_project
$ git init
After running this command, your project is now under Git version control.
2. git clone
Unlike git init
, which creates a new, empty repository, git clone
is used to copy an existing repository from a remote server to your local machine. This command is commonly used to pull down repositories from hosting services like GitHub, GitLab, and others, allowing you to have a complete copy of the project, including all of its history, branches, and files.
Example
$ git clone https://github.com/0dev-hq/0dev
This command creates a folder named 0dev
in your current directory and copies all the content into it.
3. git add
When you make changes to your codebase, whether adding new files or updating existing ones, you’ll need to tell Git which changes to track. The git add
command stages these changes, preparing them for a commit. This step is crucial, as Git requires you to specify exactly which modifications you want to include in the next commit. Staging with git add
allows you to control what gets recorded in the project’s history, enabling you to group related changes together for a cleaner, more organized commit history.
Example
$ git add index.html
To stage all changes, use:
$ git add .
4. git commit
After staging files with git add
(meaning you've selected specific changes to track), use git commit
to save them in the repository’s history. Each commit records a snapshot of your files at that point in time, preserving the exact state of your project.
Remember that after you run git commit
, these changes are only saved locally. They won’t be shared with others or saved to a remote repository until you use git push
to upload them. This two-step process ensures that you have control over when your changes are finalized locally and when they’re shared remotely.
Example
$ git commit -m "Add index.html with initial content"
The -m
flag allows you to add a message describing the changes. Make your messages clear and descriptive to keep your history meaningful.
5. git push
As we saw in git commit
your changes won’t be reflected in the remote repository, hence to anyone else using that repository until you push your local changes to the remote origin.
git push
is the command that sends your committed changes to a remote repository, making them available for others.
Example
To push your changes:
$ git push origin main
Replace main
with the branch name as necessary.
Notes:
Typically, you don’t push your changes directly to the main branch. Instead, you create a separate branch to work on features or fixes. This approach keeps the main branch stable and avoids disrupting other collaborators.
-
If you’ve created a new branch and are pushing it for the first time, you’ll need to specify the branch name with the following command:
$ git push -u origin your-branch-name
The
-u
flag sets the upstream branch, so future pushes can be done simply withgit push
, without specifying the branch name.
6. git branch
As we just saw, it’s usually best not to push changes directly to the main or default branch, typically called main. Git allows you to isolate your work from others—or even from your own other work on the repository—by using branches. You can think of a branch as a parallel line of development where you can make changes without affecting the main project. Each branch represents a separate version of the repository, enabling you to work on features or bug fixes independently.
The git branch
command is used to create, list, or delete branches. This command is essential for managing different lines of development in a project and keeping your work organized.
Example
To create a new branch:
$ git branch feature-branch
7. git checkout
The git checkout command allows you to switch between branches or restore files to a previous state. It’s frequently used in a multi-branch workflow, letting you work on different features or bug fixes simultaneously without interfering with each other.
Example
To switch to an existing branch named feature-branch:
$ git checkout feature-branch
The git checkout command has multiple options. For example, if you want to create a new branch and switch to it immediately, you can use the -b
flag:
$ git checkout -b new-branch-name
This command creates new-branch-name
and checks it out in a single step, making it convenient to start work on a fresh branch.
8. git pull
We saw how you can push your changes to a remote repository, but in a collaborative project, you’re usually not the only one working on the code. Sometimes, even you might have worked on a different machine, creating updates that aren’t on your current device.
The git pull
command is used to fetch and integrate updates from a remote repository into your local branch. This command combines two actions: it retrieves the latest changes (using git fetch
) and merges them into your current branch (using git merge
). Regularly pulling changes keeps your local work up-to-date with the remote repository, ensuring you’re working with the latest code.
Example
To pull changes from the main branch on a remote repository (usually origin):
$ git pull origin main
This command fetches the latest updates from the remote main branch and merges them into your current branch. If there are any conflicting changes, Git will prompt you to resolve them before the merge can be completed.
9. git merge
and git fetch
The git merge
command is used to combine changes from one branch into another, making it essential when you’re ready to bring new features or updates into the main branch. This command integrates changes from the specified branch into your current branch, preserving the history of both branches.
Meanwhile, git fetch
allows you to retrieve updates from a remote repository without merging them into your branch right away. This is useful if you want to review changes first before merging, as it brings down updates to your local repository without altering your current branch.
Example: Merging Branches
To merge a feature branch into main, first switch to the branch you want to merge into:
$ git checkout main
Then, merge the changes from feature-branch:
$ git merge feature-branch
If there are conflicting changes between main and feature-branch, Git will prompt you to resolve them before completing the merge. Once you’ve resolved conflicts and saved the files, complete the merge with:
$ git commit
Example: Fetching Changes
To fetch updates from the remote repository without merging:
$ git fetch origin
After running git fetch, you’ll have the latest updates from the remote repository in your local copy, and you can then decide to merge specific branches or review the changes first.
10. git status
and git log
Often, you’ll find yourself needing to check the current state of your branch or review the history of your commits. Git provides two essential commands for this purpose: git status
and git log
.
The git status
command gives you an overview of the current state of your working directory and staging area. It shows which files have been modified, added, or removed, and whether these changes are staged or unstaged. This command is essential for checking the status of your changes before committing them, helping you keep track of which modifications will be included in the next commit (staged
) and also if there are any untracked files that Git has not yet started tracking (unstaged
).
Example: Checking Status
$ git status
The git log
command displays the commit history, giving you a detailed view of each commit’s unique ID, author, date, and message. This command allows you to review the sequence of changes made to the project, making it easier to track the history and understand who made which updates.
Example: Viewing Log History
$ git log
To simplify the log view, you can use git log --oneline
, which condenses each commit into a single line with just the commit ID and message, providing a more streamlined overview of the commit history.
These commands form the foundation of Git and are essential for managing your project’s history and collaborating effectively. Mastering these basics will give you confidence in version control, setting the stage for more advanced techniques.
In the next posts of this series, we’ll dive into some intermediate and advanced Git commands, exploring ways to streamline workflows, manage conflicts, and optimize collaboration even further.
Top comments (0)