Git can be overwhelming to new developers and they get scared looking at all the git commands. What If I tell you that these 10 commands are all that I’ve used most of the time as a developer in my 9 years of experience?
Of course, there are more git commands which are used sometimes but these 10 git commands are all you need to be a git master.
I’ll be explaining the git commands in layman’s terms rather than advanced technical terms
1. git status
git status is generally used to check the current state of the repo. it shows you modified files, staged files, and the current branch name.
I usually use it for the following tasks
- check the modified files list
- check the branch name
- check if the branch is clean and can pull remote changes
2. git add
git add is used to stage any file or files. Any change that we want to commit should be staged first using the git add command
Stage a single file
git add <path-to-file>
Stage all modified files
git add .
3. git commit
git commit is used to commit your changes when you are ready. Commit the changes along with a message using the -m flag
git commit -m "commit message goes here"
4. git push
git push is used to push your commits to the remote branch
If you get an error that no upstream branch exists in the remote. This is because the branch that you created locally doesn’t exist remotely yet. So, we should push with the -u flag so that the branch gets created in the remote.
git push -u origin branchname
5. git checkout
git checkout can be used to switch from one branch to another branch
git checkout branchname
6. git checkout -b branch
git checkout with the -b flag can be used to create a new branch
git checkout -b branchname
7. git pull
git pull is used to pull the latest changes to your local branch from the remote branch. If someone else has updated the remote branch then you can get those changes in your local branch by using git pull
8. git merge branch
git merge is used to merge one branch into another.
Practical example: Let’s say you are working on a branch and your colleague pushes some changes to another branch and now you need those changes in your branch. Then you can merge that branch into your branch.
First checkout to the branch which you want to merge, let’s say branch1. Make sure that it is up to date by running git pull. After this, check out to your branch again, let’s say branch2. Now you can merge branch1 into branch2 by running the following command.
git merge branch1
9. git reset –hard origin/branchname
Practical example: Let’s say you are working on a branch and made a few commits but haven’t pushed it yet. Now, you realize that your commits are not needed and you just want to reset the branch to be the same as the remote branch. Then, this is how you do it.
First, fetch the remote to make sure that it is the latest by running:
git fetch origin
Now, Reset your branch to be the same as the remote branch by running:
git reset --hard origin/branchname
10. git stash
git stash is used to temporarily remove your changes and store them safely so that the changes can be
Practical example: Let’s say you are working on a branch and made a few changes. Now you want to switch to a different branch and do some other work but you do not want to commit your changes. In this case, you can stash your changes by running:
Your changes will be stashed and kept safely. Whenever you want to get those changes back to your branch. Then checkout to the branch to which you want to get the changes back and run:
git stash pop
This will get your changes back to the current branch.
View changes made to files:
I use VSCode’s source control tool to review my changes to the files before staging and committing them.
Sometimes we might want to discard the changes made to files. I use VSCode for this. Simply right click on any modified file in the source control tab and click on “Discard changes” to discard changes made to a file.
If you want to discard all the changes then right-click on “Changes” and click on “Discard all changes”
There are many commands in git but these 10 commands are what every developer would use 95% of the time ✌🏻
- Flutter lookup failed in @fields error (solved) - July 14, 2023
- Free open source alternative to Notion along with AI - July 13, 2023
- Threads API for developers for programmatic access - July 12, 2023