Git

Configure Settings

git config --global user.name "Bob B"
git config --global user.email bob@example.com
git config --global core.editor nano
git config user.name "Bob B"
git config user.email bob@example.com
git config core.editor nano
git config --local user.name "Bob B
git config --local user.email bob@example.com
git config --local core.editor nano
git config --list
git config --list --local
git config --list --global
git config --list --system

More infos: git book, atlassian

Stash

stash changes: git stash

remove latest stash from storage and apply it: git stash pop

apply latest stash, keep in storage: git stash apply

list all stored stashes: git stash list

list with dates when stashed: git stash list --date=local

show contents / patch of stash: git stash show -p 2 or show -p stash@2

stash also untracked files: git stash save -u

pop a specific stash: git stash pop 1 or pop stash@1

delete all stashes: git stash clear

delete latest (top) stash: git stash drop

delete a specific stash git stash drop 2 or drop stash@2

stash a specific path (more info): git stash push suites/bob/test.py

Add Files to Commit

add multiple files: git add file1.txt file2.py file3.go

add all files in current directory: git add .

add all files in foo/bar directory: git add foo/bar

Status

check the status of local vs remote changes: git status

Commit

commit all added changes in working area: git commit -m "hello"

change previous commit message (before push) git commit --amend

merge with squash (more info): git merge --squash dev

Log

more info

show commit log:

git log
git log --oneline
git log master
git log origin/master
git log V1.0.0
git log -- src/create_tables.sql
git log --pretty=format:"%h%x09%an%x09%ad%x09%s"

more on last line above / author and date: SO

show changes in a commit: git show SHA1

show only modified files in a commit:

git show 10c52da68e77c1ba846b3925ceeb5c40c738d16a --name-only

or a shorter hash works: git show 10c52da68e --name-only

Diff

diff working dir vs branch: git diff

diff in staged/added files vs committed branch: git diff --cached

or git diff --staged

diff of working copy vs specific commit: git diff --staged 10c52da68e

diff two commits, show d19bfca as added: git diff f197ff4 d19bfca

diff specific file only: git diff myfile.py

diff specific files: git diff myfile.py myfile2.py

Apply Diff as Patch

save diffs to file: git diff > mypatch.patch

git diff --cached > mypatch.patch

apply the saved diff to local directory: git apply mypatch.patch

check patch without applying it: git apply --check ../../mypatch.patch

Branches

create local branch, do not switch: git branch mynewbranch

switch branch: git checkout refactoringbranch

create new branch and switch to it: git checkout -b mynewbranch

list all branches (including remote): git branch -a

delete local branch: git branch -d mynewbranch

reset all files to match given remote/branch: git reset --hard origin/master

show which branches are merged to current branch: git branch --merged

show which branches are not yet merged: git branch --no-merged

Remotes

check which remote current branch is tracking SO:

git remote -v
git branch -vv
git remote show origin

change remote for current branch SO: git branch -u origin/mynewbranch

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

rename remote (locally): git remote rename pb paul

remote local reference to a remote: git remote remove paul

Reset files

reset a file: git checkout db/create.py

force filename (vs eg. branch name): git checkout -- origin/refactoring.py

clean workspace, delete changes:

git clean -xfd
git pull --rebase
#https://git-scm.com/docs/git-clean
#-x = delete also .gitignore defined files, dont want that
#-f = force delete, often required by git config
#-d = remove also untracked directories
git clean -fd

Shell Prompts

creating a shell prompt with branch name, colors, etc:

https://jonasjacek.github.io/colors/
Moving to zsh, part 6 – Customizing the zsh Prompt
https://superuser.com/questions/943844/add-timestamp-to-oh-my-zsh-robbyrussell-theme/943916

for bash etc, slightly different but the internet helps

~/.zshrc example:
PROMPT='[%D{%b/%d,} %T] %2~ %%'
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

setopt PROMPT_SUBST
PROMPT='[%B%F{12}%D{%b/%d,} %T, $(parse_git_branch)]%b%F{0} %2~ > '
Advertisement