Skip to content

Instantly share code, notes, and snippets.

@mileshaley
Last active March 5, 2026 03:45
Show Gist options
  • Select an option

  • Save mileshaley/299dcc358d1b7888d3daad383ca111f6 to your computer and use it in GitHub Desktop.

Select an option

Save mileshaley/299dcc358d1b7888d3daad383ca111f6 to your computer and use it in GitHub Desktop.
My Notes on Git

Understanding Git

The following sections should be read top to bottom as they are ordered by level.

i.e. level 1 is the shallowest but most important for a beginner to read, level 2 builds on level 1 and is deeper and targeted towards slightly more advanced/eager users, and so on.

Make sure to read each level all together. For instance, level 1 won't be usable until you finish reading it.

Important

If you ONLY read level 1, you should be able to understand how to use Git at a basic level, BUT you'll have an oversimplified idea, you'll miss out on useful information and features, and you may be susceptible to common pitfalls.

Level 1: Essential Basics

Git is a version control software (VCS) which stores the history of all your work on a project on your computer or a server. You CAN simply use it to back up your work BUT it is far more useful than that. You can easily: Undo unwanted changes, find exactly when a bug appeared, see/load the whole project from a specific point in time, and much more.

Git also allows for easy collaboration between multiple people/computers.

Note

Git itself doesn't provide you a server to share a project between people/computers. GitHub is one of many popular services that provide a server to allow sharing a project (Plus some nice extra features).

