Repository setup
Clone an existing repository
git clone <repo-url>
Create a repository from a local folder
Create a folder, place the code inside it, and initialize Git.
git init
Add the remote repository.
git remote add <shortname> <repo-url>
# <shortname> is the name you want to give the remote
origin, but Git allows another short name.
Configure Git identity
git config --global user.name "FIRST_NAME LAST_NAME"
git config --global user.email "MY_NAME@example.com"
Inspect or change a remote
git remote -v
git remote set-url origin <newGit>
git remote rm <remote_name>
Add, commit, and push
Basic workflow
git add --all
git commit -m "contents wanting to note"
git push origin <branch_name>
Undo Git add
git restore --staged <file>
History and differences
See commit history
git log
git log --pretty=oneline
git diff
Use git diff to inspect differences between the current working state and commits, or between two commits.
git diff
git diff <commitName>
git diff <commitName1> <commitName2>
git diff <filename>
git show
In these notes, git show is used to inspect the changes introduced by a specific commit.
git show <commitName>
git show <commitName1> <commitName2>
git show <commitName1> <commitName2> --color-words
Undo and reset
Commands using --hard, --force, or history rewriting can remove local changes or change remote history.
Undo a commit before pushing
git reset HEAD~1
Reset to a previous point
git reset --hard HEAD
git reset --hard HEAD^
git reset --hard <commit_name>
git push origin HEAD --force
Keep current code while removing later commits
The original notes use a soft reset when the goal is to move the branch pointer while preserving the current work for recommitting.
git reset --soft <commit>
Remove untracked files and directories
git clean -fd
# -f: force
# -d: remove directories
Tags
Create and push a tag
git tag -a <tagName> -m "message"
git push origin <tagName>
git push --tags
Tag a particular commit
git log --pretty=oneline
git tag -a <tagName> <commit> -m "message"
git push origin <tagName>
Inspect, delete, or rename tags
git tag -n
git tag
git tag -d <tag_name>
git ls-remote --tag
git push origin --delete <tag_name>
git tag <new_tag> <old_tag>
git push --tags
Branches and stash
Create and switch branches
git branch <name> <commit>
git checkout <branch>
git push origin <branch_name>
Delete a branch
git branch -d <branch_name>
git push origin --delete <branch_name>
Merge
Check out the branch that should receive the changes, then merge the source branch into it.
git merge <branch_to_merge_from>
Stash temporary work
git stash push -m <message>
git stash list
git stash pop --index <index>
git stash apply --index <index>
git stash clear
Submodules
Add a submodule
git submodule add <url_to_repo> <projectfolder>
Initialize submodules after cloning
git submodule update --init --recursive
Clone together with submodules
git clone --recurse-submodules <repo-url>
Git LFS
Initialize and track files
git lfs install
git lfs track "*.psd"
git add .gitattributes
Migrate existing Git history to LFS
# Dry run
git lfs migrate info --everything --include="*.zip"
# Migrate history
git lfs migrate import --everything --include="*.zip" --verbose
# Current history scope only
git lfs migrate import --include="*.zip" --verbose
Pull missing LFS files
git lfs pull
Files and .gitignore
Remove a tracked file
git rm --cached file1.txt
git commit -m "remove file1.txt"
git rm file1.txt
git commit -m "remove file1.txt"
Create a .gitignore file
touch .gitignore
Example patterns from the original notes:
*.exe
*.rar
*.zip
venv_*
**/.vscode/
Refresh tracking after changing .gitignore
git rm -r --cached .
git add .
git commit -m "Update .gitignore"
Proxy and other commands
Proxy configuration
# Check current proxy configuration
git config --global --get-regexp http.*
# Configure proxy
git config --global http.proxy http://proxyUsername:proxyPassword@proxy.server.com:port
# Remove proxy
git config --global --unset http.proxy
Repository size and cleanup
git count-objects -vH
git gc --prune=now --aggressive
Quick reference
git clone <repo-url>git statusgit log --pretty=onelinegit remote -vgit diffgit show <commit>