Table of Contents
- Git CLI snippets
- Notes & Additional information
- Git snippet
- Git Command Reference
- Repository Information
- Staging Changes
- Commits
- Branches
- Remote Branches
- Remotes
- Pulling and Fetching
- Merging
- Rebasing
- Comparing Changes
- Viewing Files
- Stashing
- Undo Changes
- Tags
- Working with Bare Repositories
- Exporting Files
- Cleaning
- Troubleshooting
- Common Workflows
Git CLI snippets
- Move recent changes into a separate local branch
git stash git checkout -b <branch-name> git stash pop
Note: You don’t need to include
-bif the local branch you are checking out to already exists.
- Remove local branch
git branch -d <branch-name> - Merge two local branches together
- Switch to target branch you want to merge changes into
git checkout <branch-name> - Merge branch and resolve conflicts
git merge <branch-with-changes>
- Switch to target branch you want to merge changes into
- Get upstream URL for local branch
git rev-parse --abbrev-ref --symbolic-full-name @{u} - Get remote branches from git repo
git ls-remote --heads <repo-url> - Clone specifc remote branch to local
git clone -b branch-name <repo-url> - Delete remote branch on local machine
git push origin --delete branch-name - Create new local branch and add to remote
git checkout -b <branch-name> git push -u origin <branch-name> - Change URL used for “origin” remote
git remote set-url origin <link-here> - Ignore vim swap files
*~
Note: Add this to your .gitignore and commit changes
- Cache ( store ) git credentials
git config credential.helper store - Reset all changes made on branch to HEAD
git reset --hard HEAD - Store git credentials permanantly
git config --global credential.helper store - Undo recent changes ( commits )
git reset --hard HEAD
Note : This throws away all your recent uncommited changes
- Create new local branch
git checkout -b <branch name> - Push local branch to remote repo as separate branch
git push <remote-name> <local-branch-name>:<remote-branch-name>
WARNING: DO NOT omit the
:<remote-branch-name>or the remote branch will be DELTED
- Get branch name
git rev-parse --abbrev-ref HEAD - Push all local branches to remote
git push --all -u
Note : This is handy if you aren’t sharing a repo with someone, I might use this more often than not since i’m a hobbyist with no partners on my projects. ( As of yet )
- Change default editor for commits
git config --global core.editor vim
Note : This sets the default editor to vim
Notes & Additional information
Where are my creds stored when using git config credential.helper store ?
The credentials are stored on disk in a text file in the home directory of the user , this location is consistent when using both --global and --system arguments , this file by default only gives read / write permissions to the user who initiated the command. The file is stored under ~/.git-credentials. Do note however that this file is in plain text so anyone who can compromise your account ( or malicious NPM modules ) will be able to view your password and token.
Git snippet
Git Command Reference
Repository Initialization
Initialize a New Repository
git init
Clone a Repository
git clone user@server:/srv/repo
Clone a Specific Branch
git clone --single-branch -b customer-map user@server:/srv/repo
Clone Into a Specific Directory
git clone user@server:/srv/repo my-project
Repository Information
Show Repository Status
git status
Show Commit History
git log
Compact Commit History
git log --oneline
Graph View
git log --oneline --graph --all --decorate
Show Current Branch
git branch --show-current
Show Remotes
git remote -v
Staging Changes
Stage a File
git add file.txt
Stage All Changes
git add .
Unstage a File
git restore --staged file.txt
Stage Deleted Files
git add -u
Commits
Commit Changes
git commit -m "Added feature"
Commit All Tracked Changes
git commit -am "Updated files"
Amend Last Commit
git commit --amend
Amend Without Changing Message
git commit --amend --no-edit
Branches
List Local Branches
git branch
List Remote Branches
git branch -r
List All Branches
git branch -a
Create Branch
git branch feature-x
Create and Switch Branch
git checkout -b feature-x
or
git switch -c feature-x
Switch Branch
git checkout main
or
git switch main
Rename Current Branch
git branch -m new-name
Delete Branch
git branch -d feature-x
Force Delete Branch
git branch -D feature-x
Remote Branches
Push New Branch
git push -u origin feature-x
Push Existing Branch
git push
Fetch Remote Branches
git fetch
Create Local Branch from Remote
git checkout -b feature-x origin/feature-x
Track Existing Remote Branch
git branch --set-upstream-to=origin/feature-x
Delete Remote Branch
git push origin --delete feature-x
Remotes
Add Remote
git remote add origin user@server:/srv/repo
Change Remote URL
git remote set-url origin user@server:/srv/repo
Remove Remote
git remote remove origin
Rename Remote
git remote rename origin production
Show Remote Details
git remote show origin
Pulling and Fetching
Fetch Updates
git fetch
Pull Changes
git pull
Pull Specific Branch
git pull origin main
Pull Unrelated Histories
git pull origin main --allow-unrelated-histories
Merging
Merge Branch into Current Branch
git merge feature-x
Merge Without Fast Forward
git merge --no-ff feature-x
Abort Merge
git merge --abort
Rebasing
Rebase Current Branch onto Main
git rebase main
Rebase Onto Remote Main
git fetch
git rebase origin/main
Abort Rebase
git rebase --abort
Continue Rebase
git rebase --continue
Comparing Changes
Compare Working Tree to HEAD
git diff
Compare Staged Changes
git diff --cached
Compare Branches
git diff main..feature-x
Show Changed Files Between Branches
git diff --name-status main..feature-x
Compare Remote Branches
git diff origin/main..origin/feature-x
Viewing Files
List Files in Commit
git ls-tree -r HEAD --name-only
Show File History
git log -- file.txt
Show File Contents from Branch
git show main:file.txt
Stashing
Save Changes
git stash
Save with Description
git stash push -m "Work in progress"
List Stashes
git stash list
Restore Most Recent Stash
git stash pop
Apply Without Removing
git stash apply
Delete Stash
git stash drop stash@{0}
Undo Changes
Restore File
git restore file.txt
Restore Entire Working Tree
git restore .
Reset File to HEAD
git checkout HEAD -- file.txt
Soft Reset
git reset --soft HEAD~1
Mixed Reset
git reset HEAD~1
Hard Reset
git reset --hard HEAD~1
Reset to Remote
git fetch
git reset --hard origin/main
Tags
Create Tag
git tag v1.0
Annotated Tag
git tag -a v1.0 -m "Release 1.0"
Push Tag
git push origin v1.0
Push All Tags
git push --tags
Delete Local Tag
git tag -d v1.0
Delete Remote Tag
git push origin :refs/tags/v1.0
Working with Bare Repositories
Create Bare Repository
git init --bare /srv/repo
Clone Bare Repository
git clone user@server:/srv/repo
Show References
git show-ref
Exporting Files
Archive Branch
git archive --format=tar.gz HEAD -o project.tar.gz
Export Branch to Directory
git archive main | tar -x -C /tmp/export
Cleaning
Show Files to be Removed
git clean -nd
Remove Untracked Files
git clean -fd
Remove Untracked Files and Directories
git clean -fdx
Troubleshooting
Detect Current Upstream
git rev-parse --abbrev-ref --symbolic-full-name @{u}
Show Tracking Branches
git branch -vv
Find Unmerged Branches
git branch --no-merged
Find Merged Branches
git branch --merged
Verify Repository
git fsck
Show Configuration
git config --list
Show Origin URL
git config --get remote.origin.url
Show Global Username
git config --global user.name
Show Global Email
git config --global user.email
Common Workflows
Create Feature Branch
git checkout main
git pull
git checkout -b feature-x
Push Feature Branch
git add .
git commit -m "Added feature"
git push -u origin feature-x
Merge Feature Branch into Main
git checkout main
git pull
git merge feature-x
git push
Update Feature Branch from Main
git checkout feature-x
git fetch origin
git merge origin/main
Replace Remote Branch with Another Branch
git checkout tfind-staging
git push origin tfind-staging:tfind --force
Create Local Branch from Remote
git fetch origin
git checkout -b customer-map origin/customer-map
Synchronize Local Branch with Remote
git fetch origin
git reset --hard origin/main