Concepts (Level 1)

  • repository/repo: A "folder" that represents an entire project and its history.

  • change: When a file in the repository is created, deleted, moved, or has its contents modified.

    • A file is untracked if Git hasn't been told to care about it's changes. Newly added files will be untracked until you use a command like git add on them.
  • command: The way you tell Git what to do. All commands start with git and have arguments, e.g. git add my_file.txt.

  • commit: A snapshot of the project at the exact moment in time after you apply a group of changes.

    • Commits have changes, a message about the commit, and an author (you or your teammate).

    • Each commit has a unique "name" called a hash based on the content of the commit. For example:

      b35e26a1de46f907f09a7a941739e3ba3638e491 is a commit's "full legal name".
      b35e26a is that same commit's shortened, more readable, "first name".

    • Each commit knows the "name" of the commit that came before it: Its parent that tells us how the project looked before the changes of the child commit happened.

      // ABC are commits, oldest to newest, left to right (A is oldest, C is newest). 
      // Generally, A <- B means A is B's parent and B is A's child.
      A <- B <- C 
    • This means if we look at a child commit's parent, then that parent's parent (child's grandparent), and so on until we get to the first commit (A), we see a chain of every change since the beginning, in order, that led to the current child snapshot. This technique is the foundation of Git, and is how it knows how the repository changed and looked at any commit.

  • history:

  • stage: Before changes can be committed, they have to be added to the stage, which is called staging changes. This lets you control exactly what files and changes you group into a single commit. This helps you organize commits logically and keep personal info, incomplete features, build outputs, etc. on only your computer.

  • branch: A point in history (a commit) where the contents of the project diverged/branched into separate versions with differing histories/commits. Don't worry, we'll be able to put the branches back together later by merging them.

    • Each branch has a name and acts like a separate workspace that you can commit to.

    • You can only use one at a time but you can easily switch between them.

    • Each repository starts with 1 branch, the trunk (Git term): It's usually named main or master (Not Git terms, just common names) and treated as the canonical version, or "source of truth" of the project.

      // We're at the same point as before, let's branch from C.
      // We'll say the top branch was always named 'main' and we'll name the bottom one 'feature' when we branch.
      A <- B <- C <- E <------ H <- main
                ^               
                |               
                +- D <- F <- G <- feature
      // After we branched, we made commits in the order DEFGH:
      //     DFG were commited to 'feature', making its complete history ABCDFG.
      //     EH were commited to 'main',     making its complete history ABCEH.
    • When you're done working on a branch, you can merge it back into the other branch. Despite how it sounds, the branch will still exist after this BUT you would typically want to delete it after anyway.

    • deleting a branch is no biggie! You can do it if you don't care for the work on the branch, or you already merged your changes. You can even reuse the branch's name again after deletion if you like.

  • merge: A way to integrate work from one branch into another by putting their collective changes together.

    // Same point as before, let's merge feature into main.
    A <- B <- C <- E <------ H <- I <- J <- main
              ^                   |
              |                   |
              +- D <- F <- G <----+
                           ^
                           | 
                        feature
    // When we merged, I was AUTOMATICALLY created. Notice that I has not one but TWO parents.
    // After we merged, J was committed to 'main', making its complete history ABCDEFGHIJ
    // Notice how branch 'feature' still exists and the latest commit on 'feature' is still G and NOT I or J!
    • Merging creates a commit with TWO parents, which makes sense since there are now two histories (chains of commits) that led to the current history (at I and beyond).

    • If neither branch changes a specific block of a file, do nothing.

    • If one branch changes a specific block of a file, use the modified block in the merged version of a file.

    • If both branches change a specific block of a file, that's called a merge conflict and it has to be resolved manually by the person merging.

  • remote: A name for a server containing the canonical version of the repository via a URL (which can be a link to a website or a path to a folder on your computer).

    • A very common name for a remote (Git term) is origin (Not a Git term, just a common name).

    • By hosting a single, "source of truth" version of the repository, the remote, everyone working on the project only has to negotiate changes with the server instead of having to do it with everyone else. Nice!

    • It's important to understand that Git is not affiliated with GitHub nor does it need to acknowledge its existence and the same goes for any other Git "provider". Git's remote server model allows for any party build on top of it and host Git repositories. The host of your repository could be GitHub, GitLab, your own server, a non-Git-specific server hosting platform, or even just an offline "hosting" version of the repository on your computer, and Git doesn't know any of those things exist!

Usage Philosophy [Level 1]

  • Organize and label your commits logically and descriptively: Doing this will save you a LOT of headaches later on. Organizing and naming your commits well is (unfortunately) up to you and not up to Git. Make small commits only including changes that relate to each other. Add a commit message that not only says what you did but WHY.

    Imagine you caused a bug at some point and you have 3 commits to search through to find it: "start", 12 files modified; "fixed bug and other stuff", 43 files modified; and "h", 10 files modified. Where do you even start? spending an extra 20 seconds each commit being organized can save you hours down the line.

  • Don't commit directly to main: Only commit to branches, and then merge to main.

  • Your commits DON'T have to be non-breaking. That's why we use branches and don't commit directly to main, so we can keep potentially broken commits and work on them until they're working and ready for main.

  • Use branches liberally: Branches off of main are perfect for single small features and fixes. Branches are incredibly powerful AND incredibly cheap (literally 41 bytes per), so don't worry about making/deleting too many.

  • Avoid long-term branches (especially when working with others): The longer a branch stays alive, the more commits will accumulate on main, the more conflicts you'll get when you merge. Delete the branch after after merging.

Commands (Level 1)

Commands are how you interact with Git through the command line. Most take additional arguments, e.g. -m or --source.

In the commands below,

  • <commit> is any commit's hash ("first name" or "full legal name").
  • <file> can be multiple files separated by spaces for most commands.

Tip

If you ever get "stuck" inside a git command and ctrl+C doesn't work, press q instead.

Local Commands

  • git init: Create a brand-new repository that represents the current working directory.
  • git status: Use often, especially before commiting or when you're confused! It gives you a very useful snippet of info: Current branch, untracked files, and changes staged for commit.
  • git add <file>: Add <file> to the stage so it can be committed.
  • git commit <file>: ...
  • git branch <branch>: Create a branch named <branch> off of the latest commit. It does not automatically switch your current branch to the new one. For that, see git switch.
  • git branch -d <branch>: Delete the branch named <branch>.
  • git switch <branch>: Change the branch you're on to <branch>. Add -c to create a branch and switch to it.
  • git merge <branch>: Merge the changes from the branch named <branch>

Remote Commands

  • git clone <repository-url>: Create a local repository that has the same content as the remote at <repository-url>.
  • git fetch: Get the latest "updates" about the state of the branch on the remote. It does NOT modify or add files to the repo.
  • git push: Upload files to the upstream branch which is the remote's branch you want to push to (usually corresponding to the branch you're on).
    • This will fail if your branch is behind the upstream branch, meaning there are commits there that aren't on your branch. This forces you to git pull first, and deal with conflicts on your end, instead of immediately pushing conflicts to main (a good thing).
    • Use -u <remote-name>/<remote-branch> to set the upstream branch (only need to the first time you create a branch)
  • git pull: Performs git fetch followed by git merge <remote-name>/<remote-branch> to get files and merge them from the upstream branch which is the remote's branch you want to pull from (usually corresponding to the branch you're on)

