Skip to content

Instantly share code, notes, and snippets.

@caiodesign
Created October 2, 2020 20:40
Show Gist options
  • Save caiodesign/e519cdb333a60c3776859e8bfbf0f81c to your computer and use it in GitHub Desktop.
Save caiodesign/e519cdb333a60c3776859e8bfbf0f81c to your computer and use it in GitHub Desktop.
Some git commands that i use

———————————————————————————————————————————————————————————————————————————————— Adding and updating ————————————————————————————————————————————————————————————————————————————————

Update local branch from remote and merge into current branch

$ REMOTE='' && BRANCH='' && git fetch $REMOTE $BRANCH:$BRANCH && git merge $BRANCH $ REMOTE='origin' && BRANCH='' && git fetch $REMOTE $BRANCH:$BRANCH && git merge $BRANCH $ REMOTE='origin' &&
BRANCH='develop' &&
git fetch $REMOTE $BRANCH:$BRANCH &&
git merge $BRANCH $ git fetch : && git merge $ git fetch origin master:master && git merge master $ git fetch origin develop:develop && git merge develop

Rename current branch

$ git branch -m <old_name> <new_name> # local change $ git push --delete <old_name> # erase from remote $ git push <new_name> # re-publish to remote with new name $ git push -u # set new upstream

single line

$ REMOTE='' && OLD_NAME='<old_name>' && NEW_NAME='<new_name>' && git branch -m $OLD_NAME $NEW_NAME && git push $REMOTE --delete $OLD_NAME && git push $REMOTE $NEW_NAME && git push $REMOTE -u $NEW_NAME $ REMOTE='origin' && OLD_NAME='sec/UFA-536' && NEW_NAME='sec/UFA-538' && git branch -m $OLD_NAME $NEW_NAME && git push $REMOTE --delete $OLD_NAME && git push $REMOTE $NEW_NAME && git push $REMOTE -u $NEW_NAME

Create branch with remote

Set upstream

git branch <local_branch> --set-upstream-to <remote_name>/<remote_branch> git branch develop --set-upstream-to origin/develop

Checkout files from another branch

$ git checkout <branch_name> --

Cherry pick without commit

$ git cherry-pick -n

create new orphan branch

$ git checkout --orphan ———————————————————————————————————————————————————————————————————————————————— Status and visualization ————————————————————————————————————————————————————————————————————————————————

Show what a single commit changed

COMMIT=<commit_hash> && git diff $COMMIT~ $COMMIT

Show log in graphical ASCII

$ git log --graph

Show log in one-line

git log --oneline

Show commits of a given user

$ git log --author

Compare added and branched (commited)

$ git diff --cached

Compare between branches

$ git diff ..<another_branch> $ git diff develop..master

Compare a single file between branches

$ git diff .. --

Compare only file names in the difference between branches

$ git diff --name-status ..

Diff excluding files

git diff -- . ':(exclude)' git diff -- . ':(exclude)package-lock.json' git diff -- . ':(exclude)db/irrelevant.php' ':(exclude)db/irrelevant2.php' ———————————————————————————————————————————————————————————————————————————————— Reseting and reverting ————————————————————————————————————————————————————————————————————————————————

undo a git add

git reset HEAD --

Fetch without checkout

$ git fetch origin master:master

Reset current branch based on remote

$ git fetch origin && git reset --hard origin/ $ git fetch origin && git reset --hard origin/master $ git fetch origin && git reset --hard origin/develop ———————————————————————————————————————————————————————————————————————————————— Adding and updating ————————————————————————————————————————————————————————————————————————————————

Update local branch from remote and merge into current branch

$ REMOTE='' && BRANCH='' && git fetch $REMOTE $BRANCH:$BRANCH && git merge $BRANCH $ REMOTE='origin' && BRANCH='' && git fetch $REMOTE $BRANCH:$BRANCH && git merge $BRANCH $ REMOTE='origin' &&
BRANCH='develop' &&
git fetch $REMOTE $BRANCH:$BRANCH &&
git merge $BRANCH $ git fetch : && git merge $ git fetch origin master:master && git merge master $ git fetch origin develop:develop && git merge develop

Rename current branch

$ git branch -m <old_name> <new_name> # local change $ git push --delete <old_name> # erase from remote $ git push <new_name> # re-publish to remote with new name $ git push -u # set new upstream

single line

$ REMOTE='' && OLD_NAME='<old_name>' && NEW_NAME='<new_name>' && git branch -m $OLD_NAME $NEW_NAME && git push $REMOTE --delete $OLD_NAME && git push $REMOTE $NEW_NAME && git push $REMOTE -u $NEW_NAME $ REMOTE='origin' && OLD_NAME='sec/UFA-536' && NEW_NAME='sec/UFA-538' && git branch -m $OLD_NAME $NEW_NAME && git push $REMOTE --delete $OLD_NAME && git push $REMOTE $NEW_NAME && git push $REMOTE -u $NEW_NAME

Create branch with remote

