5 Advanced Git Commands to Level-Up Your Coding Skills
Published in
2 min readNov 2, 2020
Top 5 Advanced Git commands which make a programmer’s life easy.
Git is a widely used version control system. This tutorial will help you to understand 5 Advanced Git commands like git squash, cherry-pick.
1. Squash multiple Git commits into one
- check how many commits you want to squash.
git log -<n>
- rebase head with particular commit.
git rebase -i HEAD~<n>
- change n-1 commit from pick to squash(s).
- save file and again add commit message.
- force push head branch
git push -f origin head
2. Amend a new commit in the last commit
- amend new changes in the last commit
git commit --amend
git push -u origin head
3. Cherry-pick
- Cherry-picking in Git means to choose a commit from one branch and apply it to another.
- Make sure you are on the branch you want to apply the commit to.
git checkout master
Execute the following:
git cherry-pick <commit-hash>
4. Clone a subfolder from the git repo
git init <repo>
cd <repo>
git remote add -f origin <url>git config core.sparseCheckout trueecho "some/dir/" >> .git/info/sparse-checkout
echo "another/sub/tree" >> .git/info/sparse-checkout
This tells git which directories you want to checkout. Then you can pull just those directoriesgit pull origin master
5. Take pull from an upstream forked repository.
git remote add upstream <git@url>
git fetch upstream
git checkout master
git rebase upstream/master
git push -f origin master
You are welcome to follow if you like this article and others so you won’t miss new updates in the future.
More content at plainenglish.io