(WIP) Level 2: Foundational Basics

Caution

Content in this section is incomplete and may be misleading/incorrect!

Concepts (Level 2)

  • change: When a file in the repository is created, deleted, moved, or has its contents modified.
    • Changes to/within files specified by the special Git ignore file, .gitignore, file do not count.

    • File content modifications are either block additions (+) or deletions (-).

    • blocks of a file are just adjacent lines that were changed in the same way (+ or -).

    • If you change a character of a single line, it counts as deleting the old line, and adding a new line with that character changed. This creates TWO blocks, not one, since + and - need separate blocks.

    • changes in files specified by the special Git ignore .gitignore file do not count.

  • SHA/hash: A 40-character-long hexadecimal code generated by hashing things like file contents, usernames, times, and other hashes. Git uses a type of hash called a SHA hash.
    • hashing is an algorithm that turns different inputs (regardless of size or type) into fixed-size and likely unique outputs.
    • Hashing has "high entropy" meaning small changes to the input will lead to a completely different looking output.
  • stage/index: The technical name for the staging area is the index.

Structure (Level 2)

Objects

Commits

Branches

Usage Philosophy (Level 2)

  • No need to comment out code and commit it: You have a full history of everything that was ever in the file, just delete the code.
  • Use an imperative tense when committing: e.g. "fix null dereference" instead of "fixed null dereference".

Commands (Level 2)

-- can be used to separate filenames and command line options in commands.

Local Commands

  • git init <name>: Create a brand-new folder called <name> with and put a brand-new repository .

TODO: Note that these are the original level 1 commands but they were too in-depth so now they're level 2

  • git status. Do this often, especially before commiting or if you're confused! It gives you a very useful snippet of info: Current branch, untracked files, and changes staged for commit.
  • git add <file> to stage changes so they can be committed.
  • git branch <name> creates a branch pointing the commit that HEAD is pointing to.
  • git switch <name> to change branch. Add -c to create a branch and switch to it.
  • git restore --source <commit> <file> to return a specific file back to its state at <commit>
  • Avoid git checkout since it has smaller commands with better names (git restore --source and git switch)

Remote Commands

  • git fetch to get the latest objects

  • git push to upload files and objects to the upstream branch which is the remote's branch you want to push

  • git revert <commit> creates a new commit that is the inverse of the target commit, effectively undoing the changes in it

  • git cherry-pick <commit> is like similar to rebase but for a single commit. Good for bug fixes (e.g. fix bug on main and cherry-pick into release branches)

(WIP) Level 3: Intermediate

Caution

Content in this section is incomplete and may be misleading/incorrect!

(WIP) Level 4: Advanced

Caution

Content in this section is incomplete and may be misleading/incorrect!

