Skip to content

Instantly share code, notes, and snippets.

@Br1ght0ne
Last active September 12, 2019 10:38
Show Gist options
  • Save Br1ght0ne/6f16bf0c110a5702696530e12676013b to your computer and use it in GitHub Desktop.
Save Br1ght0ne/6f16bf0c110a5702696530e12676013b to your computer and use it in GitHub Desktop.
Confused by git? Here's a git crash course to fix that

[2019-09-12 Thu 13:04]

This was originally posted as a twitter thread.

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’

    https://res.cloudinary.com/practicaldev/image/fetch/s--a5EKi49_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mbn8umr5j0pljsgbs630.png

  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)

    https://res.cloudinary.com/practicaldev/image/fetch/s--AjJeH8nD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/xlyfhp29i31yf16lj9e6.png

  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

    https://res.cloudinary.com/practicaldev/image/fetch/s–_iVj7qv0–/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/vl216qavkx2xbblrktje.png

  4. Why have a dedicated staging area?

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

    https://res.cloudinary.com/practicaldev/image/fetch/s–vJEym-oT–/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/b7n7jvx79yiebgcfizqf.png

  5. git status and git log
    • 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.

    https://res.cloudinary.com/practicaldev/image/fetch/s--Le-lv5CE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/lr2ilrf0e8th2d1hmcb6.png

  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.

    https://res.cloudinary.com/practicaldev/image/fetch/s–187QtFCo–/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/lia078b2ubxb01wh4ip8.png

  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

    https://res.cloudinary.com/practicaldev/image/fetch/s–3lZWCBhL–/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/f60jj17zthpu9r7lw8xt.png

  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!)

    https://res.cloudinary.com/practicaldev/image/fetch/s—IpYpslf–/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/1u7lui33bzhmudstr4wv.png

  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!

    https://res.cloudinary.com/practicaldev/image/fetch/s–mjeH_6SM–/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/5o2ahxumbv3viui874ix.png

  10. Merge vs. Rebase
    • 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
    • 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)

    https://res.cloudinary.com/practicaldev/image/fetch/s--bCfmyi3l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/w0gu5apjs65rodbtvi3k.png

  12. Care about types.
    • 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. Undo in Git

    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. Author & Attribution

    Twitter: @chrisachard

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

    Thanks for reading!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment