This document is meant to serve as a cheatsheet for users learning git that I have compiled over my time as a student.
See Docs here for more in-depth information
- Arguments contained in
{--brackets}are considered optional - If an argument is prefixed with a
$, it is mandatory ie)${argument}
~$ git init {--bare}Creates a repository to begin tracking
- If initalized with
--bareflag establishes respository to be used for remote work
~$ git clone <linkToRepository>~$ git add ${filename}~$ git rm {--cached} ${file}- Removes file from filesystem and tracking
- If
--cachedflag, only remove $file from tracking not filesystem
Will open default editor for you to commit with message (this is not optional)
~$ git commit {--all or -a}Flag commit all staged changes)
~$ git commit --amend Commits staged changes to last commit (incase you forgot something)
~$ git statusBehavior:
- Files staged to be changed
- Files not included in repo
~$ git branch {-d, -D} ${newBranchName}Behavior:
- Creates a new branch with copies of original files. Note this does not checkout the new branch
- if using
-dflag followed by a branch name will delete that branch - if using
-Dflag forcefully deletes that branch - Note these deleted branches are local changes that must be committed
~$ git checkout ${filename} {branch}Behavior:
- If branch no specified, reverts uncommitted changes to the modified file
- Brings
$filenamefrom$branchinto current branch as uncommitted changes - Moves
HEADto$branchif filename unspecified
~$ git checkout BranchNameBehavior:
- Switches to
$BranchName
~$ git merge ${TargetBranch}- Merges
$TargetBranchto the currently checkedout branch - If conflict between branches will show diff between branches and ask with changes you want to keep
~$ git rebase ${branch} #ACTION dependent on working branch Let feature = non master branch
Let release = master branch or branch that does not recieve many updates\
-
Case: bringing release changes to feature branch will advance feature branch to latest release commit and append any changes made in feature
-
Case: bringing feature changes to release branch will bring all feature commits from last identical commit to release
Visualize:
master : m1 - m2 - m3
|
feature : m2 - f1 - f2
We want to move f1 and f2 commits to m3 without overriding the m3 commit
(in feature)
git rebase master
master : m1 - m2 - m3
|
feature : m3 - f1 - f2
now we can either merge to master
or
(in master)
git rebase feature
master : m1 - m2 - m3 - f1 - f2
|
feature : m3 - f1 - f2Adding files to a .gitignore file will make file not added to the repo not be flagged by git status
Files can be exculed using:
- Unix wildcards.
- ex)
*.txt
- ex)
- Directories
- ex)
{directory_name}/
- ex)
- Files
- ex)
placeholder.exe
- ex)
- Exception to pattern
!- ex)
!something.txt(even though files ending in .txt are ignored something.txt is tracked)
- ex)
Secnario: Your program generates some output files in your working directory that you dont plan on tracking.
~git clean ${-dfi} {-e ${pattern}} # at least one requiredFlags:
-
-d, remove untracked directories aswell as untracked files -
-f, --force, unless force is specified, no files will be deleted -
-i --interactive, interactively clean git repo -
-e {pattern}, --exclude {pattern}, exclude pattern from removal
~$ git diffBehavior: to see unstaged changes compared to commited files in repo
~$ git log - shows changes between commits
~$ git remote add ${remoteName} ${url}-
adds remote repository named
$remoteNameat$url~$ git remote remove ${remoteName}
-
removes
$remoteNameas a commitable repository
~$ git push ${remoteName} {--delete} ${RemoteBranch}- Pushes changes to online repo for others to clone or fetch
- If
--deletebefore the branch you want to push will delete the branch locally
~$ git fetch ${remote branch name} {-all}- Clones remote branches to local to be checkout out
~$ git pull ${remoteName} ${remoteBranchName}- Fetch then merge
$remoteBranchNameintoHEAD