Set upstream

git branch <local_branch> --set-upstream-to <remote_name>/<remote_branch> git branch develop --set-upstream-to origin/develop

Checkout files from another branch

$ git checkout <branch_name> --

Cherry pick without commit

$ git cherry-pick -n

create new orphan branch

$ git checkout --orphan ———————————————————————————————————————————————————————————————————————————————— Status and visualization ————————————————————————————————————————————————————————————————————————————————

Show what a single commit changed

COMMIT=<commit_hash> && git diff $COMMIT~ $COMMIT

Show log in graphical ASCII

$ git log --graph

Show log in one-line

git log --oneline

Show commits of a given user

$ git log --author

Compare added and branched (commited)

$ git diff --cached

Compare between branches

$ git diff ..<another_branch> $ git diff develop..master

Compare a single file between branches

$ git diff .. --

List only file names in the difference between branches

$ git diff --name-status ..

excluding files

git diff -- . ':(exclude)' git diff -- . ':(exclude)package-lock.json' git diff -- . ':(exclude)db/irrelevant.php' ':(exclude)db/irrelevant2.php' ———————————————————————————————————————————————————————————————————————————————— Reseting and reverting ————————————————————————————————————————————————————————————————————————————————

undo a git add

git reset HEAD --

Fetch without checkout

$ git fetch origin master:master

Reset current branch based on remote

$ git fetch origin && git reset --hard origin/ $ git fetch origin && git reset --hard origin/master $ git fetch origin && git reset --hard origin/develop

Reset/discard unstaged (not added) changes, but keep staged ones (added) for unpushed commits

$ git reset --hard / $ git reset --hard origin/develop $ git checkout -- .

Remove local untracked files from the current Git branch

$ git clean -n # preview what will be removed $ git clean -fd # actually remove

Stash single file

git stash push -m ""

Stash by hunk (each modification)

$ git stash push -p -m "_____"

Show given stash content

$ git stash show -p stash@{1} ———————————————————————————————————————————————————————————————————————————————— Erasing ————————————————————————————————————————————————————————————————————————————————

Delete branch

Local

$ git branch -d branch_name $ git branch -D branch_name # force

Remote

$ git push <remote_name> --delete <branch_name> $ git push <remote_name> :<branch_name> # alternate method

delete tag local and remote (and revez)

$ git tag -d && git push origin :refs/tags/ $ git tag -d v0.9.5 && git push origin :refs/tags/v0.9.5 $ git push --delete origin && git tag -d

delete all tags, local and remote

$ git tag -l | xargs git tag -d && git fetch && git tag -l | xargs -n 1 git push --delete origin && git tag -l | xargs git tag -d ———————————————————————————————————————————————————————————————————————————————— Etc ————————————————————————————————————————————————————————————————————————————————

Optimize git folder

$ git repack -a -d --depth=250 --window=250

alternate method not so recommended ("git --aggressive is mostly dumb" -- Linus Torvalds)

$ git gc --aggressive --prune

Remotes: list, add, edit, erase, rename

git remote -v git remote add git remote set-url

git remote rm git remote rename

Cool Git tips

https://dev.to/zaiste/15-git-commands-you-may-not-know-4a8j https://blog.novoda.com/git-code-detectives/

Reset/discard unstaged (not added) changes, but keep staged ones (added) for unpushed commits

$ git reset --hard / $ git reset --hard origin/develop $ git checkout -- .

Remove local untracked files from the current Git branch

$ git clean -n # preview what will be removed $ git clean -fd # actually remove

Stash single file

git stash push -m ""

Stash by hunk (each modification)

$ git stash push -p -m "_____"

Show given stash content

$ git stash show -p stash@{1} ———————————————————————————————————————————————————————————————————————————————— Erasing ————————————————————————————————————————————————————————————————————————————————

Delete branch

Local

$ git branch -d branch_name $ git branch -D branch_name # force

Remote

$ git push <remote_name> --delete <branch_name> $ git push <remote_name> :<branch_name> # alternate method

delete tag local and remote (and revez)

$ git tag -d && git push origin :refs/tags/ $ git tag -d v0.9.5 && git push origin :refs/tags/v0.9.5 $ git push --delete origin && git tag -d

delete all tags, local and remote

$ git tag -l | xargs git tag -d && git fetch && git tag -l | xargs -n 1 git push --delete origin && git tag -l | xargs git tag -d ———————————————————————————————————————————————————————————————————————————————— Etc ————————————————————————————————————————————————————————————————————————————————

Optimize git folder

$ git repack -a -d --depth=250 --window=250

alternate method not so recommended ("git --aggressive is mostly dumb" -- Linus Torvalds)

$ git gc --aggressive --prune

Remotes: list, add, edit, erase, rename

git remote -v git remote add git remote set-url

git remote rm git remote rename

Cool Git tips

https://dev.to/zaiste/15-git-commands-you-may-not-know-4a8j https://blog.novoda.com/git-code-detectives/

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