Git Cheat Sheet

A Programming › Git cheat sheet in English by shribee

git commands reference sheet

9,384 views 2,360 downloads 17 Feb 2020 History
3.7 avg · 0 ratings

Initialize

git init
Initialize repository
git remote add <or­igi­n/a­lte­rnate name> <repo url>
Link local repo with <repo URL>
git push -u origin master
Push committed changes (-u and track branch with remote)

Commit

git add .
Adds all modified and new (untra­cked) files in the current directory and all subdir­ect­ories to the staging area
git commit -m <commit messag­e>
Locally commit
git checkout .
Revert changes to local copy
git reset
Revert changes made to the index (i.e., that you have added). Warning this will reset all of your unpushed commits to master!
git revert <commit 1> <commit 2>
Revert a change that you have committed
git clean -f
Remove untracked files (e.g., new files, generated files)
git clean -fd
Remove untracked direct­ories
git-stash
Stash the changes away in a working directory
git diff <branch 1> <branch 2>
Compare two branches
git diff <branch 1> <branch 2> --name­-only
Compare two branches, file names only
git diff <br­anc­h>
Compare <br­anc­h> with current branch
git rm <fi­le> --cached
Remove file from remote, while retaining local copy

Branching, Merging

git checkout -b <br­anc­h> master
Create <br­anc­h> from master
git push -u origin <br­anc­h>
Push <br­anc­h>
git merge <br­anc­h>
Merge <br­anc­h> into current branch
git branch -d <br­anc­h>
Delete local branch if it is merged, if not -D to force delete
git push <re­mot­e_n­ame> --delete <br­anc­h>
Delete remote branch
git branch <br­anc­h>
Create branch <br­anc­h>
git branch [-a]
List branches, -a including remote, * indicates current branch
git rebase <br­anc­h>
Rebase from <br­anc­h> into current branch, i.e. take <br­anc­h> and then apply current branch­'s commits onto it to create linear history
git branch -m <ol­d> <new>
git push origin :<o­ld> <new>
git push origin -u <ne­w>
1. Rename your local branch
2. Delete the <ol­d> remote branch and push the <ne­w> local branch
3. Reset the upstream branch for the <ne­w> local branch
git diff --name­-status <b1­>..<­b2>
Show diffs (filenames only) between two branches
git diff --name­-status master
Show diffs (filenames only) between master and current branch

Admin, Troubl­e-s­hooting

git log [-p] [-n]
Shows commit history, last n, -p differ­ences
git remote set-url origin <new url>
Change remote url
git remote -v
List remote inform­ation
git config --global creden­tia­l.h­elper cache
git config --global creden­tia­l.h­elper '­cache --time­out­=36­00'
Set git to use the credential memory cache
set the cache to timeout after 1 hour (setting is in seconds)
git config [--global] user.name "­FIR­ST_NAME LAST_N­AME­"
Set (globa­l/repo) username for commits
git config [--global] user.email "­MY_­NAM­E@e­xam­ple.co­m"
Set (globa­l/repo) email for commits

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.