In this post,I have considered all the common situations which i have come across while working as developer, curated all the commands and listed down according to the situation.
With more than 160 commands available, becoming a git expert takes time. But everyone has a few commands that they use frequently. In this post I’ll share mine.
SITUATION 1:I want a clean working directory, but I don’t want to lose my current changes.
# save the current state of the working directory git stash # apply the latest stashed change to your current working tree git stash pop # list stashed changes git stash list
SITUATION 2: I messed up my last commit message.
# change the latest commit's message git commit --amend -m "Fixed message"
SITUATION 3: I accidentally pushed a file. Now I want to remove it from the remote branch (but not from my local branch).
git rm --cached <file-path>
SITUATION 4: I made a bunch of changes to a file but I regret it 😱. I want to go back to the main branch version.
git checkout main <file-path>
SITUATION 5: I want to take a commit from one branch and apply it onto mine.
git cherry-pick <commit-hash>
SITUATION 6: I want my branch to be based off the most recent commit of the main branch.
# 1. Get the latest version of the main branch git checkout main git pull origin # 2. Checkout your branch git checkout <my-branch> # 3. Replay your changes on top of the main branch git rebase main
SITUATION 7:I merged my branch into main but I want to rollback the merge
# 1. Checkout a new branch from the main branch git checkout -b mybranch-rollback # 2. Find out the commit hash that merged your branch to develop. git log # 3. Revert the merge commit git revert -m 1 <commit-hash> # 4. Push the changes to your new branch git push origin mybranch-rollback # 5. Open a pull request to main for your new branch
If you liked it, follow me on github
Would love to hear from you which commands you use daily so that i get to learn all those as well.
Comment down below 👇
Top comments (0)