Skip to content

Instantly share code, notes, and snippets.

@davidbarkhuizen
Last active August 21, 2024 07:29
Show Gist options
  • Select an option

  • Save davidbarkhuizen/bddea21bb93a6daedff1b8c756a90a0e to your computer and use it in GitHub Desktop.

Select an option

Save davidbarkhuizen/bddea21bb93a6daedff1b8c756a90a0e to your computer and use it in GitHub Desktop.
git

git

branch

list all (local + remote) branches

$ git branch -a

create local tracking branches for all remote branches

gitbash $ for remote in `git branch -r `; do git branch --track $remote; done

submodule

register submodule, pull from repo

git submodule add url_to_repo						

delete submodule

git rm --cached path_to_submodule						

config

set username globally

git config --global user.name "david.barkhuizen@gmail.com"						

remote

set username for repo

git config remote.origin.url https://user_name@repo						

edit remote url

git remote set-url origin git://new.url.here						

list remotes

git remote -v						

fetch & pull

fetch all remote branches

$ git fetch --all

fetch remote branch xxx, anx create local tracking branch xxx

git checkout --track origin/xxx

fetch all remote branches, NOT creating local branches tracking respective remotes

git fetch --all						

pull (fetch and merges) remote branches for all existing local branches

git pull --all						

fetch all branches from remote origin

git fetch origin						

fetch branch xxx from remote origin, and create local branch with same name

git fetch origin xxx:xxx						

set local branch to track remote with same name

git push --set-upstream origin xxx						

fetch all remote branches, and create local branch xxx tracking remote branch xxx

git fetch && git checkout xxx						

branch

switch to local branch xxx

git checkout xxx						

create new local branch xxx, keeping uncommitted changes

git checkout -b xxx						

create new local branch xxx, from commit yyy

git checkout -b xxx yyy			

rename local branch old_name to new_name

git branch -m old_name new_name

tag

list local tags

git tag						
git tag -l						

create annotated tag with name xxx and description yyy

git tag -a xxx -m yyy						

create annotated tag with name xxx and description yyy, from commit with SHA hash hash

git tag -a xxx hash -m yyy						

create lightweight tag named xxx

git tag xxx						

push local tags to remote repo

git push origin --tags						

get revision detail for tag xxx

git rev-list -1 xxx						

checkout tag xxx

git checkout tags/xxx						

delete local tag xxx

git tag -d xxx						

delete remote tag xxx

git push --delete origin xxx						

push all local branches to remote, configuring tracking

git push --all -u

merge

merge a specific commit with hash xxx from another branch into the current one

git cherry-pick xxx

undo last merge before pushing (assuming no other changes)

git reset --merge ORIG_HEAD

overwrite the deploy_to_SIT branch with an existing branch, xxx

git checkout deploy_to_SIT						
git reset --hard xxx						
git push origin --force			

rebase

accept all incoming changes during rebase

git rebase -X theirs origin/master

revert & discard

create changes (but do not commit them) that will revert to revision with hash

git revert --no-commit <commit-id> -n HEAD

revert a single file @ xxx/y.js to its state in revision with hash rev_hash_zzz

git checkout rev_hash_zzz xxx/y.js

temporarily revert repo to previous commit xxx, leaving head in detached state

git checkout xxx

revert to commit xxx, permanently discarding all subsequent unpublished commits

git reset --hard xxx

revert to commit xxx, retaining all changes

git reset xxx

diff

diff two branches - branch_x, branch_y

git diff branch_x branch_y

log

print hash of last commit

git rev-parse HEAD

print last commit message

git log -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment