Table of Contents

  1. Git CLI snippets
  2. Notes & Additional information
    1. Where are my creds stored when using git config credential.helper store ?
  3. Git snippet
  4. Git Command Reference
    1. Repository Initialization
      1. Initialize a New Repository
      2. Clone a Repository
      3. Clone a Specific Branch
      4. Clone Into a Specific Directory
  5. Repository Information
    1. Show Repository Status
    2. Show Commit History
    3. Compact Commit History
    4. Graph View
    5. Show Current Branch
    6. Show Remotes
  6. Staging Changes
    1. Stage a File
    2. Stage All Changes
    3. Unstage a File
    4. Stage Deleted Files
  7. Commits
    1. Commit Changes
    2. Commit All Tracked Changes
    3. Amend Last Commit
    4. Amend Without Changing Message
  8. Branches
    1. List Local Branches
    2. List Remote Branches
    3. List All Branches
    4. Create Branch
    5. Create and Switch Branch
    6. Switch Branch
    7. Rename Current Branch
    8. Delete Branch
    9. Force Delete Branch
  9. Remote Branches
    1. Push New Branch
    2. Push Existing Branch
    3. Fetch Remote Branches
    4. Create Local Branch from Remote
    5. Track Existing Remote Branch
    6. Delete Remote Branch
  10. Remotes
    1. Add Remote
    2. Change Remote URL
    3. Remove Remote
    4. Rename Remote
    5. Show Remote Details
  11. Pulling and Fetching
    1. Fetch Updates
    2. Pull Changes
    3. Pull Specific Branch
    4. Pull Unrelated Histories
  12. Merging
    1. Merge Branch into Current Branch
    2. Merge Without Fast Forward
    3. Abort Merge
  13. Rebasing
    1. Rebase Current Branch onto Main
    2. Rebase Onto Remote Main
    3. Abort Rebase
    4. Continue Rebase
  14. Comparing Changes
    1. Compare Working Tree to HEAD
    2. Compare Staged Changes
    3. Compare Branches
    4. Show Changed Files Between Branches
    5. Compare Remote Branches
  15. Viewing Files
    1. List Files in Commit
    2. Show File History
    3. Show File Contents from Branch
  16. Stashing
    1. Save Changes
    2. Save with Description
    3. List Stashes
    4. Restore Most Recent Stash
    5. Apply Without Removing
    6. Delete Stash
  17. Undo Changes
    1. Restore File
    2. Restore Entire Working Tree
    3. Reset File to HEAD
    4. Soft Reset
    5. Mixed Reset
    6. Hard Reset
    7. Reset to Remote
  18. Tags
    1. Create Tag
    2. Annotated Tag
    3. Push Tag
    4. Push All Tags
    5. Delete Local Tag
    6. Delete Remote Tag
  19. Working with Bare Repositories
    1. Create Bare Repository
    2. Clone Bare Repository
    3. Show References
  20. Exporting Files
    1. Archive Branch
    2. Export Branch to Directory
  21. Cleaning
    1. Show Files to be Removed
    2. Remove Untracked Files
    3. Remove Untracked Files and Directories
  22. Troubleshooting
    1. Detect Current Upstream
    2. Show Tracking Branches
    3. Find Unmerged Branches
    4. Find Merged Branches
    5. Verify Repository
    6. Show Configuration
    7. Show Origin URL
    8. Show Global Username
    9. Show Global Email
  23. Common Workflows
    1. Create Feature Branch
    2. Push Feature Branch
    3. Merge Feature Branch into Main
    4. Update Feature Branch from Main
    5. Replace Remote Branch with Another Branch
    6. Create Local Branch from Remote
    7. Synchronize Local Branch with Remote

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 -b if the local branch you are checking out to already exists.

  • Remove local branch
      git branch -d <branch-name> 
    
  • Merge two local branches together
    1. Switch to target branch you want to merge changes into
       git checkout <branch-name>
      
    2. Merge branch and resolve conflicts
       git merge <branch-with-changes>
      
  • 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