Git or not git ... lessons learned (WIP)
I call it TDD: tragedy driven development :(
This post is dedicated to VCS mishaps.
A few words ahead:
There are plenty of articles and tutorials on the internet that teach you git,
many of them list commands and tell you what does what. But few of them list the
real-world senario where you fuck up as a beginner. And even less than few
writes from a beginner’s perspective on how they get good and pick up the skill
from trail and error.
This is a someone(me) sharing their suffering (so that tragedies won’t repeat). I’m not trying to (neither am I qualified) to teach you anything. And If you don’t know nothing about git, you shouldn’t start from here.
The takeaway is don’t take anything (tragedy) away. Stay safe.
git reflog
is your final rescue.- Read this page through and follow the examples, carefully.
- Did you read it? Good, now read it again.
- Do yourself (and co-developers, if any) a favor and make commit messages meaningful.
- Do spontaneous/bulky/ambiguous commits the way you like it, but guess who’s crying miserably in the bathroom?
- Don’t ever directly work on the
master/main
unless trivial. - work on your own branch and REBASE YOUR COMMITS before merging/submitting.
- Are you sure you’ve read that page? Now put it in your calendar and read it again next week.
- unless you understand what a git command does, DON’T just run it.
- you need a few good tools to inspect git log and resolve conflicts.
- DON’T use github’s code editor for anything non-trivial.
- Or better, don’t use github to host your repos.
- Or even better: goto sr.ht and also learn to use patches and mailing list.
There is always unknown terror lurking in the codebase. And sometimes when I fuck up, I really fuck up.
pull (torture), merge (blind), and rebase (kill).
It’s taught to use git pull
in many documentations and tutorials… But when
you use it: wait … What the heck is {rebase, fastforward, fastforward-only}?
What’s the difference between merging and rebasing?
First off, git pull
is a shorthand combination of git fetch
and
git {merge, rebase}
. git pull
hides the complexity from user and, if you
simply follow whatever commands that pop up on your screen and just hit
git pull
, without knowing how {merge,rebase} works, tragedy will happen soon
or later.
You and your schoolmates are working on a group project. You have a repository
hosted on github. You did your work and, when you do git push origin master
,
you get the message:
! [rejected] master -> master (fetch first)
error: failed to push some refs to <your-repo>
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
hmmm what does it mean? It means your teammates pushed code to master before you
do! And you are lost … Wait.. there is a hint that I should git pull
! So let
me just do that!
$ git pull
From <your-repo>
08549fd..8d4455a master -> origin/master
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
wtf? What are these? … wait there is another hint! It says
git config pull.rebase false # merge
And I’ve heard about merge! It means I
führe branches zusammen1. Let me do just that.
with pull.rebase false
, you get
From <your-repo>
5313e0e..57cb8fc master -> origin/master
Auto-merging <some-file>
CONFLICT (content): Merge conflict in <some-file>
Automatic merge failed; fix conflicts and then commit the result.
or with pull.rebase true
, you get
Auto-merging <some-file>
CONFLICT (content): Merge conflict in <some-file>
error: could not apply <some-commit>
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase",
hint: run "git rebase --abort".
Could not apply <some-commit>
You think you are fucking up and starting panic2.
rebase or merge
Now spend some time to read this:
https://www.atlassian.com/git/tutorials/merging-vs-rebasing
- Don’t rebase shared branches.
What is what, theirs? ours? local? remote? base?
Driven by Tragedies
Let’s look at stackoverflow’s most upvoted git questions.
https://stackoverflow.com/questions/tagged/git?tab=Votes
Aftertouch:
https://news.ycombinator.com/item?id=25123014
-
german “merge together”, see https://github.com/danielauener/git-auf-deutsch ↩︎
-
I won’t teach you how to fix it here. Instead I’ll write about why you are having this issues in the first place. ↩︎
[+] click to leave a comment [+]
>> SEND COMMENT <<