Github Cheat Sheet

General Terminology

origin is the default name given to the remote repository when you clone a repository. You can change it to any name you want.

Basic Commands

Command Action
git init Initialize a new repository
git clone Clone a repository
git status Show the working tree status
git log Show commit logs
git log --oneline Show commit logs one line

Add, Commit and Push

Command Action
git add Add file(s) to staging area
git add . Add all files to staging
git commit -am 'message' Stage, add comment and commit
git commit -m 'message' Commit files
git push && git push --tags Push with tags
git push origin [branch] Push branch to server
git push origin master Push to server

Configurations

Command Action
git config --global Set global config
git config --local Set local config
git config --global user.name set global user name
git config --global user.email set global config email
git config --global github.user set global github username
git config --list List all configs

Remote Repositories

Command Action
git remote add origin Add a remote repository
git fetch origin Fetch changes from remote
git pull origin Pull changes from remote
git push origin Push changes to remote
git remote rename Rename remote repository
git remote remove Remove remote repository

Branching and Merging

Command Action
git branch List all branches
git branch -a List all branches (local & remote)
git branch -d Delete a local branch
git branch -m Rename branch
git branch Create a new branch
git checkout -b Create branch and switch
git checkout Switch to a branch

Merge, Rebase and Squash

Command Action
git rebase -i head~N Squash commits by number
git merge --abort Cancel merge
git merge Merge a branch
git merge --squash Merge changes and into a single commit

Housekeeping

Command Action
git clean -df Remove untracked files
git gc Garbage collection. Optimizes the local repository
git prune Prune unreachable objects
git push origin --delete branch-name Delete remote branch (local & repo)
git reset --hard && git clean -df Reset to last commit and remove untracked files
git rm -rf --cached . refresh git cache

Resetting and Reverting

Command Action
git reset --soft Soft reset
git reset --mixed Mixed reset
git reset --hard Hard reset
git revert Revert a commit
git reset --soft HEAD~1         # Restore last commit but keep changes
git reset --hard HEAD~1         # Restore last commit and delete changes

Release and Version Tags

Command Action
git tag Create a tag
git tag List tags
git tag -d Delete a tag

Stashing Changes

Command Action
git stash Stash changes
git stash clear Clear all stashes
git stash drop Drop a stash
git stash list Show stash list
git stash pop Get changes from stash
git stash show Show changes in stash

Creating Alias

git config --global alias.nk branch clone https://github.com/naykel76/laravel-starter"

how can i change this to accept two argument where the first argument is the repositor name and the second is option to name the folder to clone to? git config --global alias.naykel 'clone https://github.com/naykel76/argument1 argument2'

git config --global alias.naykel '!'"git clone https://github.com/naykel76/$1 argument2"

Create Alias to Clone Repo

git config --global alias.naykel '!'"f() { git clone https://github.com/naykel76/\$1 \$2; }; f"

naykel = !git clone https://github.com/naykel/$1 $2

[alias] files = "!f() { git diff --name-status "$1^" "$1"; }; f"

An alias without ! is treated as a Git command; e.g. commit-all = commit -a.

With the !, it's run as its own command in the shell, letting you use stronger magic like this.

FAQ's

How do I update the local repo name when it is changed on the remote repo?

If the repository's URL has also changed (which is common when the name changes), you need to update the URL for your remote. You can do this with:

git remote set-url origin https://github.com/yourusername/new-repo.git

Verify the changes with:

git remote -v

How do I change the last commit message?

git commit --amend -m "New commit message."

How do i rename a local and remote git branch?

# Update local repo name
git branch -m new-branch-name
# Delete old branch
git push origin --delete old-branch-name
# Push renamed branch
git push origin HEAD

# Update remote repo name
git branch --unset-upstream
# Re-set upstream
git push --set-upstream origin main