Terminology used:
-
origin: the default name git gives to the remote repository you cloned from (origin = https://github.com/...) -
tracked: whengit addis used -
staged: whengit commitis used -
linear: meaning commits follow a linear pattern likeA -- B -- C -- D -
non-linear: meaning commits don't follow a linear order
A -- B -- C
\
D -- E
\
M (merge commit)
When applied, this commit with ___.
Try and explain everything you do in this format, if its clear and makes the commit!
Command: git init
Parameters:
--initial-branch: sets the initial branch in a newly created repository
Description:
Creates an empty git repository - basically a .git directory with sub-directories.
Command: git status
Parameters: NONE
Description:
Shows the difference between the staged area and working directory (the HEAD commit and the index file)
Use Case:
See modified, staged, and untracked files. Whats staged but not committed?
Command: git diff
Parameters:
--stagedis staged changes that have not yet been committed.-wignore the white spacce
Description:
Compared the changes made in your working directory to the staged directory.
Use Case:
Whats changed but not staged?
Command: git show
Parameters:
- HASH ID
Description:
Show changes made in a commit
Command: git add <file>
Parameters:
-p/--patchhunks specific code to add them to the index. Gives you a chance to review the difference before adding modified contents to the index.
Description:
Adds files to be tracked.
Command: git commit
Parameters:
- NONE an interactive environment, allows for setting long-descriptions by using an empty space
-m "<message>"adds a message describing the commit-a "<message>"does agit add .and provides an interactive environment.-am "<message>"just like above but also does agit add .-pexactly likegit add -pjust remember not to do agit addbefore--amendallows you to edit commit message, and adds the most recent changes to your latest commit
Description:
Commits staged files and records a snapshot of the project.
A git commit should only be selective and include relevant changes
Use Case:
Saving a stable point, to revert back to in the future.
Command: git log
Parameters:
--onelineexactly what the name says (keeps simplicity by including commit hash and message)--graphgraphs logs in a horizontal format--oneline --grapha simplified graph-plogs actual changes in a file--no-mergesremoves the merge commits--decoratedecorates with merge commits, branches, tags--parentsshows parents of each commit
for (commit/s) filters:
--grepfor message--afterand--beforefor dates--authorby the author--for a specific file
to compare:
git log feature/login..main all commits that are in main but not in feature/login
Description:
Shows a timeline of project changes and commits.
Command: git branch
Parameters:
- NONE creates a new branch
-ashows all the branches-mrenames a branchbranch/subbranchadds sub-branches
Description:
Managing branches in a repository
Long-running branches are branches that have been in the complete life-cycle. You mirror this structure (used for releasing code)
Short-lived branches for new features, bug fix and refactoring
Command: git checkout
Parameters:
<hash | commit id>checkout to a specific hash<branch>checkout a branch<tags>checkout to a tag-bcreates a branch and switches automatically
Description:
Switch to a commit, branch, or tag.
Command: git remote add
Parameters:
<origin-name> <url>
Description:
Links your local repository to a remote one
Use Case:
Command: git fetch
Parameters:
--depth to set how deep its history will go
Description:
Downloads objects and refs from a remote without merging
Use Case:
Check for updates but don't touch my files.
git fetch origin
git log main..origin/main
Command: git pull
Parameters:
<origin> <branch>--rebasesolve merge conflicts, meaning git will first pull changes, then reapply your unpushed commits on top of the latest version of the remote branch (no need to merge).
Description:
Acts exactly like fetch but also merges (or rebase) the two repositories
Command: git push
Parameters:
<origin> <branch>--set-upstream/-uflags a local branch to a remote branch. (acts more as a setting option, set it once and forget about it, it links yourbranchto theorigin/branch)--tagspush all the tags
Description:
Uploads the local repository with the staged changes to the remote
Command: git merge
Parameters:
<branch>specifies the branch to merge from--no-ffprevents git from making a linear history (no-fast-forward)
Description:
Combines two branches into one
Use Case:
Merge is additive meaning history is preserved.
Fast forwarding:
In the terminology, i mentioned that we have two types of repositories, linear and non-linear. This same concept can be applied to git merge, a linear merge will have all the commits after the commit, otherwise it git will keep the current branch and make a new commit with the branch creating a non-linear history.
This is useful when we have a collection of closely related branches.
Command: git rebase
Parameters:
<branch>specifies the branch to rebase from-iprovides an interactive setup for rebasing
Description:
Reapplies commits on top of another branch.
Use Case:
Clean up local commit history. Rebase is reconstructive meaning history is rewritten.
Command: git cherry-pick
Parameters:
-
<commit-hash>the hash of the commit you want to cherry pick -
<hash1> <hash2> <hash3>/A..Bfor selecting multiple hashes
Description:
Copies a specific commit from one branch and applies it onto the current branch. Notes that it duplicates commits.
Use Case:
When you've mistakenly committed to the wrong branch you use cherry-pick to revert.
Command: git reset
Parameters:
--hardcompletely discards commits and changes.--softuncommit changes but keep them staged (moves HEAD back to a previous commit).--mixeduncommit and unstage changes.<commit>you can also do some cool stuff likeHEAD~1meaning last commit from head
Description:
Resets branch to a specific commit
Use Case:
You committed too early or made a typo in the message.
Command: git revert
Parameters:
<commit-hash>the hash of the commit-ntells git only to look at the changes
Description:
Creates a new commit that undoes changes from a previous one while keeping history intact.
Use Case:
You pushed a commit that breaks stuff, you cant change history so you revert it instead.
Command: git stash
Parameters:
-pstash only some fileslistview saved stashesapply {stash}reaply a stash
Description: Temporarily saves uncommitted changes.
Use Case: You've made new changes yet need to prioritize something new, without committing save your code and commit later.
Command: git mergetool
Parameters: NONE
Description:
Launches a graphical or diff tool to help resolve merge conflicts.
Command: git reflog
Parameters: NONE
Description:
Acts as gits "undo history", meaning even if you have rebased or reset you can see where your branch used to point
Use Case:
You mistakenly delete a branch, you can use reflog to get the hash and checkout back to it
git checkout def5678
git reset --hard def5678
Command: git submodule
Parameters:
add <repository> self explanatory
update --remote pull all the latest versions of the submodules
Description:
A git repository inside another git repository.
FYI: when others clone your repo they should also initialize your submodules with submodule init or clone --recurse-submodules
Also submodules track commits and not branches (meaning when you clone a repository it goes to the exact commit at where the submodules was added, yet in web based github it opts to look at the default branch)
Use Case:
When wanting to use other repositories in your current repository you can have it set as a submodule
Command: git worktree
Parameters:
list shows all the worktrees
remove ../ removes a worktree
add ../ -b new-branch adds a worktree
Description:
Allows for having multiple working directories for a single git repository. It allows for multiple workspaces in the same repository.
Use Case:
You want to work on multiple branches without having to switch all the time
Command: git config
Parameters:
--global alias.l "log --oneline --graph"
Use Case: Easily set aliases for commands you use all the time but don't want to type out the whole name
Command: git tag
Parameters:
NONE lists all the tags
v1.0 a pointer to the latest commit
v1.0 <hash> to a commit in the past
-a v1.0 -m "<message>" includes metadata
Description:
A git tag is a pointer to a specific commit in git.
Use Case:
Acts like a sticky note like releases and milestones. They don't move after they're created.
Command: git clean
Parameters:
-n show only what would be deleted
-f actually delete the files
Description: Removes untracked files from your currently working directory
Use Case: Remove artifacts, temp files, and debug outputs.
Command: git clone
Parameters:
--depth: add depth for the history of the repository
Description: Cloning a repository
Command: git bisect
Parameters:
startstart the bisectbad <hash>mark the bad comitgood <hash>mark the good commitresetfinish when the bisect is done
Description: Helps you find a specific commit that introduced a bug in the code using binary search.