Setup git with extra tips and tricks

Home
Author: avolent
Updated on: December 2025
/tools/setup git with extra tips and tricks

Summary

This page will go over how I setup git and some of the useful tip/tricks I wish I knew before.

Assumptions

Contents

  1. Useful Links
  2. Setting up github SSH Keys
  3. Commands
  4. References

Setting Up Github SSH Keys

Linux / WSL / MacOS

Generate a SSH Key1 or use a pre-existing one 2 In the terminal use the following command to generate a key.

ssh-keygen -t ed25519 -C "your_email@example.com"

Start the ssh-agent in the background.

eval "$(ssh-agent -s)"
> Agent pid 59566

Add your SSH private key to the ssh-agent.

ssh-add ~/.ssh/key_name

Now add the key to your github account Copy the contents of your key and add to your github account under - New SSH Key

cat ~/.ssh/key_name.pub

add_ssh_key.png

You can test your connection with the following:

$ ssh -T git@github.com
> Hi USERNAME! You've successfully authenticated, but GitHub does not
> provide shell access.

Commands

Official cheat sheet 3

Setup

git config --global user.name “name” #Set the name that will be used in commits, Normally your username.
git config --global user.email “[valid-email]” #Set a email that will be used in commits.
git config --global color.ui auto #Add colouring to git commands.

Setup & Init

git init #Initialise an existing directory as a Git repository.
git clone [url] #Retreive entire repository from a hosted location via URL.

Stage & Snapshot

git status #Show modified files in working directory, staged for your next commit.
git add [file] #Add a file as it looks not to your next commit (Stage).
git reset [file] #Unstage a file while retaining the changes in working directory.
git diff #Diff of what is changed but not staged.
git diff --staged #Diff of what is staged but not yet commited.
git commit -m "[descriptive message]" #Commit your staged content as a new commit snapshot.

Branch & Merge

git branch #List your branches, a * will appear next to the currently active branch.
git branch [branch-name] #Create a new branch at the current commit.
git checkout [branch-name] #Switch to another branch and check it out into your working directory.
git checkout -b [branch-name] #Create a new branch and switch to it in one command.
git merge [branch] #Merge the specified branch's history into the current one.
git log #Show all commits in the current branch's history.
git branch -d [branch-name] #Delete a local branch (use -D to force delete).
git push origin --delete [branch-name] #Delete a remote branch.

Inspect & Compare

git log #Show the commit history for the currently active branch.
git log --oneline #Show commit history in condensed format.
git log --graph --oneline --all #Visual representation of branch structure.
git log --follow [file] #Show the commits that changed file, even across renames.
git diff [branch-a]...[branch-b] #Show the diff of what is in branch-a that is not in branch-b.
git show [commit] #Show any object in Git in human-readable format.
git log --author="[name]" #Show commits by a specific author.
git log --since="2 weeks ago" #Show commits from a specific time period.
git blame [file] #Show who changed what and when in a file.

Tracking Path Changes

git rm [file] #Delete the file from project and stage the removal for commit.
git mv [existing-path] [new-path] #Change an existing file path and stage the move.
git log --stat -M #Show all commit logs with indication of any paths that moved.
git rm --cached [file] #Remove file from version control but preserve locally.

Ignoring Patterns

# Create a .gitignore file to prevent staging unwanted files
git config --global core.excludesfile [file] #System wide ignore pattern for all local repositories.

Common .gitignore patterns:

# OS files
.DS_Store
Thumbs.db

# IDE files
.vscode/
.idea/
*.swp

# Dependencies
node_modules/
vendor/

# Build outputs
dist/
build/
*.log

# Environment files
.env
.env.local

Share & Update

git remote add [alias] [url] #Add a git URL as an alias.
git fetch [alias] #Fetch down all the branches from that Git remote.
git merge [alias]/[branch] #Merge a remote branch into your current branch.
git push [alias] [branch] #Push local branch commits to remote repository.
git pull #Fetch and merge any commits from the tracking remote branch.
git push -u origin [branch] #Push branch to remote and set upstream tracking.
git remote -v #List all configured remotes.
git remote show origin #Show information about the remote.

Rewrite History

