10 Most Essential Git Commands: Must-Have Knowledge for Developers
Hey there, fellow coding enthusiasts! If you're anything like me, you know that version control is the backbone of efficient and organized software development. When it comes to version control, Git reigns supreme.
Intro
Git is a powerful tool that allows you to track changes, collaborate with others, and maintain code integrity effortlessly. In this blog post, I am going to share with you the 10 most essential Git commands that everyone, from beginners to seasoned developers, should know. These commands are the building blocks of a smooth Git journey.
1. git init
The journey begins with git init
. This command initializes a new Git repository in your project directory. It's like giving birth to your code, creating a clean slate for Git to work its magic.
git init
2. git clone
Need to grab a copy of a remote repository? The git clone
command is your go-to. It creates a local copy of a remote repository, ready for you to work on.
git clone <repository_url>
<repository_url>
should be replaced with the actual URL of the repository which you want to clone. e.g. git clone
https://github.com/sazidthe1/100DaysOfCode.git
3. git add
Before you commit changes, you need to tell Git which files to include in the next commit. git add
stages change for commit. There are two commands for adding files.
Single/specific file:
git add <file_name>
<file_name>
should be replaced with your actual file name. e.g. git add day1.py
Multiple files:
git add <file1> <file2>
<file1>
and <file2>
should be replaced with the actual names of the files you want to stage. e.g. git add requirements.txt project.py
All the changes:
git add .
4. git commit
Once your changes are staged, it's time to make a commit. Think of commits as snapshots of your code at a specific point in time. Each commit comes with a message that describes the changes you made.
git commit -m "Your commit message here"
5. git pull
Working in a team? Use git pull
to fetch and merge changes from a remote repository into your local branch.
git pull
6. git push
On the flip side, git push
is used to send your committed changes to a remote repository. It's essential for collaboration.
git push
7. git branch
Branches are incredibly useful for working on different features or fixing bugs independently. git branch
helps you manage and create branches.
git branch <branch_name>
8. git checkout
To switch between branches or specific commits, you can use git checkout
. It's like teleporting between different versions of your project.
git checkout <branch_name or commit_hash>
9. git merge
When you're done with a feature or bug fix, it's time to merge your changes back into the main branch. git merge
helps you do just that.
git merge <branch_name>
10. git log
Curious about the history of your project? git log
provides a chronological list of commits, including commit messages, authors, and timestamps.
git log
And there you have it, the top 10 essential Git commands! Mastering these commands will set you on the path to becoming a Git pro.
But remember, Git is a vast and powerful tool with many more features to explore. Here are some additional Git commands with their respective code snippets:
git status
: Check the status of your working directory and see which files are modified, staged, or untracked.git status
git diff
: View the changes made to your files before staging or committing them.git diff
git checkout -b
: Create and switch to a new branch in one command.git checkout -b <new_branch_name>
git remote
: List all remote repositories associated with your local Git project.git remote -v
git fetch
: Download changes from a remote repository but don't automatically merge them.git fetch
git stash
: Temporarily save your changes and revert your working directory to the last committed state. Useful for switching branches without committing unfinished work.git stash
git cherry-pick
: Select and apply specific commits from one branch to another.git cherry-pick <commit_hash>
git rebase
: Reorganize the commit history by moving, squashing, or editing commits interactively.git rebase -i <base_commit>
git reset
: Unstage changes or move the HEAD pointer to a different commit, without creating a new commit.git reset <commit_or_file>
git tag
: Create, list, or delete tags for specific commits, making it easier to reference important points in your history.git tag <tag_name> <commit_hash>
These additional Git commands will help you become more proficient in managing your version-controlled projects. As you continue to learn and use Git, you'll find these commands invaluable for your daily development tasks.
Resources
For further reading and learning, here are some excellent resources:
Official Git Documentation: The official Git documentation is a comprehensive guide to Git's features and commands.
Git Cheat Sheet by GitHub: GitHub offers interactive courses on Git and GitHub, providing hands-on experience in a practical context.
Git Cheat Sheet by Atlassian: Atlassian's tutorials cover Git concepts and commands in a straightforward and user-friendly way.
Tutorial (Hands-on practice)
In English: Git and GitHub for Beginners Crash Course
In Hindi: Complete Git and GitHub Tutorial
In Bangla: Crash Course on Git and GitHub
So, grab your favorite text editor, fire up your terminal, and start your Git journey today. Happy coding!
Subscribe to my newsletter
Read articles from Sazidul Islam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Sazidul Islam
Sazidul Islam
I love things around numbers, helping people, and discovering stories in data ๐ข๐ค๐