Git Commands
Terms
- working directory : Where you are coding (can be same as git directory)
- staging area : Where you commit changes from working directory to git directory (but not pushed in)
- git directory : Where your local repository is hosted
- local : Your git repository on your computer
- remote : Git repository NOT on your computer, and you are pushing / pulling from
- origin : Where the repository was ORGINALLY cloned (i.e. master version)
- pull : Fetch from remote to local
- push : Push from local to remote
- commit hash : ID of commit
- tree : contains a list/history of git changes
- upstream : changes/commits that are in origin
Setting up
-
git version
- Checks your git version
-
git config --global user.name "Your Name"
- Your commit name
- Leave "<>" blank to check current name
-
git config --global user.email "youremail@gmail.com
- Your commit email
- Leave "<>" blank to check current email
-
git config --list
- Shows all config settings
-
git init
- Creates a git repository at current directory (creates .git directory)
- remove .git directory to remove git status
-
git add <filename>
-
-A
add all files in current directory - Stages all files in current directory (adds to git repo)
-
-
git clone <URL>.git <cloning_directory>
- Leaving
<cloning_directory>
blank to clone at current directory - URL should end with a .git to specify its a git repository
- Some git services like GitHub are automatically detected so doesn't need the .git
-
--depth <no.>
determines amount of commits to clone -
--recurse-submodules
clones submodules
- Leaving
-
git remote -v
- Check if remote is the origin
-
git branch -a
- List all branches in the local and remote
Basic Commands
-
git help <command>
- Gets help for specific command
- Alt:
git <command> --help
-
git status
- Shows status, includes:
- Untracked files (fresh repo), Uncommited/commited changes, Conflicts
- Use before pushing and pulling to check for possible conflicts!
-
git add <filename>
-
-A
add all files in current directory - Stages files (adds to commit)
-
-
git commit -m "Commit Message"
- Commit changes
- Use without
-m
option to check full commit status in a code editor - Use with
--amend
to amend last commit
-
git log
- Shows recent changes
-
q
to quit,Return
for next line
-
git diff
- Shows any differences
- optional:
<commit-hash> <commit-hash>
;<branch-name> / head
- latest ----stat
for a condensed summary (file changes) -
q
to quit,Return
for next line
-
git fetch
- Fetches (download) from remote to local
-
git pull (origin) (branch)
- Pulls from remote to local
- bracketed items are optional (remove brackets!):
-
origin
- specifies from origin directory -
branch
- specifies branch name, current branch by default
-
git push (origin) (branch)
- Pushes from local to remote
- bracketed items are optional (remove brackets!):
-
origin
- specifies from origin directory -
branch
- specifies branch name, current branch by default -
-u
- set upstream (for pushing in a new branch from local to remote-origin)
-
git checkout <commit-hash>/<branch-name>
- Switch your current git repository to a specific commit OR branch
-
git checkout main
to switch back to latest -
git checkout head
ADVANCED: Detaches current commit from working tree - Leave option empty to get the current status
-
git branch <branch-name>
- Creates a new branch based on current commit
- Use checkout to specify specific commit / switch to new branch
- Leave option empty to get the current branch you are on
-
git stash
- Stash current changes
-
git stash show
- Show stashed changes
-
git stash pop
- Pop stash changes (unstash)
-
git mv <file_location> <new_location/new_name>
- Rename / move existing files to another name / location
- Useful for changing file name case (windows is non-case sensitive and won't detect file name case changes)
-
git mv src/graphics.cpp src/GRAPHICS.cpp
- renaming to uppercase -
git mv src/graphics.cpp src/graphics/graphics_auto.cpp
- renaming and moving
Merging
- Squash and merge (makes your commitS into one large commit)
- Merging from
bugfix
branch INTOmaster
-
git checkout master
-
git merge --squash bugfix
- resolve conflicts....
-
git rebase
- resolve conflicts....
-
git commit
-
- Merging from
- Updating your own branch with stuff from master
-
git checkout <your_branch>
-
git rebase master
- resolve conflicts....
-
git force -f
- Why force?
- Since rebase rewrites commit history, the previous version of the branch is no longer valid.
- A force push replaces the old local branch history with the rebased one, removing the old, now obsolete commits.
- Why force?
-
- Merge with rebase (retains all your commits and move them over)
- Rebasing
bugfix
INTOmaster
-
git checkout bugfix
-
git rebase master
- resolve conflicts....
-
git checkout master
-
git merge bugfix
- push!
-
- Rebasing
Git Large File Storage (LFS)
When adding files larger than 100mb, versioning for it will no longer be available, forcing the repository to be completely usable, use git LFS to manage these large files
-
git lfs install
: install LFS if you dont have it -
git lfs track "filename"
ORgit lfs track "*.psd"
<- tracks all files of specified type -
git add .gitattributes
- if git attributes doesn't exist! git attributes is used for tracking LFS files -
git add -A
-
git commit -m "Commit message"
Advanced Commands
WARNING : Commands below will might result in an unstable repository or overwritten changes when used incorrectly! Use with care!
DANGER LEVEL
- No danger (but access advanced data)
- overwrites local changes (if no errors)
- overwrites local & can affect remote changes
- overwrites local & overwrites remote changes (poweruser!)
-
git rebase
[1]- Compare between local and remote and tries to fix tree
-
git rebase origin/<branch name>
[2]- i.e.
git rebase origin/master
rebasesmaster
branch to current branch, basically apply master branch changes into current branch
- i.e.
-
git reset
[1]- REMOVES unstaged / uncommitted changes
- Resets repository to last state
-
--hard
to force reset -
<commit-hash>
to reset to specific commit
-
git clean -fxd
[1]- Force REMOVES all unstaged / uncommited changes
-
git push -f
[3]- Does a force push, this REWRITES the remote tree with your local tree!
-
git reset --soft HEAD~3
[2]- Rewinds/undo the last 3 commits from current head, change 3 to any number
-
git add dir/dir2/ -f
[2]- Force adds/stages directory, OVERWRITES gitignore
- Replace with
<filename>
for file
-
git checkout --orphan
[2]- Makes a new orphan branch with NO commit history
-
git rebase -i --root
[3]- Allows manual rebasing in an interactive editor FROM starting of repo (root)
- Changes made here will MESS UP your local tree unless you know what you are doing
-
git rebase
to reset rebased changes -
git rebase -i <commit-hash>
to specify starting commit -
--root
specifies starting commit to be from first
-
git rebase -i HEAD~3
[3]- Allows manual rebasing in an interactive editor FOR last 3 commits
- Change the respective command in your text editor, i.e.
pick
todrop
; to affect the commit
-
git fsck --unreachable
[0]- show list of unreachable (dangling) commits
-
git gc --prune=now
[3] - removes unused history!- remove all unreachable (dangling) commits
Cross-GUI Interations
-
github .
- open github GUI at current location
- replace
.
with folder path for specified location
-
explorer .
- open file explorer at current location
- replace
.
with folder path for specified location
- typing in file explorer address path
-
cmd
- open command prompt -
powershell
- open powershell
-
Helpful Windows CMD Commands
-
cd ..
- Go to parent directory
-
cd <directory_name>
- Go to next specified directory
-
dir
- Display all files in current directory
-
help dir
for list of sorting options -
dir /os
- file ordered by smallest size first -
dir /o-s
- file ordered by largest size first
-
rmdir /s /q <directory_name>
- Force removes a directory and all its contents
-
/s
Removes all directories and files in specified directory -
/q
Quiet mode: Do not ask if "Are you sure"
-
mkdir <folder_name>
- Creates a new folder in current directory
-
echo your_text_here > filename.extension
- Create file with echo
-
code filename.extension
- Create file with vscode
-
notepad filename.extension
- Create file with notepad
-
type filename.extension
- Show file contents
-
del <file_name>
- Delete singular file
-
/f
Force delete (read-only etc) -
/q
Quiet mode: Do not ask if "Are you sure" -
help del
for list of deleting options
-
set <variable_name>=<contents>
- Stores a variable, persistent in current cmd session
-
<contents>
can be a command with%command%
- i.e.
set curpath=%cd%
set variable curpath to current directory
-
echo %<variable_name>%
- Echo variable
- Surround with
"<>"
to create a string
-
%<variable_name>%
- Utilise variable, use cases: for specifying directories etc
-
<filename>.exe/.bat
- Execute said file, .bat is a bash script
- .sh (shell script) is linux equivalent
-
<any-cmd> > filename.extension
- Pipe results to specified file
Git Configurations
.gitignore
Excludes certain files from being pushed.
Can be overriden with git add <> -f
https://git-scm.com/docs/gitignore
- prefix
!
- Should be placed after all exclude criteria
- Applied to any parameters, include said
-
*.extension
- Exclude file extension
-
<folder_name>/
- Exclude everything in folder
- Use
[Dd]
for wildcard, specified allows D or d - i.e.
[Dd]ebug/
.gitattributes
Various git config attributes Involves language statistics watcher https://git-scm.com/docs/gitattributes
- prefix
*.extension
- Exclude file extension
- i.e.
*.tex linguist-vendored
- prefix
<project-root>/<directory-1>/<directory-2>/**
- Exclude directory
- i.e.
opengl-dev/lib/** linguist-vendored
- prefix
<filename.extension> -
- Exclude specific file
- i.e.
jquery.js -linguist-vendored
- option
linguist-vendored
- Exclude as vendored code (from library)
- option
linguist-documentation
- Exclude as documentation
- option
linguist-generated
- Exclude as generated files
- option
linguist-language=<language>
- Reclassifies file to another language
- i.e.
*.rb linguist-language=Java
.gitkeep
Placeholder file for empty directories
Signing commits on github
-
git config commit.gpgsign true
- Enable signed commits for current repository, switch to
false
to disable
- Enable signed commits for current repository, switch to
-
git config --global commit.gpgsign true
- Enable signed commits globally, switch to
false
to disable
- Enable signed commits globally, switch to
Setting up (Using GPG)
- Install GPG command line tools https://www.gnupg.org/download/ For windows, get Gpg4win
- Locate gpg.exe
If following default installation path, should be under:
C:\Program Files (x86)\GnuPG\bin
cd to said directory -
gpg.exe --full-generate-key
- kind of key: Enter to accept default
- key size: Enter to accept default
- length of time: Enter to accept default (doesn't expire)
- Verify selections above
- Enter your user ID information.
For a private email address, use github provided
no-reply
! Yourno-reply
github email is viewable under github website settings >Emails
(https://github.com/settings/emails) It will look something like thisID+USERNAME@users.noreply.github.com
or thisUSERNAME@users.noreply.github.com
- Type a secure passphrase
- Get list of keys with:
gpg.exe --list-secret-keys --keyid-format=long
- copy the ID, ID should be under
sec
right after first\
For example:sec 4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
3AA5C34371567BD2
is the ID -
gpg.exe --armor --export <INSERT_ID_HERE>
- Copy the GPG key generated
- Go to your github website settings >
SSH and GPG keys
(underAccess
section) (https://github.com/settings/keys) - Click
New GPG key
- Confirm and authenticate
-
git config --global gpg.program "C:\Program Files (x86)\GnuPG\bin\gpg.exe"
replace path there to path to yourgpg.exe
if different -
git config --global gpg.program gpg
-
git config --global user.signingkey <INSERT_ID_HERE>
- Profit! Your key is stored and automatically applied as long as you are using the same device with Cpg4win installed.
Removing passphrase
-
gpg --passwd <INSERT_ID_HERE>
- Enter your passphrase as normal
- Keep the new passphrase empty and accept the warnings
- Repeat step 3
- Passphrase is now removed for this device