Skip to content

Instantly share code, notes, and snippets.

@davidgg
Last active December 28, 2022 12:09
Show Gist options
  • Save davidgg/b1a52f33742ef23db99d52c4ca1e8535 to your computer and use it in GitHub Desktop.
Save davidgg/b1a52f33742ef23db99d52c4ca1e8535 to your computer and use it in GitHub Desktop.
Git
[user]
name = DavidGG
email = yourmail@example.com
[core]
# Don't consider trailing space change as a cause for merge conflicts
whitespace = -trailing-space
[color]
# Enable colors in color-supporting terminals
ui = auto
[log]
# Use abbrev SHAs whenever possible/relevant instead of full 40 chars
abbrevCommit = true
# Automatically --follow when given a single path
follow = true
[merge]
tool = kdiff3
[diff]
tool = kdiff3
# Use better, descriptive initials (c, i, w) instead of a/b.
mnemonicPrefix = true
# Show renames/moves as such
renames = true
[mergetool]
keepBackup = false
keepTemporaries = false
[difftool "kdiff3"]
path = /Applications/kdiff3.app/Contents/MacOS/kdiff3
[mergetool "kdiff3"]
path = /Applications/kdiff3.app/Contents/MacOS/kdiff3
[difftool "sourcetree"]
cmd = opendiff \"$LOCAL\" \"$REMOTE\"
path =
[mergetool "sourcetree"]
cmd = /Applications/SourceTree.app/Contents/Resources/opendiff-w.sh \"$LOCAL\" \"$REMOTE\" -ancestor \"$BASE\" -merge \"$MERGED\"
trustExitCode = true
[alias]
#Better Log
lg = log --graph --date=relative --pretty=tformat:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%an %ad)%Creset'
#Remove local branches that have been removed in remote
prune = fetch --prune
#Undo last commit
undo = reset --soft HEAD~1
[branch]
autosetuprebase = always

Git Helpful Commands

Conf user and email for current project

git config --local user.name "Your name"
git config --local user.email "yourmail@example.com"

Rebase the current branch on top of master

git rebase master

Delete remote branch

git push origin :<branch-name>

Undo last commit (only when not pushed)

git reset HEAD~1

Update remote branches

git fetch -p

Change (add staged files, message, etc) last commit

git commit --amend

Merge to master squashing branch commits

git merge --squash <branch-to-squash-from>

Squash last X commits (mix last commits into one)

git rebase -i HEAD~<number-of-commits>

Show pretty history

git log --graph --oneline --all --decorate --topo-order

Tag a commit

git tag v<version>

Rename local and remote branch

git branch -m <old-branch-name> <new-branch-name>    
git push origin :<old-branch-name>   
git push --set-upstream origin <new-branch-name> 

Find merged branches

git branch --merged master

Restore a file state to commit

git checkout <commit>  --  <file-path>

Restore a file state to the parent of a commit

git checkout <commit>^  --  <file-path>

Check the rule that applies to an ignored file

git check-ignore -v <file-path>

Change remote URL

git remote set-url origin <new-origin-url>

Remove last commit from origin (DON'T DO THIS NEVER)

git reset --hard HEAD~1 && git push -f origin master

Resets local master to the correct point, as represented by origin/master.

git reset --hard origin/master

Find common acenstor of two commits/branches

git merge-base feat/A feat/B

Pull and rebase with your changes

git pull --rebase

List remote branches

git branch -r

Remove all branches but main

git branch | grep -v "main" | xargs git branch -D

Checkout locally a PR from other repository/fork. Use ID PR (1) and create a new branch name (test)

GitHub

git fetch origin pull/1/head:test

Bitbucket

You have to find the PR branch (hover over the branch name in the PR).

git fetch https://bitbucket.org/XXXXX/YYYY/branch/ZZZZ master

XXXX -> team YYYY -> project name ZZZZ -> PR branch name

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