In this article, you will learn the most helpful Git commands that will take you to the next level in development:
1. Git Init
The git init command is usually the first command you’d run in any new project that is not already a Git repository (also commonly called repo).
It can be used to convert an existing, un-versioned folder into a Git repository. Also, you can use it to initialize an empty repo.
$ git init
2. Git Clone
The git clone command is used to download the source code from a remote repository (like GitHub, Bitbucket, or GitLab).
$ git clone <https://url-of-the-repository>
When you clone a repo, the code is automatically downloaded to your local machine.
3. Git Branch
Branch is one of the most important functionalities of Git. This allows teams to work on the same code base in parallel.
$ git branch <branch-name>
4. Git Checkout
A mistake I often made when I first started learning git commands was forgetting to switch to the new branch I just created. Yes, after you create a branch, you’ll have to switch to it with another command. That’s where the Git Checkout command comes in.
$ git checkout <branch-name>
If you’re as lazy as I am, I’m sure you’d want one single command that will both create a new branch and automatically switch to it.
$ git checkout -b <branch-name>
5. Git Add
Every time you create a new file, delete it, or make a change, you’ll have to tell Git to track it and add it to the staging area. Otherwise, the files you made changes to wouldn’t be added when you try to push your changes.
$ git add <file-name>
This command will add only a single file to your next commit. If you want to add all the files to which changes were made, you can use
$ git add -A
6. Git Commit
Think of Git Commit command like a checkpoint in your development process.
Every time you commit your code changes, you’ll also include a message to briefly describe the changes you made. This helps other team members quickly understand what was added, changed, or removed.
Using Git Commit Command
$ git commit -a
7. Git Push
To make all your committed changes available to your teammates, you’ll have to push them to the remote origin.
$ git push <remote> <branch-name>
It’s important to remember that git push command will upload only the changes you’ve committed.
8. Git Pull
Of course, you’d want to have the latest updates from teammates as well!
The git pull command allows you to fetch all the changes that your teammates pushed and automatically merge them into your local repo.
Using Git Pull Command
$ git pull <remote>
9. Git Diff
Git Diff is my go-to command when I want to quickly see the difference between my current branch and another branch (usually the branch I’m merging into).
Using Git Diff Command
$ git diff
To compare two branches
$ git diff branch1..branch2
To compare a file from two branches
$ git diff branch1 branch2 ./path/to/file.txt
10. Git Stash
Git Stash temporarily shelves your work, so you can switch to another branch, work on something else, and then come back to this at a later time.
It’s perfect if you need to work on something else and you’re midway through a code change, but aren’t ready to commit the code.
$ git stash save “<stash-message>”
Thanks, for reading the blog, I hope it helps you. Please share this link on your social media accounts so that others can read our valuable content. Share your queries with our expert team and get Free Expert Advice for Your Business today.
Hire me on UpWork
Hire me on Linkedin