713 points · by emilong · 10y ago · 335 comments
I understand that people need to know how to use their tools, but for git most people can get away with the very basic usage that GUIs provide. If you've made some unrecoverable mistake with an important set of changes, you can always review the history in the same GUI and reimplement the important changes in a new branch.
I love this tool -- it's honestly one of the best graphical tools I've ever used -- rock solid reliable -- intuitive, not leaky in the abstractions it represents -- and not limited to only "simple" operations. And with the graphical reflog history rollback view -- it's always really easy to back up however far you want if you mess up which gives great confidence as you explore the ui's features. Ive taken designers from "never used git" to confidently pushing, pulling, rebasing, and resolving conflicts in less than a day (and most of it after the first 10 minutes without outside help).
Clicking on a commit and pressing space brings up a view showing a github-style diff, a list of all changed files, and the complete commit message. Nice.
Thanks for the tip!
I had to do a site visit with a customer recently and spent some time pair programming on their codebase -- on a Windows machine and they used an awful git client -- really reminded me how much I appreciate using gitup!
Seems like there's a glut of these apps of late—not that I'm complaining! :-)
git is basically a brilliant repo model combined with an insane cli. gitup is the brilliant ui that the repo model deserves.
bonus awesome thing about gitup: it monitors the filesystem and updates its state in realtime, so it it's seemless to mix your workflow between cli and gui.
I used to use a GUI for git, but now I just find them cumbersome; by their nature requiring me to switch window for less.
Honestly I'm not aware of any common repo tasks that I could more efficiently complete via the command line vs gitup. There are good keyboard shortcuts for everything in gitup so most everything I do doesn't even involve the mouse.
Also -- I'm the kind of person who's always got a set of terminal windows open and definitely uses them often (almost never use Finder, hate taking my hands off the keyboard to reach for the trackpad). At this point -- I never reach for the git cli over gitup (except when cloning a new repo).
I use magit myself for most things.
Every one of the tasks in the article is a simple, straightforward operation in SmartGit. Take "I accidentally committed something to master that should have been on a brand new branch!" or "I accidentally committed to the wrong branch!" for example.
Instead of an obscure series of four to six commands, you simply go to SmartGit's log view and drag the branch markers to where you want them. Easy and intuitive.
I know GUI tools are a hard sell for a lot of programmers. I honestly don't understand why. I think it may be the fear that you'd be giving up power, but SmartGit isn't a dumbed-down GUI. It gives you a more powerful set of tools, and much more visibility into the state of your repo.
You're not even giving anything up. You can still use the command line whenever you want, and SmartGit shows you the commands it used to accomplish each task.
Of course you still need to understand the underlying Git concepts to use any GUI effectively, but a tool like SmartGit lets you see those concepts.
No thanks.
I mainly use the GUI for day-to-day stuff. Only in rare cases will I need to mess with the command line.
I always use git on the command line. Regarding GUI tools I like to avoid them, for me is too much noise. People get lost most of the time because they don't know what the tool is doing. After all is just one more layer on top of git core tool per se (aka, what you have in command line).
Disclaimer: I'm now a GitLab authorized training partner and reseller. The community edition is free. :)
If you're used to the commands from the keyboard, where you're likely already typing from your editor...can't imagine going back to the GUI.
# Make correcting change
git commit --all --fixup=<ref of commit with mistake>
# Continue working on branch, then at some point
git rebase --interactive --autosquash
The --fixup option creates commits with subjects formatted like 'fixup! previous commit subject'. --autosquash uses these subjects when building the interactive rebase list.Handy enough that I set rebase.autoSquash to true in my ~/.gitconfig so 'git rebase -i' always works like this.
Yeah, true. I posted it mostly because one of the bigger threads of comments was advocating using a GUI instead of the CLI. I'm not going to argue that the git CLI is simple, but it does have some nice features to support specific common workflows.
last_commit=$(git log --oneline | head -1 | cut -d' ' -f1)
git commit -a --fixup ${last_commit}
GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash ${last_commit}~1I would like to understand what's the yearly damage of such an important tool being so difficult to use. People committing the wrong stuff, unmergeable messes, people not being able to correct their mistakes, there must be thousands of Git rookies fucking up their Git repo or throwing away their work just as I am writing this.
What would be the cost? Millions of Dollars? Perhaps even billions?
It's about as bad as 0 being a legit value for a regular pointer.
$ hg clone https://bitbucket.org/eigen/eigen/
$ cd eigen
$ time hg grep CUDA > /dev/null
real 0m16.661s
user 0m16.097s
sys 0m0.531s
$ git clone https://github.com/RLovelett/eigen.git
$ cd eigen
$ time git grep CUDA > /dev/null
real 0m0.019s
user 0m0.035s
sys 0m0.057s
Never looked back. $ time hg grep --rev tip CUDA > /dev/null
real 0m0.075s
user 0m0.055s
sys 0m0.020s
Much better. Still much slower than git, which doesn't matter for this small repo but would definitely matter on a larger repo. But better. And hg lets you define aliases, so you don't have to remember to type "--rev tip".* Network effect. Almost any dev can use git, so it's our "common language". * Staging area. Mercurial doesn't have one, which I find is enough to make me never look back.
$ time taskset -c 2 hg grep CUDA >/dev/null
taskset -c 2 hg grep CUDA > /dev/null 25.04s user 0.46s system 99% cpu 25.514 total
$ time taskset -c 2 sh -c 'git grep --cached -l CUDA | xargs -n 1 git blame >/dev/null'
taskset -c 2 sh -c 4.85s user 0.13s system 97% cpu 5.097 total(End Sarcasm).
Seriously? You're so productive that the cost of cloning a repository is a major impediment?
The benefits of a cleaner interface mostly trump the benefits of speed.
Mercurial has a clean and simple UI, especially if you come from a SVN background.
But Git's internals are simpler and the concept is much easier to understand, especially if you start looking at a handful of the most popular Mercurial extensions, some of which will be "must have" sooner or later.
This is no excuse for an inconsistent UI in Git, but the damage is already done. There's no point in making major changes now (some renaming for consistency might be alright, though), it would be breaking existing workflows and scripts.
I find the best explanation of how Git works is... the first commit of Git[0] itself.
[0] https://github.com/git/git/commit/e83c5163316f89bfbde7d9ab23...
I'm only an occasional Git user, but am a heavy Mercurial user.
I have never needed to understand Mercurial's internals.
Are you suggesting a Git user will eventually need to understand it? That's a strike against Git already.
From my observation over the years, people who use Git get "stuck" more often, because they are trying to leverage the extra power Git seems to provide. With Mercurial, you often can do similar powerful stuff, but they make sure you have to go through hoops to do it. As such, unsuspecting non-power users do not shoot themselves in the foot.
There are A LOT of mercurial users which are big companies, they just aren't so vocal on the internal like people using GIT. Mercurial has an established community, great plugability system, an awesome feature like phases.
We see more and more people adopting Mercurial together with our product.
I wish to see more advocates of Mercurial, it's a shame it doesn't get more public attention nowadays.
The problem, that I don't think anybody even expected, is that there are strong network effects on the selection of your CVS. That gives the tool that can handle more use cases a big advantage, and bigger still if those features are needed on the projects with more developers.
Certainly, it's got a lot of different switches and stuff, but since its data model is so simple, you can always understand what it's doing to your data. So you can read a description of what a switch does, and then understand exactly what will happen when you use it.
(The exception to this is automatic merge conflict resolution with rerere, which is just magic to me. But it's a bonus feature anyway)
For me, as a long time git user, git's UI and UX is approximately a bajillion times better than something like SVN, which can barely even do half the things that I use every day in git.
Maybe I haven't just dug deep enough into Subversion's manual, but can you tell me how to do the equivalent of "git log -p"? Last time I checked (SVN 1.8) that's not very trivial. Or how to revert a commit? IIRC that requires a merge.
So at least compared to subversion, git enables a workflow that was previously too much work, and gives me a better tool for working with version-controlled text data.
Mercurial is another thing I find puzzling, and I'm honestly interested why it is that people find it so much easier to use than git. I'm so used to working with throw-away branches that hg's default branching model just feels needlessly difficult to me, and good support for managing branches is the most important feature of a VCS, since any change you ever make is automatically a branch from the base. In git, this feels natural, since branches are a feature of the underlying data model, not something treated specially.
svn log --diff, added in 1.9. Also doable with a little (est. < 10 lines) of shell scripting.
> Or how to revert a commit? IIRC that requires a merge.
git and svn disagree on whether history should be permanent or not.
Non-distributed VCS clearly don't cut it for many use cases, and of the other DVCS I've tried, none were really better than Git in my opinion.
Sure, it depends on your workflow. But if part of your workflow is authoring sequences of commits that make sense in hindsight, then I honestly haven't seen an alternative.
The point of Git is that it gets the underlying data model right, and just exposes that. Once you grok that, it is superior to everything else.
If you don't grok it, you have a problem. For this reason, I think GUIs are actually somewhat detrimental for Git use. git gui and gitk serve a purpose, and they could definitely need some polish, but my other explorations into Git GUIs have basically turned out to be badly done and leaky abstractions which hurt more than they help.
(Not to mention the fact that Perforce as I know it simply doesn't cover the distributed use cases that Git does. Obviously adding distributed features makes any VCS more complex, but that is the inherent complexity of the problem space as opposed to artificial complexity. If your problem is conceptually easier, you can make the solution appear easier.)
I'm still at uni (at a highly ranked but actually crap university where we don't learn git properly) and this year was my 'year in industry' as we call it in the UK, and my first proper experience with git, aside from `git init` at the end of my project and pushing it to a repo.
I've become so much more confident with git. Seriously, with one caveat (i.e., you haven't pushed your changes to a branch which other developers are working on), it is almost impossible to break irrevocably. Even if you do accidentally break master/develop/whatever, it only causes a bit of hassle and grumbling.
Highly recommend that everyone take a bit of time to learn about "undoing" git commands, whether that's through soft resets, hard resets to origin, or the reflog.
Reflog is also useful for figuring out how someone else broke something and explaining what they did wrong, since you can see what branch they were on at what commit and what commands they ran.
I think git's main problem is the somewhat arcane language it uses, and lack of understanding of what's actually happening behind those words like "rebase", "commit", "patch", "reset" etc.
But not how to use the tool.
We were told to version control a group project, but all we were given was an SVN repo and told "if anything goes wrong, email this address".
Personally I would expect tuition through the use of a tool, that tool doesn't have to be git, it just so happens to be the tool I've had the most exposure to.
a) Explain the value of version control and actively promote its usage
b) Drop some hints on which good version control systems are out there, one of which is git, then let students pick, learn and run with their own choice
It'd take all of half an hour, with great benefits for students.
This tells me you still don't get the point about using a revision control system: the whole point is NOT that you can hack the revision control system's database every which way, but that you can never, as in ever, break something in a repository, because you can always revert the previous commit and do a new commit of that revert. Keeping it simple since January 1st, 1970.
It's called the "unceremonious revert", popularized by Jeff Bonwick, the father of ZFS and former Solaris gatekeeper, in the "Quality death spiral":
Bonwick was granted authority to "rip it out if itʼs broken" — an early BDFL model, and a template for later generations of engineering leadership.
https://web.archive.org/web/20091028095830/http://hub.openso...
https://wiki.smartos.org/display/DOC/Community+History
SEE ALSO
http://dtrace.org/blogs/bmc/2015/09/03/software-immaculate-f...
...and do youserlf a favor, try out Bitkeeper (it's open source!) before you get completely absorbed into git. You might never be able to get out of that tar pit afterwards, or it might be decades before you realize it. Who will compensate you for all that lost time, and how would you be compensated?
@mathieuh was only referring to not being able to break the something in the repo, He didn't mention hacking the git or the git data structure.
Also, @mathieuh is a uni student, so comments like
> This tells me you still don't get the point about using a revision control system
Can come across a little unnice.
See also 'svn obliterate'.
What this does is check what's on the remote branch and compare it with what you think is on the remote branch, and only do the force if they're the same thing. So if someone pushes a commit, the force push errors out instead of silently overwriting it.
Basically every single time you want to force push, you probably should be doing a `--force-with-lease` instead. I can't think of a situation where you'd want to silently lose commits you don't know about rather than get an error.
By my reckoning, rewriting history just isn't a reasonable decision in shared branches.
git revert <bad commit>
git push
It leaves a history of the mistake, for better or worse, but it does undo the mistake on origin.Simple, completely linear history for origin/master is just so powerful in many that aspects that I'm constantly baffled why merging seems to be the flow mostly being talked about.
Now someone usually comes and says that merges are superior for long running branches. Which may be true in some aspects, but when you have more than one "running" branch and you got to try and find which commits have the code changes that only together seem to break and this is after 2 non-trivial merge commits, you really start to wish you'd gone with rebase.
But does anyone know why this project has two homes?
Also, I think everyone should go through Neo's Git Immersion at some point early in their experience with git. It is the best crash course by far.
git tag tmp
git perform-stunt
This eases undoing the stunt without needing to find the "before" state from reflog. And if you use a graphical log viewer (I like SourceTree on Mac) you'll see the tagged state in the history view - which makes things a lot clearer.And to be aware what happens, there one single explanation of git that helps a lot: http://eagain.net/articles/git-for-computer-scientists/
As soon as you start viewing git as a graph of nodes with branches/tags just being "marked" nodes a lot of things make sense, and whatever "git perform-stunt" you attempt it's easy to explain within that mental model.
Other ways to do it (that don't require to retype the commit message): - rebase onto the correct branch:
git branch foo
git reset --hard HEAD~
git rebase --onto name-of-the-correct-branch HEAD foo
git checkout name-of-the-correct-branch
git merge foo
git branch -D foo
- cherry-pick git reset --hard HEAD~
git checkout name-of-the-correct-branch
git cherry-pick name-of-the-branch-you-mistakenly-committed-to@{1} (or git cherry-pick HEAD@{2})
> Oh shit, I tried to run a diff but nothing happened?!You probably want to know `git diff HEAD` too.
Edit: formatting.
git checkout -b actual-branch
git branch -f master origin/master git branch name-of-the-correct-branch
git reset --hard HEAD~
git checkout name-of-the-correct-branchThe thing is, some repositories on github require you to commit only after proper rebasing. But I can never in my life remember how and need to google it...
git merge --abort
To abort a screwed up rebase: git rebase --abort
Both of these will take you back to immediately before you began the operation.Been there too, with the same result.
> Any points on how to continue from there?
Honestly? Not always from the command-line, unfortunately. Basically, I just don't know the right commands to type on the command-line for everything I do with git, and it's not necessarily easy to find them either. I use the TortoiseGit GUI, and over there, it's a LOT more easy to see what is going on and to abort or continue rebases/merges as necessary, or to do fancier stuff. But the thing is, because I understand git and I know what should logically happen at every step, I can cherry-pick commits, revert them, abort/continue merges, rebase commits, or do whatever else the heck I want without getting into trouble anymore... even though I don't know how to do many of them on the command line.
Of course, it took a lot of suffering to get here. Starting with the GUI would of course be the wrong approach. But once you've understood what's going on, then I would recommend you entirely ditch the command line and switch to something like TortoiseGit (I hope you're either on Windows or your platform has something as good as that). The GUI actually shows you what your options are at every step, so you don't have to know all the valid commands at every possible state. You just need to know what effect you need to cause.
Running it gives you a log of all the things `HEAD` has ever pointed at, meaning you can find what you had before a rebase and check that out.
git reflog
git checkout HEAD@{10}
And you're usually good to go.People use rebase in a far too casual way. Rebasing is a pretty advanced use case, one that is unnecessary for normal git usage. You sentence sounds like: I stopped trusting Ford because the car stopped working when I reprogrammed the ECU.
In short: If you are not entirely comfortable with git's inner workings, do not use rebase. Do not complain if you shoot yourself the foot with rebase.
So that example should have been:
cd ..
mv fucking-git-repo-dir fucking-git-repo-dir.archived.$(date +%s)
git clone https://some.github.url/fucking-git-repo-dir.git sudo rm -rf /some/path --no-preserve-rootFor example, the last "bad situation" I had to get myself out of involved unreadable .git contents caused by filesystem corruption. If you can "rm -rf fucking-git-repo-dir" then it's not too bad; when that fails with an IO error is when things get interesting!
> git reset HEAD~ --hard
What is ~ after HEAD? What is --hard? Is there a --deep option as well?
So I think that you could upgrade this with some annotations over the cryptic parts with a little explanation. What do you think?
That is I believe `git stash` should be removed from git as evil data loosing feature, not needed. Instead just make an alias `git save <TEMP_BRANCH_NAME>` which saves your temporary work to the branch:
`save = !sh -c 'export PREV=$(git symbolic-ref HEAD|cut -d/ -f3-) && git checkout -b "$1" && git commit -am "$1" && git checkout "$PREV"' -`
"Applying the state can fail with conflicts; in this case, it is not removed from the stash list. You need to resolve the conflicts by hand and call git stash drop manually afterwards."
Or do I misunderstand you?
If you don't then there are ry only two things you need to know how to do:
If you didn't push to origin do an ammend. If you did, revert soft and commit the previous code to revert it (you can also put a stash or patch to apply it back).
Which frankly is what the article does, basically.
You need to expand on this because i don't quite understand what you mean, are you talking about "fix typo" commits or are we getting to the level of just committing away until things work and then not cleaning up the work later on? The linked article doesn't cover rebasing or squashing commits, which can be pretty powerful when used correctly.
Your commit log is the one thing that is immutably linked to your code changes. Your documentation isn't, the comments in your code aren't, you will forget why you made a change, anyone reviewing your logs needs to understand your intention, your bug tracker will probably change several times over the life of a project, and so on. So, make an effort to have a commit log that is clean and comprehensive and commits that don't break tests.
I go into this in more detail in a talk i gave last week: https://www.youtube.com/watch?v=9OHAq8dCoS4
You get your cake and you can eat it too.
Be verbose in your commit messages and don't worry about merge commits, it will pay dividends later.
Yes, that sounds as easy as anything that accepts the idea of "let's do this the shitty-but-quick way now, and pay the technical debt later". So yes, it is "easier" to work with in the beginning.
On the other hand, if I had to inherit your repository that was developed by "not caring about history", I would probably have to hunt you down and kill you. So there's that.
Yep. Some people just want to keep taking technical debt on (read: hack-it-'till-it-works), which makes them likely candidates for a good thrashing in some dark blind street in the middle of the night. And some don't even get it after that. Actually, now that you made me think about it, most people in this industry aren't really computer people material, that's the problem.
> Oh shit, I accidentally committed something to master that should have been on a brand new branch!
# disappear the last commit and all changes from it
git reset --hard HEAD^
# make a new branch using the last commit
git checkout -b new-branch HEAD@{1}
> Oh shit, I accidentally committed to the wrong branch!first, you don't need to git-add before and after stash, stash will save the working directory and the index (as documented in the DESCRIPTION of git-stash(1)). but for a more logical way:
# disappear the last commit and all changes from it
git reset --hard HEAD^
# get onto the new branch
git checkout new-branch
# grab the stuff from what was on the old branch
git cherry-pick old-branch@{1}
> Oh shit, I tried to run a diff but nothing happened?! git diff --cached
recommended reading for intermediate git users: the DESCRIPTIONs of all of these commands (git-reset(1), git-checkout(1), git-cherry-pick(1), git-diff(1)), and the entirety of gitrevisions(7). git checkout new-branch
git cherry-pick old-branch
git checkout old-branch
git reset --hard HEAD^Shameless plug: http://engineering.hipolabs.com/how-to-work-in-a-team-versio...
I think that's a tad judgemental for two reasons:
1- because git is legitimately hard to learn. Personally I suspect git is unnecessarily hard to learn, that command names and the concepts and workflows I need are possible to learn and use with less effort. For many years people around me have been asking for git help because I know how to recover from lost stashes and use the reflog, etc., and yet I still have to google the magic incantations for commands I use regularly because they're impossible to remember.
2- There is actually a lot of value in being able to spin up a new repo instantly, in knowing that you can, and in practicing it often. Not unlike the move to VMs for development environments. Plus, there are definitely bad situations where a fresh git clone is the simpler way to go -- just not in this article. ;)
Anyway, I also agree with you because this blog post doesn't describe any truly bad situations, and because for years I've seen people blowing away their repos and starting over, and always thought to myself it was funny. It's a drastic action that takes more work than a rebase or reflog or whatever the problem was, and doesn't work well if you've made changes.
2- Yes, absolutely, it's good to remember that it's a fairly painless and easy process to spin up fresh clones. Though, if all you want is a fresh copy, in 99% of cases a "git clean -xfd" will do that for you (read the man page to find out what the options mean! man pages are your friend!). Though that one is generally a "pull ripcord in case of emergency" type of command, "git stash" generally suffices and avoids risk of losing data.
I don't recommend git stash, it frequently causes bad situations. Just branch instead, branching is safer and just as easy as stashing. Stash is not as safe as other git commands. From the man page: "If you mistakenly drop or clear stashes, they cannot be recovered through the normal safety mechanisms."
I think the nuclear option is pretty nice, since it solves every git problem I've ever heard of. All the "right" solutions only solve some particular slice of cases. Why should I be bothered to care?
There's a class of bad situations that nuke will make worse and not better. Any time you have un-pushed work, you're better off figuring out how to restore it with proper git commands than by nuking your repo. Dropped stashes or screwed up merges or rebase mistakes are all things that take some unavoidable time to learn how to fix.
Not all people who write software are full-time developers. Not all people who use git are developers at all, come to mention it. Not all people who use git for one thing use it for everything. Not everyone can justify the time required for a course for something that might be a tiny part of their work.
Long term, it's best to thouroughly read the man pages, e.g. nicely formatted here: https://git-scm.com/docs
I've been doing that for a while for dropbear ssh, it does hit occasional problems but is overall more pleasant than straight git.
:)
s/branches/bookmarks/ and it's the same as git.
Now you mention it my git tags are out of date...
It's a cute website, and useful, I really like it. This sentence,
Bizarrely, git won't do a dif of files that have
been add-ed to your staging area without this flag.
File under ¯\_(ツ)_/¯"
just screams to me (a professional Git trainer), "I don't understand the Git staging area! I don't know my Git fundamentals! Train me!"If you have a team that needs Git training, email me and we can deliver a instance of this webinar (complete with Q&A session) free of charge (as an introductory service).
2. For someone just starting out, I also recommend "Learn Enough Git to be Dangerous", https://www.learnenough.com/git-tutorial (HTML version available for free), from @mhartl, author of the popular RailsTutorial.org
3. The "Pro Git" book is great. https://git-scm.com/book/en/v2 A comprehensive resource.
Version control systems are an important part of the programmers toolkit and it's worth investing a little time to get the fundamentals right.
Sure git is not the friendliest of beasts but what it lacks in interface, it more than makes up in internal consistency and trying to learn it "inside out" is a better long term investment than having a list of ways to solve "git problems".
It's better to accept history as it is, and fix the problem with reverts, cherry-picking and new commits. History won't be as pretty or clean, but it will reflect what actually happened, which is what history is, after all.
The biggest trick to understanding git is to learn to think in commits rather than file content. If one branch has a commit and the other branch doesn't, merging them will mean that the commit is there. If one branch has the commit while the other branch has the commit and a revert for that commit, merging will mean it will be reverted.
Same goes with pushing. If you reset locally to before a commit that you already shared, that commit returns on your next pull. If you revert it, it will stay reverted.
The most dangerous thing git can do, is rebasing. Rebase changes history. This is fine if it hasn't been shared yet (you commited a new change and try to push, but remote changes need to be pulled first, so rebasing is fine). Rebasing is not fine if the commit has already been shared, through a different branch or a different remote repository, for example. In that case, you need a merge.
As pretty as rebase can make your history, you should really only use it when you understand what it does. If you don't, stick with merge.
The docs say:
All changes made by commits in the current branch but that are not in <upstream> are saved to a temporary area. This is the same set of commits that would be shown by git log <upstream>..HEAD
Cleaner history makes life much easier for those who will try to understand what you did weeks or months or even years later (including your future self).
Of course, you shouldn't (with rare exceptions) edit history you have already shared with others; but, I never found that concept, or the way git implements it, particularly hard to understand. (I will admit some of git's commands are hard to remember - I am always forgetting the difference between "git reset --hard" and "git reset --soft".)
Recorded history is not necessarily what actually happened though, often (or always, according to some) it's an interpretation.
"History is written by the victors" - Walter Benjamin?
Funny, I think the biggest problem is people who refuse to understand the caveats of rewriting shared history and spread FUD about it.
See, I fundamentally disagree that this is what I wanted. When I merge my thing with change x into head I expect to see my changes actually go in. Not to see commits retroactively added to head. The merge was the action, not some weird rewriting of historical order of events.
> The biggest trick to understanding git is to learn to think in commits rather than file content.
Ok, I'm starting to see what you meant. (I didn't understand this until the second part and my being confused as heck)
Git is basically Linus in a nutshell: abrasive, unforgiving, and behaving like an absolute asshole to any non-expert struggling user.
Sure, engineers should probably get to know its quirks and learn to work around them because it's now ubiquitous in the field, but let's not pretend that there's something virtuous about it. This is a piece of truly terrible software with an even worse UX that just happened to win the PR battle against its superior foes, because...Idunno, Linux, I guess?
You forgot "brilliant".
He knows a lot about his domain, sure, but he's no god and there are a lot of areas he knows diddley-squat about.... user interface design for one!
The learning curve to git is not great but as far as I know nobody has publicly released an option that's improved enough over git to motivate the cost of switching technology. If it's not working for you the only advice I can give is either learn it until it is actually working for you rather than being in the way or try to find a better workflow.
Edit: also in a Windows environment, svn copes slightly better than git. First project we tried it on had the worst possible environment, a mix of Windows and Linux systems. Line ending nightmares everywhere.
It also took a while to work out how to kill (with fast-forward) the spurious "merge commit" you get every time someone does a pull. You don't have that with svn, you just do the merging locally when you update and it doesn't generate a commit.
I don't know any of the other distributed version control systems. The only other version control system I have much (too much) experience with is Subversion and it can't hold a candle against Git.
In my opinion Git is the C language of version control systems. If you are careless it's not the tool for you. Otherwise you have a really great tool with lots of power.
In an ideal world -- one where network effects of GitHub hadn't crushed all competition -- I'd get to choose Mercurial as my daily version-control system. It has similarities to git, in that both are largely user interfaces to a DAG, but its guiding philosophy and approach to the interface it exposes are far better in my opinion.
In my opinion Git is the C language of version control systems. If you are careless it's not the tool for you.
You probably don't want to make that assertion; we now have close to half a century of evidence that no human currently living can use C safely, and I don't think you'd want that to carry over to git. And it's not a matter of "careless" or not -- extremely intelligent, extremely-well-trained, extremely-careful people still write unsafe C.
Be that as it may, it's a common misconception that the more popular tool is always the better tool. There are a lot of factors to a tool's success and the technical ones are generally far less important than most people think.
Brushing aside some relatively minor differences, Mercurial is very similar to Git, except that it was designed by people that don't have an all-consuming disdain for all of humankind.
Not saying I disagree... but subversion is still king in the corporate-engineering companies and it tends to work well in those types of setups.
Just beware of having unjustified-opinions. SVN may not be trendy... but neither will git in a few years time.
BTW: Mercurial still beats them all.
> just happened to win the PR battle against its superior foes, because...Idunno, Linux, I guess?
I would assume GitHub is to blame for thatThe Git DAG and its content-addressable file system are an elegant solution to the problem of distributed version control. The implementation is clean and dead-simple, yet immensely powerful. This is the basis of good engineering.
> an even worse UX
Git was designed to expose its underlying implementation and not to isolate the user from it. Unlike other systems, its subcommands give the user the ability to manipulate the underlying data structure directly.
That doesn't mean it has a bad UX. Just that it has the UX of a power tool.
I also think that Git played a big role in the growth and popularity of command-line tools over the past decade. And that its interface inspired the subcommand pattern that is now commonplace in docker, vagrant, and others.
I think besides a PR battle (there's PR people for RCS'es??) I think Git won out because "the server was free". Competitors at the time needed a big (if there were large numbers of committers) server that someone had to pay for. By that I mean the hardware to host the server -- Git needs either none, or a very lightweight machine for a server.
Chalk that up to the power of fashion. Otherwise, people would use an SCM that doesn't destroy their work in the blink of an eye and actually has an API that deserves the name.
I disagree. "spell X solves problem Y" is an excellent way to both get started with fixing your Y fast AND for learning a system deeper.
I think we better learn by example (such as that) and in practical use over time, as opposed to getting some abstract knowledge of the whole system first, or (god forbid) reading the manual, which noone does.
Some coworkers (across all experience levels) use Git this way. It's frustrating how slowly they learn and conversely, how inefficiently they work.
Which again, we learn best by absorbing bits and pieces piecemeal over time -- that's how toddlers learn their native language, by immersion. Not by getting some "understanding of fundamentals" course first.
The irony is that if you think of git in terms of deltas, with branches just being tags to commits, and "git commit" actually being "git commit + move branch", then a lot of stuff is easier to reason about.
Like, if you get the notion of working tree, staging area, commits, and branches on a deep-ish level, things get easier. Things can still be hard, but there's a lot less "does running this cause everything to disappear???"
I think it would be possible to make this awesome page even more awesome with a little "annotated version" that gives the details of what you're actually doing.
Can you elaborate on that a little more? I liked where it was going -- because being able to explain how git works to someone coming from SVN has been an ongoing problem for me.
This way, you get a chain of commits, each pointing to the previous commit, and and branch references pointing to the last commit of each branch.
In this world, when you commit, you're doing two things: 1. Creating a new top level folder, e.g. "Project v42". 2. Updating the branch symlink you are on to point to that new folder.
I'm actually not simplifying things that much here, the main git addition is that each folder (revision) also has a list of previous folders (revisions) and that the folders (revisions) are not named sequentially, they are named based on a hash of their contents.
So you don't have three hours to read the fsckin' manual that describes how git repository is organized? O_o
For example, something as simple as "Changing the Last Commit" (as in "git commit --amend") is found in chapter 7.6 in the git book which is given as main documentation on git-scm.org. The first 6 chapter mainly deal with introducing the model, architecture and high level workflows. Even if you skip the chapter on github, that's still 5 chapters on mostly theory to read before you get to some often-used tool commands. I would argue that is more than 3 hours for most people if you don't just skim it but try to also understand it.
Apart from that, to answer the question: indeed, often I don't have 3 hours for something to be considered "nice to know" as opposed to "absolutely necessary". And if the manual is a "fsckin'" one, I'm unlikely to read it at all.
I agree completely! Anytime you give a list of 'run this command', I think you've actually written a CLI tool in the wrong language (English instead of a programming language).
At work, we want to increase dev's confidence with git in the next few months. We're making sure to separate out 'Technical' knowledge (what do steps do I perform to recover a deleted branch) from 'Conceptual' knowledge (why would it be a good/bad idea to delete a branch, how branches are intended to be used, understanding what git thinks a branch is, etc.). The former go into git aliases or CLI swiss army knife tool, the latter into training videos and workshops.
i agree, if you really want to be more productive (and without having to go find some git expert on your team) knowing how each operation works and why its doing what its doing is really beneficial.
despite that, its sometimes frustrating due to inconsistencies in the git "grammar" that i sometimes don't understand, like why if you want to apply only one file from git stash you have to use `git checkout` rather than `git stash apply [<stash>] -- <file>`. it makes "sense" but is unintuitive when i'm thinking about using stashed commits.
Thank you!
Generally there are (roughly) three approaches depending on where you are in your journey from beginner to expert:
As a beginner, you need specific, detailed, step-by-step instructions: First run X, then run Y, finally run Z, if something goes wrong along the way, follow these other steps, if that doesn't work, ask someone for help.
Once you've moved beyond being a beginner, you understand how the different steps are connected, so you already know the intermediate steps and just need to be told the task at hand: do an X (e.g. "move these commits onto the other branch").
An expert fully understands all the inner workings and just needs to be told the big picture: this is what I have, that is what I want, make it happen.
If you try to instruct experts like beginners, they will be frustrated because it seems so tedious and you're not giving them the information they want: what you're trying to do. If you try to instruct beginners like experts, they will be horribly confused because they don't know how to make any of it happen without you laying out each individual step they need to follow.
A case can be made that a beginner should learn each individual operation by heart before moving on to the next one (like kata in martial arts) but this style of learning assumes you're willing to spend a lifetime perfecting a single art. It's unlikely git will stick around that long and for most of its users git is just a tiny (though important) aspect of their work.
> As a beginner, you need specific, detailed, step-by-step instructions: First run X, then run Y, finally run Z, if something goes wrong along the way, follow these other steps, if that doesn't work, ask someone for help.
That's one way to do it, but it's not preferable. I would rather sit the beginner down for at least an hour and go through the basic concepts with them.
I've taught Git to a few people (mostly fellow students or colleagues) and I found that they have a much easier time when they are introduced early on to the basic concepts of Git (the commit graph, and branches/tags as pointers into the graph), they have a much easier time grasping relevant tasks like merge and rebase.
For specific questions (mostly in these "I screw up" moments), a whiteboard is also really helpful to explain them how the two or three magic commands that I tell them are actually fixing things up.
http://stackoverflow.com/questions/28759887/git-gc-no-space-...
> This usually happens to me if I merge to master, then run tests/linters.
If this happens on more than one occasion, I’d strongly consider creating a pre-commit hook to run tests and/or lint the changed files, e.g., I run `checkbashisms` and `shellcheck` as a pre-commit hook when working on shell scripts.
Ie, if the implementation of Git is right, but only the cli commands/etc are wrong.. what would the right UX be? What would a friendly UX look like?
Seems like something a lot of people could love - even if blasphemy to Git purists.
Just git reset <ref> should do. The --soft flag is implicit.
Amending and rebasing is something you should be careful with. If you've already pushed, you'll now need to force push. If another person has put new commits upstream, they will be wiped out irreparably. Not saying don't do it, just that it's very risky.
Instead of deleting and recloning the repo at the end, if you're really at that point, just doing git reset --hard origin/master should be equally as effective with fewer steps and less time.
Pulling down the repo a second time, however, can be more useful for just having a snapshot of the code in a second location that is totally independent of git traffic (don't pull to it very often). Say someone force pushes something that removes code. Your snapshot or someone else's non-pulled repo is your only hope of getting it back.
Regarding the case of an erroneous force push removing code, usually things can be recovered by looking through the reflog and doing some surgery via the lower-level plumbing commands... it's not exactly a fun process though :)
I wish I still had a log of the conversation or remembered the exact problem that led up to it, but it involved a simple amend totally screwing up my repo, and I've avoided it since.
Amending your commit in your own feature branch or fork isn't a problem. I often amend commits to make a cleaner git history to ease PR/code review.
git checkout master
git checkout -b 10-new-feature
# Make changes
git add example.code
git commit
git push origin 10-new-feature
# Oops... left a debug message, fix it.
git add example.code
git commit --amend
git push -f # Rewrites the remote history with your local modified version
Consider --amend as the "Just the last commit" form of rebasing.Amend after push, I could see. You commit, push, amend, push, but after your first push someone else pushes... then you have two heads. I could see that, but I'm guessing you'd mention that if it were the case.
If one were to attempt avoiding amend you could always make all changes in a feature branch and rebase this branch onto your target afterwards, squashing commits.
Once other people can be expected to have done work based on your amended or rebased work, you can expect some annoyances (or worse). If you haven't pushed yet (or if you're really, really sure nobody's basing further work on something you've recently pushed), I say go crazy and amend and rebase to your heart's content.
I've often had the thought that if everyone did this it could have potential to be quite the open-sourced collection of material - a distributed self-answered Stack Overflow perhaps.
Is there any sanity in this? Or would posting everything as self-answers to Stackoverflow be more welcome to the average Googler? (higher ranking, better meta, more likely for the user to see it and community features such as commenting/voting/editing)
http://www.slideshare.net/ChristianCouder/git-back-onyourfee...
I was expecting some actual "bad" situations based on the title, and to be fair these were bad to me once and are bad for people new to git, but I'd love to see the level 2,3,etc. version of this article.
* Learn Git Branching (gentle introduction to git's UI, with a visualization of the DAG) [0]
* Git for ages 4 and up (beginner-oriented, teaches you how to model git's internal concepts, 90m video) [1]
* Git from the bottom-up (high-level overview of git's internal data structures) [2]
* My Git Habits (sophisticated, you will have to study the man pages used to completely internalize the workflow) [3]
[0]: http://pcottle.github.io/learnGitBranching/
[1]: https://youtu.be/1ffBJ4sVUb4
Because otherwise, how can I be sure that some tool didn't mess up my repo, and one day I'll be getting error messages when checking out files, etcetera?
git add .
Ugh, no, never do this, never recommend to users to BLINDLY ADD ALL THE CHANGES FROM THE WORKING COPY. I honestly can't think of any worse git usage than this.Add the single change you missed, or even better, `git add -p`, to add chunks manually.
git reset --hard origin/master
(assuming that the remote is called "origin") With the example in the text, you have to know the number of commits you've made to master. git branch some-new-branch-name
git reset --hard origin/master
git checkout some-new-branch-name git checkout correct-branch
git cherry-pick wrong-branch
git checkout wrong-branch
git reset --hard HEAD~Also, if you have more than one commit before you noticed you were on the wrong branch, this only grabs the one commit.
Not sure what's bizarre about that. Doing so helps keep your commits clean and helps git tools (diff, git-gutter, etc) by ignoring things you've already stated are complete.
For example, I quickly fix a bug. The code is ugly and I don't want to commit it yet. So I add the bugfix files. Then I clean up the code (before commiting). Now I can do another diff between the staged and unstaged files, checking that it looks better than before, and still works. This way there is 1 clean commit "bugix" and not 2 commits "bugfix" and "bugfix code cleanup"
http://gitready.com/ contains a number of small articles categorized by beginner, intermediate and advanced that might be helpful.
Another resource for commonly used git tips and tricks: https://github.com/git-tips/tips
Like cherry picking, force pushing, merge --no-commit, rebasing... almost any operation can end up going wrong.
Just pay attention.
cd ..
sudo rm -r fucking-git-repo-dir
git clone https://some.github.url/fucking-git-repo-dir.git # create a new branch from the current state of master
git checkout -b some-new-branch-name
# remove the commit from the master branch
git checkout master
Or just `git branch some-new-branch-name`... cd ..
sudo rmdir nsfw-git-repo-dir
That will only remove it if it's empty? Which it never will be, because there's at least `.git/*`...Still, amusing :)
alias gitshit="open http://ohshitgit.com/"
Two days ago, I wanted to delete .git only, but accidentally as my fingers were accustomed with -fr , the command was `rm -fr * .git`. Rails server was running and some hope arose at the moment to just `lsof | grep` .. unfortunately that didn't work with me !
Ironically, all dot files have stuck as obvious :)
Those wouldn't have if you had set the following:
shopt -s dotglobThis is a feat of engineering, to take something complex and make it easy for anyone to undestand and use. It shows real expertise and deep understanding of the area.
In many ways it's a shining example against the 'culture of complexity' that we increasingly find ourselves in. Here rather than simplying the objective is to be to make thing as complex as possible, usually in pursuit of extremely niche use cases or because either the expertise or the interest to simplify is not there. If git was designed in this culture it would be fragile, full of buzz words, poorly documented and prone to failure, and something only a few self appointed experts could reason about and use properly.
Am I the only one that runs my tests before committing, let alone merging to master?
That's pretty much git 101
1. Click "Run all tests"
On your local machine even thousands of tests only takes a few seconds including the build process. We've got automated testing in the dev and test environments as well but if you're failing those there's a failure in workflow somewhere. There's very little reason to fail a test or to have a broken build in source control.If you don't want to change the commit message, in addition to --amend:
git commit --amend --no-editOh shit, someone checked-in huge file that shouldn't be in repo.
Oh shit, this folder I had been working on should have been in its own repo.
I also still say "new up" an object thanks to him. I'm not so proud of that one.
Until I switched, there was always a panic when branching or merging. With git, I can branch like a nutter and things seem to still work out in the end.
Not sure why, perhaps someone else has a perspective on it.
And conflicts from 'stash pop' are somewhat counterintuitive to handle.
Of the sites that are mostly static content, I would estimate around 60%. There's a small chunk of sites that are just blank with JS disabled, and there's a larger chunk that will load images only when JS is enabled.
60% is better than I'd expect for sites you expect to work without JS. =)
However would appreciate a quick and dirty handbook.
git checkout otherbranch
git checkout firstbranch -- fileIwant maybe1more
git commit -m "brought over files"I kept having problems with git, so I read a fucking book on it https://git-scm.com/book/en/v2
I'm not saying I never get into situations I can't get myself out of, but the examples in the oh shit website now look like obviously trivialities.
I noticed that git errors on my team were almost eliminated when we started doing that - especially once I started adding sanity checks to the git workflows. It also allowed members of the team who previously struggled with git to contribute much more effectively - they'd be rebasing without even necessarily knowing what rebasing was, for instance.
It also meant that the git workflow, if it was version controlled, could also be amended.
This worked out much better than the git course people were sent on.
Fine, that might be cheaper than properly training your employees. Of course, if your employees aren't training themselves out of genuine interest that's really the only option.
But then once something goes wrong you'll have to resort to "oh shit" websites and hope that you can the exact issue that you're having, since you really don't understand what's going on anyway.
> This worked out much better than the git course people were sent on.
I submit this must be because people came out of the course without really understanding the tool. Why they didn't understand I won't speculate, but I submit that they didn't.
EDIT: I think that which one is more appropriate depends on where it's used. I a big structured company I can believe that yours is better, or even crucial. But on a scenario where I'm starting a company with two other guys on a garage, I wouldn't want the guys to be the type of people who can only use a workflow that someone designed for them and never had the interest to dig under.
If things went wrong, people could come to me. That often resulted in me fixing their problem and fixing the script to ensure that kind of problem didn't happen again - after which people stopped coming to me because they didn't have problems.
Basically it was like releasing software.
>I submit this must be because people came out of the course without really understanding the tool.
I submit that not everybody needs to be an expert in the tool. Non-programmers can actually help a lot with writing stuff that needs to be version controlled with code - e.g. writing test scripts, updating translations, various configurations, etc.
Perl 5 -- I don't know enough about 6 to know whether this is still the case -- involved a couple of design decisions which worked well for one subset of people and did whatever the complete polar opposite of "works" is for a different subset of people.
One of those design decisions is the famous "there's more than one way to do it". Ask five Perl programmers to solve a simple problem in the language and you'll get fourteen solutions, and no way of knowing which, if any, is to be preferred in real-world use. Git similarly tends to have multiple ways of achieving a given goal, and endless words have been devoted to documenting them; nobody has yet settled which of them is or should be generally recommended, or if there even can be such things as generally-recommended ways to use git.
Perl also involves an incredible amount of memorization. The large collection of "magic" variables which can be read or set to determine language and local-scope behavior are daunting and can't be mastered through any process other than rote memorization. And the differing behavior of basic operations depending on context produces a combinatorial explosion of possibilities which, again, are not amenable to any technique short of rote memorization. The same is true in git: though there are underlying abstractions, the interface provided to them is inconsistent, varies depending on sometimes-invisible contextual factors, and ends up requiring rote memorization of commands and options and how they behave in any given situation.
Unfortunately, no amount of documentation or review by technical writers can fix this. A bad API can't be made good through better documentation, it can only be made good through being replaced with a better API.
Git is worse than Perl. Perl is just opinionated: despite what you are saying about it is true, you can write working programs and have fun at all levels of Perl mastery. There's a lot to memorize, but you can start coding immediately and do something useful. Also, Perl community is incredibly friendly and helpful.
Not so with Git. If you start without thorough understanding how Git internals work, you will get burned really soon. And more often than not, the only response you'll get is the arrogant RTFM or the link to Pro Git book.
Next on the list: Larry McVoy's Bitkeeper promises to be everything git and Mercurial aren't. (git is "inspired" (read: copycat) by Bitkeeper).
It's funny how Sun Microsystems influenced the industry in so many ways, isn't it?