Git Quick Reference

Organized cheat sheet with copy buttons — commands & one-line descriptions.

Author: Akash

Setup & Configuration

git --version                               # check installed git version

git config --global user.name "Akash"       # set your username globally
git config --global user.email "akash@example.com"  # set your email globally
git config --global user.name               # check the saved username
git config --global core.editor "code --wait"  # set VS Code as default editor

# On Windows (view global config):
# c:\> type %userprofile%\.gitconfig
Tip: use a real email and name for commits (company or personal).

Initialize & Status

git init              # initialize a new git repository in current folder
git status            # show the status of working directory & staging area
Run git init once per project folder. Use git status frequently.

Staging & Committing

git add filename.txt                  # add a file to staging area
git commit -m "add new file"          # commit staged files with a message
git log                               # view full commit history
git log --oneline                     # view compact commit history
Use descriptive commit messages. Use git add -p to stage hunks interactively.

Branching & Switching

git branch                            # list all branches
git branch nav-bar                    # create new branch called 'nav-bar'

git checkout nav-bar                  # switch to 'nav-bar' branch
git switch nav-bar                    # switch to 'nav-bar' branch
git switch -c nav-bar                 # create + switch to 'nav-bar'
git checkout -b nav-bar               # create + switch to 'nav-bar'

git merge nav-bar -m "merge branch"   # merge 'nav-bar' into current branch
Prefer git switch & git restore over older checkout for clarity.

Comparing & Resetting

git diff --staged                     # show differences of staged files
git checkout <commit_id>              # move (checkout) to a specific commit
git checkout master                   # move back to master/main branch
When on a detached HEAD after checking out a commit, create a branch to keep changes.

Remotes

git remote -v                         # check remote repositories
git remote add name url               # add new remote
git remote add origin <url>           # add remote 'origin'
git remote rename oldname newname     # rename remote
git remote remove name                # remove a remote
Common remote name is origin. Use descriptive remote names for multiple repos.

Pushing & Pulling

git push <remote> <branch>            # push branch to remote
git push origin main                  # push main branch to origin
git push <remote> local:remote        # push local branch to remote with diff name
git push -u origin main               # push and set upstream (track remote branch)

git pull origin main                  # fetch + merge latest changes from remote

# If you see 'refusing to merge unrelated histories', use:
# git pull origin main --allow-unrelated-histories
Careful with --force pushes. Prefer git pull --rebase to keep linear history when collaborating.

VS Code Tips

Install Git Graph extension in VS Code   # visualize branches & commits

Press CTRL + , in VS Code and uncheck Git from the Exclude section

Press q to quit any ongoing git pager (eg. git log output)
Use Git Graph or Source Control panel for visual operations (merge, rebase, checkout).