Confused by git?  Here's a git crash course to fix that 🎉
Chris Achard

Chris Achard @chrisachard

About: I'm trying to teach everything I know at chrisachard.com Instructor at egghead.io Mostly, I use JS, React, Rails, and Node

Location:
USA
Joined:
Aug 14, 2019

Confused by git? Here's a git crash course to fix that 🎉

Publish Date: Sep 10 '19
1349 40

This was originally posted as a twitter thread: https://twitter.com/chrisachard/status/1171124289128554498

NOTE: if you are looking for a very basic intro to git, I recommend reading this guide by Atlassian first.

Do you use git but still don't really understand it?

Here's a 🔥 git crash course 🔥 to fix that 🎉

1.

Git gives you a FULLY FEATURED repository on your local computer

This is different than other version control systems

Once you embrace that, you can start demystifying some of the git 'magic'

Decentralized repos

2.

Think of files (and changes) as being in 5 different places, or "states"

  • Working directory
  • Staging (Index)
  • Commit tree (local repo or HEAD)
  • Stash
  • Remote repo (github, Bitbucket, gitlab, etc)

Files move between 5 states

3.

Think of moving files (or changes) between those places:

git add working dir => staging

git commit staging => HEAD

git push HEAD => remote repo

git stash working dir <=> stash

git reset and git checkout to pull from upstream

Commands that move files between states

4.

Why have a dedicated staging area?

So that you can choose and review which files and changes to commit before committing.

Dedicated staging area

5.

git status shows changes in both your working directory and staging, but think of them as separate things.

git log shows the history of commits your local repository

git status and git log

6.

Learn to love git log

It's a snapshot of repo state: shows past commits as well as local HEAD, local branch, remote HEAD and remote branch

git log --oneline is compact way to view commit history

git log

7.

A branch is a reference to the tip of a line of commits

It automatically updates when new commits are added to that line

Making a new branch will diverge the tree at that point

branching

8.

A merge takes two branches and makes a NEW commit which combines them

If there are conflicts, you have to manually resolve them (no shortcuts!)

Merge

9.

git rebase lets you rewrite commit history

Applies your current commits directly to branch HEAD

Can squash all your commits into one to clean up history

Don't do this to public (remote) commits!

Rebase

10.

Some people say you should only ever merge to keep your entire history

Some people say you should always rebase before merging into master to keep a clean history tree

I say: do whatever works for you and your team 🤷‍♂️

11.

HEAD can point to a branch or a specific commit

If it points to an old commit, that's called a "detached HEAD"

Editing in a detached HEAD state is dangerous (can lose work or cause problems combining work)

HEAD

12.

Many git commands can operate on either: individual files, commits, or branches

This can cause a lot of confusion - so make sure you know what TYPE of object you're operating on

13.

There are many ways to undo unwanted actions in git
Here are the most common:

unstage a file: git reset [file]

change last LOCAL commit: git commit --amend

undo local commit: git reset [commit BEFORE the one to undo]

undo remote commit: git revert [commit to undo]

14.

There's SO MUCH MORE I could have talked about!

What other things confuse you about git?

Comment below and I'll try to answer or find some resources for you 🙌

 

Like this crash course?

Follow me on twitter for more: @chrisachard

Or you can join the newsletter: https://chrisachard.com/newsletter

Thanks for reading!

