Skip to content

Instantly share code, notes, and snippets.

@weblogix
Last active August 19, 2020 06:20
Show Gist options
  • Save weblogix/e94eee3f8e05566d9e427864c6a22496 to your computer and use it in GitHub Desktop.
Save weblogix/e94eee3f8e05566d9e427864c6a22496 to your computer and use it in GitHub Desktop.
[git cheatsheet] #git
[user]
name = Randy
email = [email protected]
[alias]
co = checkout
ci = commit
st = status
br = branch
hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
type = cat-file -t
dump = cat-file -p

Remote

List remote branches

git branch -r

List all branches

git branch -a

Checkout Remote branch

git fetch
git checkout <branch>

Checkout remote branch + local tracking

git fetch
git checkout -b <branch> <origin>/<branch>

Checkout file from another branch

git checkout <current branch>
git checkout <branch to retrieve from> -- filename.txt
git commit -m "Update from <branch to retrieve from>"

Add all modified/deleted

git add -u

Delete commit (local and remote)

Source: https://ncona.com/2011/07/how-to-delete-a-commit-in-git-local-and-remote/

Delete remote branch

git push --delete <remote> <branch>

Rename locale and remote branch

git branch -m old_branch new_branch         # Rename branch locally
git push origin :old_branch                 # Delete the old branch    
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

Reset branch - lose all changes

git reset --hard

Undo all changes

git checkout .

Delete untracked files - show files to be deleted

git clean -n

Delete untracked files

This removes untracked files, including directories (-d) and files ignored by git (-x). git clean -d -x -f

Push all branches to remote

git push --all origin

Pull all remote branches

git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all

Compare which files have changed between 2 branches

git diff --name-status master

Delete everything/reset

git clean -xdf && git reset --hard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment