14 ms·
Key Git Concepts Explained the Hard Way
- zwischenzug 9y agoThe raw shell for the workout is: rm -rf lgthw_* mkdir lgthw_origin cd lgthw_origin git init echo 1 > afile git add afile git commit -m firstcommit git log --oneline --decorate --all --graph git branch otherbranch git tag firstcommittag git log --oneline --decorate --all --graph echo 2 >> afile git commit -am secondcommit git checkout otherbranch git log --oneline --decorate --all --graph echo 3 >> afile git commit -am thirdcommit git log --oneline --decorate --all --graph git checkout firstcommittag git log --oneline --decorate --all --graph git checkout -b firstcommitbranch git log --oneline --decorate --all --graph cd .. git clone lgthw_origin lgthw_cloned cd lgthw_cloned git remote -v git log --oneline --decorate --all --graph git branch -a git checkout master cd ../lgthw_origin git checkout master echo origin_change >> afile git commit -am 'Change on the origin' cd ../lgthw_cloned git fetch origin git log --oneline --decorate --all --graph git merge origin/master git log --oneline --decorate --all --graph cd ../lgthw_origin echo origin_change_rebase >> afile git commit -am 'origin change rebase' git log --oneline --decorate --all --graph cd ../lgthw_cloned echo cloned_change_rebase >> anewfile git add anewfile git commit -m 'cloned change rebase in anewfile' git log --oneline --decorate --all --graph git fetch origin git log --oneline --decorate --all --graph git rebase origin/master git log --oneline --decorate --all --graph
- Osiris 9y agogit is one technology that I was able to really understand quickly. Once you understand the architecture, what a commit is and how they relate to one another, all the git commands make a lot of sense, including the more complicated commands like rebasing and branch history rewriting. The simplicity of the architecture is a great feature of git.
- bluGill 9y agoGit is only simple if you haven't been exposed to a simpler version control system before. Despite being one of the newer version control systems git does worse than most version control systems at actually hiding details. If you are not used to a version control system where you don't care about the details git seems great. However those of us who come from something else (mercurial in my case) are not happy about having to learn the git internals to do things that in mercurial didn't require knowing mercurial internals. I know what I want to do, I've been using a distributed version control for years, but git is forcing me to learn something that has always been hidden. Sadly github and the like have built enough momentum around git that I'm forced to learn it. (Note mercurial does have its warts, but they are on a different level)
- gouggoug 9y agoSo many people have a hard time with git. I have yet to meet someone who understands git and uses it proficiently. What I tell every new programmer joining my team is to read chapter 10 (Git Internals) section 10.2 and 10.3 of the amazing online git book[0][1][2]. I can not insist enough on how important it is to understand the fundamental design of git, and it is really not complicated. This is what made me understand git and changed it from a hard tool to use, to this amazing productivity software that I love. The kicker for me was to realize that in git (mostly) everything is a reference to a commit id. What's a branch? It's a human readable string, made for humans, by humans, that ultimately simply references a commit id. What is a new branch? It's just a new human readable string pointing to a specific commit in your history. [0] https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Po... [1] https://git-scm.com/book/en/v2/Git-Internals-Git-Objects https://git-scm.com/book/en/v2/Git-Internals-Git-Objects [2] https://git-scm.com/book/en/v2/Git-Internals-Git-References https://git-scm.com/book/en/v2/Git-Internals-Git-References
- zwischenzug 9y agoLearning git has given me many a-ha moments. Years ago it was even worse, with unstable porcelain and only the man pages for company. What made it all work for me was conducting courses in it, so I had to make the concepts vivid for people that weren't that interested in the theory. That forced me to reduce it to simple examples, which really worked.
- JoshTriplett 9y ago> I can not insist enough on how important it is to understand the fundamental design of git, and it is really not complicated. I absolutely agree. Everyone I know who has expert-level knowledge of git, and can do arbitrary things with it or solve arbitrary problems with it, thinks along the lines of "I want to get from this repository state to that repository state, what commands will get me there?".
- deleted 9y ago[deleted]
- CodeMage 9y ago
- lucb1e 9y agoThis post is almost impossible to understand if those concepts are truly new to you. The hard way is supposed to just dive in, not skip necessary bits. I understand it's a promotional piece for a book, but this makes me wonder whether the book is also like this (probably (hopefully?) not because there is more space).
- urda 9y agoThis feels more like an Ad than an article to be honest.
- iooi 9y agoMods, isn't it time you add something to the guidelines [1] to discourage advertisements disguised as articles? https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- lucb1e 9y agoI'm not sure about prohibiting it. I didn't really like the article, but if more people feel that way, the community just shouldn't upvote it -- or at least other, better things get more upvotes than this one. I feel like prohibition should be the last resort for things which the mods notice actively causing conflict among otherwise civil members of the community, like political discussions (e.g. there have been attempts at no politics days because of this, but that didn't work well for multiple reasons).
- maxdamantus 9y agoAnd even if the concepts are not new, the post seems very misleading. Something describing itself as teaching "the hard way" should certainly avoid fuzzy statements such as the following: > HEAD is a special reference that always points to where the git repository is. This kind of makes sense if you think about it a certain way (it generally points to something (possibly indirectly) that the index/working tree is based off), but off-hand, "where the git repository is" sounds like it means the location of the repository within the filesystem or something. > If you checked out a branch, it’s pointed to the last commit in that branch. If you checked out a specific commit, it’s pointed to that commit. This is inaccurate. When you check out a branch, HEAD points to the ref for the branch, which in turn usually points to a commit. > Every time you commit, the HEAD reference/pointer is moved from the old to the new commit. This is wrong unless you're in a "detached HEAD" state. `git commit` updates a ref that does not point to another ref. If you're in a "detached HEAD" state, that means HEAD is updated (since HEAD is pointing directly to a commit). If you've checked out a branch, that branch's ref will usually be updated (since HEAD is pointing to a ref which points directly to a commit). In my opinion, git is best described in terms of what is ACTUALLY happening in terms of modifications to the refs database (essentially a `Map FilePath (Either FilePath ObjectId)`), rather than trying to come up with metaphors for what it means to perform certain actions. When I say that "HEAD points to the ref for the branch", that is demonstrably exactly what happens: > $ cat .git/HEAD > ref: refs/heads/master > $ git checkout -b other-branch > $ cat .git/HEAD > ref: refs/heads/other-branch > $ git checkout --detach > ffd914475c61c18c684ccb0024ca141c54255e28 Understanding this basic concept should make most of the concepts explored in sections 1) and 2) of the article self-explanatory.
- thrownaway954 9y agoGit comes down to knowing 6 commands... and you don't need to even need to be an expert with all 6. If can grasp the following 6 commands, you will know about 99% of every day uses of git: git add . git commit -m "my commit message" git merge my_feature_branch | or my personal favorite: git merge --squash my_feature_branch git rebase my_feature_branch git log git checkout -b my_feature_branch
- reificator 9y agoI don't know - cloning, pushing, pulling, and arguably fetching are all part of a normal workflow with git. You're up to 9-10 commands now.
- tjr225 9y agountil you accidentally use rebase to squash commits and accidentally squash the wrong commits...
- iooi 9y agothen just run `git reflog` to go back to the commit before you rebased
- AlexCoventry 9y agoreflog is really out in the tail of git commands you ought to know to use it effectively, though. I'd say you're at much more than six commands at that point.
- recursive 9y agoI never use log or rebase. I do use push, pull, and checkout. I guess everyone has a different 99%.
- kccqzy 9y agoI don't know. I just grepped my own shell history, selecting those lines that start with git and printed the first two words. Even removing those commands that I use just once or twice, I have a much longer list than yours. Here are mine that are not mentioned by you: git cherry-pick git shortlog git push git pull git fetch git stash git branch git diff git blame git show git reset git help
- number6 9y agoto learn git this is amazing: https://learngitbranching.js.org/ https://learngitbranching.js.org/ It's almost like one of theese games like TIS-100 until you grock it and then you understood git :D
- friedButter 9y agogit commit -a -m "Commit" && git push 90% of the time, all you need :)
- zwischenzug 9y agoSure, but it's the 10% that hurts!
- jameshart 9y agoThe other 10% of the time: git commit -a -m "Commit" && git push -f
- saagarjha 9y agoForcing your coworkers do deal with your incompetence, I see.
- jameshart 9y agoSince the OP never mentioned a need to use 'git pull', I assume the 10% of the time it didn't work was when other people had gone and corrupted the repository by pushing their own commits. Clearly that's not something we want to have to deal with.
- ilammy 9y agoFixed this for you: git commit -a -m "Commit" && git push || ( git push --force; echo "Bob, my git is weird again, could you please help?" > /run/slack/dm/bob-the-local-git-expert ) Obviously, you do this from master.
- hnrodey 9y agoUntil you actually need git add . && git commit -m "message" && git push <remote> <branch>
- aequitas 9y agoUnderstanding that in git 'branches' are not really the branches but merely post-its that can trivially moved between the 'real' branches on the tree of commits really opened my eyes. Along with the use of visual tools like https://github.com/gitx/gitx https://github.com/gitx/gitx, https://github.com/FredrikNoren/ungit https://github.com/FredrikNoren/ungit, https://github.com/jonas/tig https://github.com/jonas/tig and aliases like `log --oneline --decorate -50 --all --graph --remotes`. It really changes Git from a bunch of vague commands to a toolkit that you can use to apply the changes you desire on the database and worktree. But even then I still get confused as the concepts of Git are not well translated into the commands, the various functions of reset and checkout. I think it is mentioned often before on HN but http://gitless.com/ http://gitless.com/ is an good approach to create a new API/concepts on top of the same internals.
- dbkaplun 9y agoGitUp[1] is an excellent alternative to gitx and ungit, for those unhappy with either. [1]: https://github.com/git-up/GitUp https://github.com/git-up/GitUp
- bowmessage 9y agowhy isn't `git log --oneline --decorate --all --graph` the default behavior of `git log`? It makes things so much easier to understand IMHO.
- hcnews 9y agoWhat does this git command do? I have used git for a decent amount of time and still have never used this.
- bowmessage 9y agoGive it a try - it produces git logs which are much more 'graph-like', and really help visualize the different branches used in a given repo. I especially like the `glola` alias provided by the `oh-my-zsh` git plugin.
- hcnews 9y agoAh I see, its git repository history. The 2 places I have worked at thankfully have other ways of visualizing this. I also don't view the different commits using git. Rather use a different tool like Phab etc.
- dsego 9y agoYou can add aliases to your ~/.gitconfig. These are mine [alias] co = checkout br = branch ci = commit st = status ll = log --pretty=format:'%C(magenta)%h%Creset -%C(bold yellow)%d%Creset %s %Cgreen(%cr) %C(bold cyan)<%an>%Creset' lg = log --graph --pretty=format:'%C(magenta)%h%Creset -%C(bold yellow)%d%Creset %s %Cgreen(%cr) %C(bold cyan)<%an>%Creset'
- bowmessage 9y agoYeah, aliases are great for those of us who have had the exposure necessary to learn about all kinds of flags and features. But I think the default `git log` view could do with a lot of improvement to help out newer git users.
- breatheoften 9y agoThere are amazing ui’s which provide really nice interfaces to git. I always cheerlead http://gitup.co http://gitup.co when I come across git complain threads (I love gitup!). Try it and you’ll quickly realize that git is hard to use because it has a terrible command line interface and because the git commands manipulate state which is not at all easy to visualize from the command line. Being able to Visualize the state of the repositories provides a huge win when learning the actual concepts behind dvcs (which are NOT complicated)
- Stratoscope 9y agoIndeed, I'm a big fan of SmartGit: https://www.syntevo.com/smartgit/ https://www.syntevo.com/smartgit/ SmartGit isn't an oversimplified GUI designed mainly for beginners, it is very powerful. It gives you a direct view of the state of your repo along with high level tools to manipulate it. Taking a couple of examples from this thread: > Stackoverflow top item on how to redo a commit (one of most common things a new user may need to do): $ git commit -m "Something terribly misguided" $ git reset HEAD~ << edit files as necessary >> $ git add ... $ git commit -c ORIG_HEAD In SmartGit you just edit your files and commit again, and in the commit dialog check the "Amend previous commit" box. Or the reflog. Ah, the reflog. You get this list of hashes and commit pointers and commit comments. Now what? Which was really the commit you were looking for? If you're like me you may have several commits with the same comment as a result of rebasing and amending and generally fooling around. How do you see the actual diff for one of those commits? In SmartGit, you open the log window and click the "Recyclable Commits" box at the bottom of the Branches panel. Now everything from the reflog is displayed as part of your commit log, just like any other commit or branch. You can click on any of those commits to see the list of changed files, and click on any of those files to see the diff. This is orders of magnitude more usable than the command line reflog. And yet, I often find a tool like SmartGit a tough "sell" when I'm talking with other developers. One very talented developer told me, "I would never use a GUI, I'm much more productive in the commaand line." But the same developer also told me they never use rebase or amend a commit for the reason that you could lose your prior commits. They'd heard of the reflog but never used it. And really, who would want to if you could avoid it? It's a real second-class feature and very awkward to use. In SmartGit, you just click that box and the entire reflog becomes part of your normal log with all of the tools available.
- nodivbyzero 9y agoDid somebody mention magit? It helps a lot
- Bromskloss 9y agoI thought all we needed was the short "Git for Computer Scientists" [0], which consists mostly of images and which had me saying "oh, that wasn't so hard after all". [0] http://eagain.net/articles/git-for-computer-scientists/ http://eagain.net/articles/git-for-computer-scientists/
- polm23 9y agoIf you'd like a guide to git that explains the basic concepts first and explains the data model only after it's shown it's useful, give Git Magic a look. http://www-cs-students.stanford.edu/~blynn/gitmagic/ http://www-cs-students.stanford.edu/~blynn/gitmagic/
- fazilakhtar 9y agoHad a git tepo that I messed up pretty bad while in college, went to the professor to ask him to delete the repo so that I could start over. He told me to fix it as he wouldn’t delete it, best advice I’ve gotten as it truly was a learning experience.
- jldugger 9y ago'the Hard Way' but no mention of Merkle Trees. Perhaps it's time for Git for Rocket Scientists.