Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
This standard definition tell you the power of git and most of us have seen it while using it our projects.
But if anyone of you haven’t used it yet, so its a right time to get your hand dirty — https://devdocs.io/git/
“Imagining a life without you is something that is impossible, you make me complete and I want you to say you mean everything to me.-”
— Most of the developers using git
This article will mostly cover the challenges and the solution to our struggles with git. If below problems sounds familiar to you, then this article is for you -
You committed something and immediately realised I want to make one more small change.
You accidentally committed something on another branch (say master)
Nothing shows in diff command
You want to squash multiple commits into one
You did something terribly wrong and want to time travel your git life cycle
-
You wanted to know who the hell have written this code
Git is hard: screwing up is easy, and figuring out how to fix your mistakes is fucking impossible.
— mSingh
I know most of above problems are very much familiar to you. So let’s dive into their solutions.
1. You committed something and immediately realised I want to make one more small change.
*# make your change*
**git add .** *# or add individual files*
**git commit --amend**
*# follow prompts to change or keep the commit message
# now your last commit contains that change! *
2. Nothing shows in diff command
**git diff --staged**
3. You want to squash multiple commits into one
*#This will squash last 3 commits into single commit*
**git reset --soft HEAD~3 &&
git commit**
4. You did something terribly wrong and want to time travel your git life cycle
**git reflog**
# you will see a list of every thing you've done in git, across all branches!
# each one has an index HEAD@{index}
# find the one before you broke everything
**git reset HEAD@{index}**
# magic time machine
5. You need to change the message on last commit
**git commit --amend**
# follow prompts to change the commit message
6. You wanted to know who the hell have written this code
# last commit by on each line**
git blame -l <filename>**
# last commit by between lines in a file**
git log -L55,60:file.c**
7. You accidentally committed something on another branch (say master)
Solution by creating new branch —
*# create a new branch from the current state of master*
**git branch -b some-new-branch-name**
*# checkout to master and remove the commit from the master branch
***git checkout master***
***git reset HEAD~ --hard**
**git checkout some-new-branch-name**
*# your commit lives in this branch now :)*
Solution using stash
# undo the last commit, but leave the changes available
**git reset HEAD~ --soft**
**git stash**
# move to the correct branch
**git checkout name-of-the-correct-branch
git stash pop**
**git add .** # or add individual files
**git commit -m "your message here"**
# now your changes are on the correct branch
Solution using cherry-pick
# take that commit to another branch and remove it from master
**git checkout name-of-the-correct-branch**
# grab the last commit to master
**git cherry-pick [master- SHA]**
# delete it from master
**git checkout master**
**git reset HEAD~ --hard**
The last and most important — Fuck this noise, I give up.
*cd ..
sudo rm -r giveup-git-repo-dir
git clone https://some.github.url/giveup-git-repo-dir.git
cd giveup-git-repo-dir
*# Delete the git repo and clone the fresh repo from remote origin
Wrapping up
Woo! That’s all about most popular git problems. Stay tuned to my future articles.
Was this article helpful for you? Let me know in the comments below if you have any questions or thoughts! I’d love to hear them :)
Thanks for reading. Did this article help you in any way? If I did, I hope you consider sharing it you might just help someone who felt the same way you did before reading the article. Thank you.
Sharing makes you bigger than you are. The more you pour out, the more life will be able to pour in.
Top comments (0)