git rebase [branch] #Apply any commits of current branch ahead of specified one.
git reset --hard [commit] #Clear staging area, rewrite working tree from specified commit.
git reset --soft HEAD~ #Undo last commit but keep changes staged.
git reset --mixed HEAD~ #Undo last commit and unstage changes (default).
git commit --amend #Modify the last commit (message or content).
git rebase -i HEAD~[n] #Interactive rebase for last n commits.
git reflog #Show a log of changes to HEAD (useful for recovering lost commits).

Warning: Never rewrite history on shared/public branches!

Temporary Commits

git stash #Save modified and staged changes for later.
git stash list #List stack-order of stashed file changes.
git stash pop #Write working from top of stash stack and remove it.
git stash apply #Apply stashed changes without removing from stash.
git stash drop #Discard the changes from top of stash stack.
git stash save "message" #Stash with a descriptive message.
git stash show #Show the changes in the latest stash.
git stash branch [branch-name] #Create a new branch from a stash.

Advanced Tips & Tricks

Git Aliases

Save time with custom shortcuts:

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual 'log --graph --oneline --all'
git config --global alias.amend 'commit --amend --no-edit'

Useful Workflows

Undo a commit that was pushed:

git revert [commit-hash] #Creates a new commit that undoes changes

Cherry-pick a commit from another branch:

git cherry-pick [commit-hash] #Apply specific commit to current branch

Find which commit introduced a bug (binary search):

git bisect start
git bisect bad #Mark current commit as bad
git bisect good [commit-hash] #Mark a known good commit
# Git will checkout commits for you to test, mark each as good/bad
git bisect reset #When done

Clean up untracked files:

git clean -n #Dry run - show what would be deleted
git clean -fd #Force delete untracked files and directories

Work with patches:

git format-patch [branch] #Create patch files for commits
git apply [patch-file] #Apply a patch file
git am [patch-file] #Apply a patch and create a commit

Performance & Maintenance

git gc #Cleanup unnecessary files and optimize local repository
git prune #Remove unreachable objects
git fsck #Verify the connectivity and validity of objects
git count-objects -vH #Show repository size statistics

Searching & Finding

git grep [pattern] #Search working directory for pattern
git log -S [string] #Search commits that added or removed a string
git log -G [regex] #Search commits with changes matching regex
git show [commit]:[file] #Show file contents at specific commit

Configuration Tips

View all settings:

git config --list #Show all git configuration
git config --list --show-origin #Show settings and their source files

Helpful global configurations:

git config --global pull.rebase true #Use rebase instead of merge for pulls
git config --global core.editor "vim" #Set your preferred editor
git config --global init.defaultBranch main #Set default branch name
git config --global rerere.enabled true #Remember how you resolved merge conflicts
git config --global core.autocrlf input #Handle line endings (input for Mac/Linux)

Debugging & Troubleshooting

Check what changed in a file:

git diff HEAD~2..HEAD [file] #Compare file between two commits ago and now
git log -p [file] #Show patch/diff for each commit affecting file

Find who deleted a file:

git log --all --full-history -- [file-path]

Recover deleted branch:

git reflog #Find the commit where branch was
git checkout -b [branch-name] [commit-hash]

Unstage all files:

git reset

Discard all local changes:

git restore . #Git 2.23+
# or
git checkout -- . #Older Git versions

Working with Submodules

git submodule add [url] [path] #Add a submodule
git submodule update --init --recursive #Initialize and update submodules
git submodule update --remote #Update submodules to latest remote commits

Best Practices

  1. Commit often, perfect later - Make small, logical commits
  2. Write meaningful commit messages - Use imperative mood (“Add feature” not “Added feature”)
  3. Pull before you push - Always sync with remote before pushing
  4. Use branches - Keep main/master stable, develop in branches
  5. Review before committing - Use git diff --staged before commit
  6. Don’t commit sensitive data - Use .gitignore and environment variables
  7. Keep commits atomic - One logical change per commit
  8. Test before pushing - Ensure code works before sharing

Commit Message Convention

<type>: <subject>

<body>

<footer>

Types: feat, fix, docs, style, refactor, test, chore

Example:

feat: add user authentication

Implement JWT-based authentication system with login and logout endpoints.

Closes #123

References

Footnotes

  1. Generating SSH keys for Github

  2. Checking for existing SSH Key

  3. GitHub Cheat Sheet

CSS is from Latex.css | Wiki built by avolent.io | Repository located on Github

Return to top