Comments 40 total

  • Jasterix
    JasterixSep 10, 2019

    Great article! I also recommend the atlassian git docs, which helped me get comfortable with git

  • Sebastian Vargr
    Sebastian VargrSep 10, 2019

    Git is the expression used for an old angry man.

    Either git good, or git’ out.

    Either way someone is going to be frustrated.

    • Sebastian Vargr
      Sebastian VargrSep 12, 2019

      Just realised i missed out on a final pun.

      "Either way someone is going to git' frustrated" ...

  • Jason
    JasonSep 10, 2019

    I was really getting confused, till I realized I was seeing the word 'comment' instead of 'commit'. Now it makes much more sense! Great article.

  • Blair Jersyer
    Blair JersyerSep 10, 2019

    nice course...i'm however a bit confused about "stash".. first time i read this. What is that ?

    • Chris Achard
      Chris AchardSep 10, 2019

      Thanks - and yeah, I'm realizing that I didn't explain stash... like at all in the post 🤦‍♂️ oops.

      Stash is a temporary place you can put work in progress. Usually, the workflow goes something like this:

      • You are working on something in your working directory

      • a high priority bug comes in. To fix it, you have to switch branches and clear your working directory of the changes you already have made

      • instead of trying to save your work for later in a commit or a special branch, etc, you can put it in the "stash" with git stash

      • Then you go and fix the high priority bug

      • later, you can re-apply what was in the stash with git stash apply or git stash pop (pop will remove it from the stash; apply just brings it back over)

      • then you can continue to work on whatever you were working on from bullet 1.

      Hope that helps a bit! I probably should have had a separate point for it in the guide 😀

  • AlexCodes
    AlexCodesSep 10, 2019

    Thank you!! I am currently learning git

  • khangprcvn
    khangprcvnSep 11, 2019

    Wow, thank you.

  • Meierreoi
    MeierreoiSep 11, 2019

    Just find something useful in this post, thanks.

  • Jamees Bedford
    Jamees BedfordSep 11, 2019

    On fire with these recent posts Chris!! 🔥

  • Papidev
    PapidevSep 11, 2019

    Love it, so clear

  • Ekanem
    EkanemSep 11, 2019

    First time hearing about git log --oneline. A truly cleaner way to look at the history.

    Thanks Chris

    • Chris Achard
      Chris AchardSep 11, 2019

      Glad it helped!

    • Jérôme Gully
      Jérôme GullySep 11, 2019

      And there is much more... try
      git log --all --decorate --oneline --graph :)

      • Chris Achard
        Chris AchardSep 11, 2019

        Yes! there's a whole bunch :) Here's the full docs if you're interested: git-scm.com/docs/git-log (examples at the bottom)

      • Ekanem
        EkanemSep 12, 2019

        Just did it! Thanks.

        Now I feel like a 10x developer :)

  • marko
    markoSep 11, 2019

    What is command when changes should be added under pushed commit?

    • Chris Achard
      Chris AchardSep 11, 2019

      If I understand correctly, what you want is to add the extra files to the staging area with:

      $ git add FILE
      

      and then you can add them to the most recent commit with

      $ git commit --amend
      

      Does that solve the issue?

      • marko
        markoSep 12, 2019

        Is this also working for remote last commit?
        Thank you.

        • Chris Achard
          Chris AchardSep 12, 2019

          If you want to change a remote commit, you'll have to do this, and then push with -f (which is a force push).

          HOWEVER! Be careful with force push. If you accidentally force push to the wrong branch, then it can really mess you up, and if you have teammates who have already downloaded a public branch (like master), then force pushing to master isn't a good idea.

          If you've already pushed to a public branch, the better choice is probably to just make a new commit.

          • marko
            markoSep 13, 2019

            I will keep that in mind. Thank you.

  • Jérôme Gully
    Jérôme GullySep 11, 2019

    Best git summary ! Clear, concise, simple illustrations. I already master all these commands, but I never seen a so summary, so thanks. I will spread my team with your post :)

  • Alberto Pertusi
    Alberto PertusiSep 12, 2019

    Very good explanation, thanks :)

  • Julien Camblan
    Julien CamblanSep 12, 2019

    The clearest git cheatseet, thank you! 🙏

  • Lasha Kakhidze
    Lasha KakhidzeSep 12, 2019

    The major git features, explained so simply, thanks Chris!

  • Omar Gaston Chalas
    Omar Gaston ChalasSep 12, 2019

    wow! git log --oneline is amazing

  • Jonathan Cousins
    Jonathan CousinsSep 12, 2019

    The biggest misconception I find (which I couldn’t see listed here) is that a the history is a list of changes. Instead commits are snapshots of the entire repo, not just changes.

    • Chris Achard
      Chris AchardSep 12, 2019

      That's a good point - git is unlike SVN in that way. SVN stores diffs (which is why it can take a long time to calculate the current state when your repo history gets really long), and git stores entire files. I always had thought that git stored diffs as well (since you "commit" just the change, right?) - but nope!

      I didn't include it because I find that, in practice, it doesn't matter much whether you think of the commit log as being diffs or snapshots - but I could be wrong... have you found cases where it matters a lot which way you think of it? Thanks!

      • Jonathan Cousins
        Jonathan CousinsSep 12, 2019

        It does when people get comfortable with cherry picking.

        • Chris Achard
          Chris AchardSep 12, 2019

          Ah, good point

          • Jonathan Cousins
            Jonathan CousinsSep 12, 2019

            You have a very helpful article. I would definitely not expect someone new to git to start cherry picking.

            • Chris Achard
              Chris AchardSep 12, 2019

              Thanks! Yeah; maybe on a more advanced course sometime :)

  • Ariful Islam
    Ariful IslamNov 23, 2020

    Pretty good and clear article no doubt. Those who always fail to memorize basic git commands, I make a datatable which name is gitcom. you can check from below link as well.
    arif98741.github.io/gitcom/

  • Chargoy
    ChargoyDec 23, 2020

    Thank you so much for this course, really amazing 👍.

Add comment