Concepts (Level 4)

  • shortened hash: Works because the high entropy of hashing means a few characters (7 by default) of the hash is likely still unique among the other shortened hashes.
    • For example, if there is only one shortened hash, b35e26a, used in the repo, then b35e26a1de46f907f09a7a941739e3ba3638e491 is obviously the only full hash it could be referring to.
    • In the unlikely case that there are multiple identical shortened hashes, Git will add one character of the full hash at a time until it is unambiguous, e.g. b35e26a could also refer to b35e26a165f0f409249723a293cde2f42a3b7f60 but b35e26a1d couldn't.

Glossary

Type Key

βœ… Official Git term πŸ’­ Unnofical Git term (conceptual or metaphorical) πŸ’» Widely used terms in software πŸ—£οΈ Commonly used name or convention

Definitions

Term ❔ Definition
ancestor βœ… A commit that can be reached by recursively following parents back from some child commit
block βœ… ...
branch βœ… A commit where history diverged. branches act like workspaces that can be switched between
change βœ… ...
child βœ… What a commit is to its parent. In Git, parents don't know their children
clone (Noun) πŸ’­ A local repository that was cloned (created to mirror) a remote repository
clone (Verb) βœ… Creating a new local repository that tracks upstream versions of its branches on a remote repository)
command βœ… ...
commit βœ… A snapshot of the entire repository identified by a unique hash
"first name" πŸ’­ Metaphorical version of a shortened hash used to identify a commit
"full legal name" πŸ’­ Metaphorical version of a hash used to identify a commit
git βœ… The command line interface for Git. Git commands always start with git
.git/ βœ… A special folder inside a repository containing all the info Git needs to track it
.gitignore βœ… A special file inside a repository that uses regex to specify files that shouldn't be tracked
Git βœ… An expressive and extensible VCS
GitHub πŸ’» A Git server provider. Also has added tools and social features. NOT affiliated with Git
hash βœ… Result of hashing used to uniquely identify a commit
hashing πŸ’» High entropy algorithm that turns non-uniform inputs into unique, uniform outputs
history πŸ’­ A chain of commits that give snapshots of a repository over its lifetime
index βœ… Historical/technical name for the staging area. The area used to prepare changes for a commit
local πŸ’­ On one's own computer. Not on the remote or server
main πŸ—£οΈ Common name for the trunk of a repository
master πŸ—£οΈ Common name for the trunk of a repository (main is now the preferred option)
merge βœ… The act of combining the commit histories of two branches
merge base βœ… The last ancestor commit that two branches have in common
origin πŸ—£οΈ Common name for the primary remote of a repository
parent βœ… The commit(s) that immediately preceeded a child commit. A backward chain of parents is what creates history
reachable βœ… A quality of an commit that is referenced somewhere in the chain of parents from some other commit
regex πŸ’» Short for regular expression. A standardized and widely used format for matching strings of text
region βœ… ...
remote βœ… A cloned repository's name for a URL that leads to a server version of the same repository.
repo repository βœ… A folder that Git is tracking and managing
server πŸ’» A computer that hosts a resource (a repository in this case) so other computers can access it
shortened hash πŸ’­ The shorter version of a hash used to make it more readable
stage (Noun) πŸ’­ Another name for the staging area
stage (Verb) βœ… Add a file or a change in a file to the staging area
staging area βœ… A better name for the index. An area used to prepare changes for a commit
SHA βœ… Type of hashing algorithm. Often used to refer to a hash in Git
tag βœ… A manually selected, human readable alias for a commit
tracked βœ… The state of a file that Git "cares about" and will record versions and changes of
trunk πŸ’­ The first branch that is automatically created when a repository is created
untracked βœ… The state of a file that Git does not "care about" and will not record versions and changes of
upstream βœ… The quality of the branch on the remote that corresponds to a branch on a local repository
URL πŸ’» Abbreviation of Uniform Resource Locator. Often in the form of a website link or a file path
VCS πŸ’» Abbreviation of Version Control Software. Git is a version control software
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment