In this article, we're breaking down 35 fundamental Git commands that every developer must know. These commands will empower you to establish a repository, make commits, spawn and switch branches, and so much more. By getting a grip on these commands, you'll be well on your way to evolving into a more agile and efficient developer.
Version control is the beating heart of software development. It's what allows you to monitor code alterations, roll back to earlier stages, and work seamlessly with your team on a project. Git, one of the most acclaimed version control systems, is at the forefront of this. Both rookies and seasoned developers stand to benefit immensely from mastering Git commands, a proficiency that can streamline your workflow like never before.
So, without further delay, let's dive right in! Here are our handpicked 35 most useful Git commands to turbocharge your coding workflows.
Also Read: Essential Python Tips for Developers
1. git init
The command “git init” is a nifty tool used to kick-start a new Git repository. It generates a fresh .git subdirectory in your current working directory, simultaneously creating a new branch named ‘master'.
Executing this command will initialize a Git repository in your current directory.
2. git add
This command is used to add a file to the staging area, setting the stage for a commit.
git add <filename>
Executing this command will add the file named “filename” to the staging area. You also have the option to utilize “git add .”, which conveniently stages all files for you.
3. git status
This command is your status update, showcasing the status of changes as untracked, modified, or staged.
Executing this command will reveal the status of your working directory.
4. git commit
This command is a time-capsule, storing your changes to the local repository. It captures a snapshot of the changes you’ve readied using git add.
git commit -m "Commit message"
Executing this command will commit your changes with a message that elucidates what you altered.
5. git clone
This command is a lifesaver when you need to clone a repository. It replicates a remote repository on your local machine.
git clone https://github.com/<username>/repo.git
Executing this command will clone the repository at the provided URL to your local machine.
6. git checkout
This command is your teleporter, helping you switch between branches in a Git repository.
git checkout <branch_name>
Executing this command will switch to the branch named “branch-name”.
7. git pull
This command is akin to an email fetch; it fetches changes from a remote repository and integrates them into your current branch.
git pull origin master
Executing this command will pull changes from the master branch of the origin remote repository.
8. git push
This command acts as a courier, transporting your committed changes to a remote repository.
git push origin master
Executing this command will push your committed changes to the master branch of the origin remote repository.
9. git branch
This command serves as your repository directory, listing all of the branches in your repository.
Executing this command will list all of the branches in your repository.
10. git merge
This command is the binder that melds changes from one branch into another.
git merge <branch_name>
Executing this command will merge the changes from the branch with the specified name into the current branch.
11. git diff
This command is the inspector, showing the file differences which are not yet staged.
Executing this command will display unstaged differences since the last commit.
12. git rm
This command is your trash bin, deleting the file from your working directory and staging the deletion.
git rm <filename>
Executing this command will delete the file named “filename” and stage the deletion.
13. git reset
This command is the reset button, unstaging the file, but preserving its content.
git reset <filename>
Executing this command will unstage the file named “filename“.
14. git tag
This command is your label maker, tagging the specified commit.
git tag <tagname>
Executing this command will tag the latest commit. This command is mostly used to add version: “v2.1”.
15. git log
This command is your history book, displaying a catalog of commits on a branch along with corresponding details.
Executing this command will yield an ordered list of the recent commits.
16. git show
This command is your microscope, revealing the metadata and content alterations of a specified commit.
Executing this command will display the metadata and content changes of the latest commit.
17. git revert
This command is your undo button, creating a new commit that undoes the changes made in a previous commit.
git revert HEAD
Executing this command will create a new commit that undoes the changes made in the last commit.
18. git fetch
This command is the retriever, fetching all the objects from the remote repository that are not present in the local one.
git fetch origin
Executing this command will fetch all objects from the origin remote that aren’t present in your current repository.
19. git rebase
This command is your time traveler, applying the changes made on the current branch ahead of another branch.
git rebase master
Executing this command will apply any changes made on the current branch ahead of the master branch.
20. git stash
This command is your storage locker, temporarily stashing changes that you don’t want to commit right away. You can retrieve and apply the changes later.
Executing this command will temporarily save all modified tracked files.
21. git stash pop
This command is your pop-up reminder, restoring the most recently stashed changes.
git stash pop
Executing this command will apply the most recently stashed changes and remove them from the stash list.
22. git stash drop
This command is your shredder, discarding the most recently stashed changeset.
git stash drop
Executing this command will discard the most recently stashed changeset.
23. git stash list
This command is your notepad, listing all stashed changesets.
git stash list
Executing this command will list all stashed changesets.
24. git grep
This command is your search engine, allowing you to search through your repository.
git grep "<search_term>"
Executing this command will trawl through the repository for any instances of search term that you entered.
25. git cherry-pick
This command is your cherry-picker, applying the changes introduced by certain existing commits.
git cherry-pick <commit_id>
Executing this command will apply the changes introduced by the commit with the provided ID.
This command is your portal, launching the Git repository browser.
Executing this command will initiate the Git repository browser.
27. git bisect
This command is your bug detector, utilizing a binary search algorithm to identify which commit in your project’s history introduced a bug.
git bisect start git bisect bad git bisect good <commit_id>
Executing this command will initiate the bisecting process, mark the current commit as bad, and tag the commit with the given ID as good.
28. git blame
This command is your auditor, revealing what revision and author last modified each line of a file.
git blame <filename>
Executing this command will display what revision and author last modified each line of “filename”.
29. git reflog
This command is your library catalog, showing a list of all references to commits in the local repository.
Executing this command will list all references to commits in your local repository.
30. git clean
This command is your janitor, expunging untracked files from your working directory.
git clean -n
Executing this command will demonstrate what will be removed without actually doing it. Replace -n with -f to actually remove the files.
31. git mv
This command allows you to relocate or rename a file, a directory, or a symlink.
git mv <old_filename> <new_filename>
Executing this command will rename the oldfile to newfile name. You can use “-f” to force the renaming or moving of a file.
32. git mergetool
This command launches your preferred editor to help you resolve conflicts in a more intuitive and user-friendly manner.
Use this to resolve conflits in your repository.
33. git merge.tool
This command sets your chosen editor, in this case vimdiff, as the default tool for managing merge operations.
git config merge.tool <vimdiff>
Use this to resolve conflict in a user-friendly way uisng any merge tool of your choice.
34. git config –list –show-origin
With this command, you can observe all of your configured settings.
git config --list --show-origin
Use this to verify all settings and configurations variables set.
35. git help
Use this command if you're finding it challenging to recall specific commands or command options; it's here to aid you.
git help <command>
You can use “git help –al” to view a comprehensive list of all available Git commands..
Harnessing the power of Git can be a game changer in your developer journey. These 35 fundamental Git commands, while just a glimpse into the vast world of Git, can substantially boost your productivity and efficiency. By regularly employing these commands, you'll not only streamline your workflows but also foster better collaboration on your projects. It may initially appear like a steep learning curve, but with consistent use and practice, you'll soon recognize the immense value Git brings to your coding table.
So why wait? Start exploring Git today and take your coding prowess to new heights! Want more coding tips and insights? Don't miss out on any updates – follow us on Twitter today!