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.
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).
-
repository/repo: A "folder" that represents an entire project and its history. -
change: When a file in therepositoryis created, deleted, moved, or has its contents modified.- A file is
untrackedif Git hasn't been told to care about it'schanges. Newly added files will be untracked until you use acommandlikegit addon them.
- A file is
-
command: The way you tell Git what to do. All commands start withgitand havearguments, e.g.git add my_file.txt. -
commit: A snapshot of the project at the exact moment in time after you apply a group ofchanges.-
Commits have
changes, a message about the commit, and an author (you or your teammate). -
Each commit has a unique "name" called a
hashbased on the content of the commit. For example:b35e26a1de46f907f09a7a941739e3ba3638e491is a commit's "full legal name".
b35e26ais that same commit's shortened, more readable, "first name". -
Each commit knows the "name" of the commit that came before it: Its
parentthat tells us how the project looked before the changes of thechildcommit 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
childcommit'sparent, then thatparent'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 currentchildsnapshot. This technique is the foundation of Git, and is how it knows how the repository changed and looked at any commit.
-
-
history: -
stage: Beforechangescan be committed, they have to be added to thestage, which is calledstaging changes. This lets you control exactly what files and changes you group into a singlecommit. This helps you organize commits logically and keep personal info, incomplete features, build outputs, etc. on only your computer. -
branch: A point in history (acommit) 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 bymergingthem.-
Each branch has a name and acts like a separate workspace that you can
committo. -
You can only use one at a time but you can easily
switchbetween them. -
Each repository starts with 1 branch, the
trunk(Git term): It's usually namedmainormaster(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
mergeit back into the other branch. Despite how it sounds, the branch will still exist after this BUT you would typically want todeleteit after anyway. -
deletinga 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 onebranchinto 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
Iand beyond). -
If neither branch changes a specific
blockof a file, do nothing. -
If one branch changes a specific
blockof a file, use the modified block in the merged version of a file. -
If both branches change a specific
blockof a file, that's called amerge conflictand 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
remoteserver 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!
-
-
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 tomain. -
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 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'shash("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.
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 thestageso 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, seegit switch.git branch -d <branch>: Delete the branch named<branch>.git switch <branch>: Change the branch you're on to<branch>. Add-cto create a branch and switch to it.git merge <branch>: Merge the changes from the branch named<branch>
git clone <repository-url>: Create a localrepositorythat has the same content as the remote at<repository-url>.git fetch: Get the latest "updates" about the state of the branch on theremote. It does NOT modify or add files to the repo.git push: Upload files to theupstream branchwhich 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
behindtheupstream branch, meaning there are commits there that aren't on your branch. This forces you togit pullfirst, and deal with conflicts on your end, instead of immediately pushing conflicts tomain(a good thing). - Use
-u <remote-name>/<remote-branch>to set theupstream branch(only need to the first time you create a branch)
- This will fail if your branch is
git pull: Performsgit fetchfollowed bygit merge <remote-name>/<remote-branch>to get files and merge them from theupstream branchwhich is the remote's branch you want to pull from (usually corresponding to the branch you're on)
Caution
Content in this section is incomplete and may be misleading/incorrect!
change: When a file in therepositoryis 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
blockadditions (+) or deletions (-). -
blocksof 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.
-
changesin files specified by the special Git ignore.gitignorefile do not count.
-
SHA/hash: A 40-character-long hexadecimal code generated byhashingthings like file contents, usernames, times, and other hashes. Git uses a type of hash called aSHA hash.hashingis 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 theindex.
- 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".
-- can be used to separate filenames and command line options in 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 thatHEADis pointing to.git switch <name>to change branch. Add-cto 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 checkoutsince it has smaller commands with better names (git restore --sourceandgit switch)
-
git fetchto get the latestobjects -
git pushto upload files andobjectsto theupstream branchwhich 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)
Caution
Content in this section is incomplete and may be misleading/incorrect!
Caution
Content in this section is incomplete and may be misleading/incorrect!
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, thenb35e26a1de46f907f09a7a941739e3ba3638e491is 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.
b35e26acould also refer tob35e26a165f0f409249723a293cde2f42a3b7f60butb35e26a1dcouldn't.
- For example, if there is only one shortened hash,
β Official Git term π Unnofical Git term (conceptual or metaphorical) π» Widely used terms in software π£οΈ Commonly used name or convention
| 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 |