Welcome to my blog, in this article, you are going to learn the top 5 git commands every developer should know.
- 5 Git Commands with real world cases
Before moving forward, I want you to promise me. Suppose you learn anything new from this article. You will share this article with at least 3 developers or more.
You can efficiently resolve 5 cases with below git commands list.
Learn them well, by working on a dummy project so, that you’ll get a better idea.
Delete all local branches except master:
The use of this command is beneficial. Whenever your branch merged with the master or dev branch, the local branch becomes useless, in some cases, it could be helpful, but it becomes unnecessary for most of the cases.
To delete all local branches except master:
git branch | grep -v "master" | xargs git branch -D
You can copy and paste, bookmark this article for later use.
Update all local branches to remote:
Sometimes, you could have a case where you need to update your local branches list with the remote origin. Means when you want to fetch the list of remote branches too.
git remote update origin --prune
When you get large file errors at Git:
Sometimes, you are trying to push the images/videos which somehow can exceed the git limit which is 100.00MB and you get the following error:
remote: error: File <path/filename> is 200 MB; this exceeds GitHub’s file size limit of 100.00 MB, and then you crash your mind by searching about git commands to solve it.
In that case, you can use LFS: (if you have a better solution you can comment)
LFS stands for Large File Storage; it’s an open-source git extension for versioning of large files.
It’s simple and straightforward:
Install, track, and add:
git lfs install
Large files or folders you want to track
git lfs track "*.png"
Add all files or .gitattributes:
Your track rules (Means, the files or folders you added into the track, the command save here for future)
You can add all files just like you normally do:
git add . (this will add all the content to the stage for commit)
git add .gitattributes (this will only add .gitattributes)
For more details, visit LFS Official Website.
Reset a local branch to remote origin version:
When you are accidentally working on a branch after pulling remote origin, you find out that you shouldn’t work on that branch. Now, you have made the local changes and have a desire to revert/reset to the remote version. To do that, all you need is to write the following command:
This command will reset your local branch and update it with the remote origin version:
git reset –hard origin/branch_name
There are two cases here whether you want to merge the updated master into the feature branch or feature branch into an updated master.
I only shared the case. You can merge any branch with any branch.
Below is a quick example:
You have two branches, branch A and branch B. Now, you want to merge branch A into B. First, you’ll have to checkout/moved to that specific branch in which you want to combine the changes. In your case, you will checkout into branch B.
git checkout B
After that, we run below git command:
git merge A
Above command will move all the changes from A into B. Just like that you can follow:
Updated master into the branch:
git checkout branch_name
git merge master
Branch into master:
git checkout master
git merge branch_name
Today, you have learned about 5 different git commands which you can start using within your project.
Bonus for you: 12 Parts Series – How to write better code in CSS
You will also love this article: List of elearning websites
Looking for ultimate developers & designers resources?: I’m compiling my list here at: CoderChamp.com