└── README.md /README.md: -------------------------------------------------------------------------------- 1 | #### Go one step back in history 2 | ```shell 3 | git checkout @~1 4 | ``` 5 | 6 | #### Show commit changes 7 | ```shell 8 | git show 9 | ``` 10 | 11 | #### Push master branch to origin 12 | ```shell 13 | git push origin master 14 | ``` 15 | 16 | #### List all branches 17 | ```shell 18 | git branch -a 19 | ``` 20 | 21 | #### List remote branches 22 | ```shell 23 | git branch -r 24 | ``` 25 | 26 | #### Sync list of remote branches 27 | ```shell 28 | git remote update 29 | ``` 30 | 31 | #### Checkout remote branch into local repository 32 | ```shell 33 | git checkout -t -b 34 | ``` 35 | 36 | #### Create a branch 37 | ```shell 38 | git checkout -b 39 | ``` 40 | 41 | #### Push local branch to remote 42 | ```shell 43 | git push origin 44 | ``` 45 | 46 | #### Merge two branches 47 | ```shell 48 | git checkout 49 | git merge 50 | ``` 51 | 52 | #### Merge two branches with squash 53 | ```shell 54 | git checkout 55 | git merge --squash 56 | ``` 57 | 58 | #### See what branches are merged into master 59 | ```shell 60 | git branch -r --merged master 61 | ``` 62 | 63 | #### Ignore files globally 64 | https://help.github.com/articles/ignoring-files 65 | 66 | #### pull changes from the server + rebase (equivalent of git stash save && git pull && git stash pop && git push) + push 67 | ```shell 68 | git pull --rebase && git push 69 | ``` 70 | 71 | #### Set up git inet server 72 | ```shell 73 | git daemon --base-path /home/git --verbose 74 | ``` 75 | 76 | #### Add remote origin 77 | ```shell 78 | git remote add origin 79 | ``` 80 | 81 | #### Fix 'branch not tracking anything' 82 | ```shell 83 | git config --add branch.master.remote origin 84 | git config --add branch.master.merge refs/heads/master 85 | ``` 86 | 87 | #### Extract patch from a given file 88 | ```shell 89 | git diff --patch-with-raw rev1 rev2 patched_file > diff_file 90 | ``` 91 | 92 | #### Diff with paging 93 | ```shell 94 | GIT_PAGER='less -r' git dc 95 | ``` 96 | 97 | #### Apply a patch 98 | ```shell 99 | git apply diff_file 100 | ``` 101 | 102 | #### Publish branch 103 | ```shell 104 | git push origin 105 | ``` 106 | 107 | #### Delete remote branch 108 | ```shell 109 | git push origin : 110 | ``` 111 | 112 | #### Cherry-pick a commit 113 | ```shell 114 | git cherry-pick -n 115 | ``` 116 | 117 | #### Revert commit 118 | ```shell 119 | git revert -n 120 | ``` 121 | 122 | #### Reset HEAD to n commits back 123 | ```shell 124 | git reset --hard HEAD~ 125 | ``` 126 | 127 | #### Squash N last commits 128 | ```shell 129 | git rebase --interactive --autosquash HEAD~N 130 | ``` 131 | 132 | #### search git log commits 133 | ```shell 134 | git log -S “free text” 135 | ``` 136 | 137 | #### remove file from history (can use 'git rm -rf…' to remove files recursively) 138 | ```shell 139 | git filter-branch --index-filter 'git rm --cached --ignore-unmatch ' --prune-empty --tag-name-filter cat -- --all 140 | git push origin master --force 141 | rm -rf .git/refs/original/ 142 | git reflog expire --expire=now --all 143 | git gc --prune=now 144 | git gc --aggressive --prune=now 145 | ``` 146 | 147 | #### Prune history 148 | ```shell 149 | git gc 150 | git gc --aggressive 151 | git prune 152 | ``` 153 | 154 | #### Checkout GitHub PR 155 | ```shell 156 | git fetch origin pull/1234/head:local-branch-name 157 | ``` 158 | 159 | #### Convert long sha to short one 160 | ```shell 161 | git rev-parse --short 162 | ``` 163 | 164 | #### My aliases 165 | ```shell 166 | git config --global alias.aa "add --all" 167 | git config --global alias.ai "add --interactive" 168 | git config --global alias.b "branch" 169 | git config --global alias.ba "branch -a" 170 | git config --global alias.c "commit" 171 | git config --global alias.ca "commit --amend" 172 | git config --global alias.cf '!sh -c "git commit --fixup $@"' 173 | git config --global alias.co "checkout" 174 | git config --global alias.col '!sh -c "git checkout -b $@"' 175 | git config --global alias.cor '!sh -c "git checkout --track -b $@ origin/$@"' 176 | git config --global alias.cp "cherry-pick" 177 | git config --global alias.cpa "cherry-pick --abort" 178 | git config --global alias.cpc "cherry-pick --continue" 179 | git config --global alias.cs '!sh -c "git commit --squash $@"' 180 | git config --global alias.d "diff" 181 | git config --global alias.dc "diff --cached" 182 | git config --global alias.ds "diff --stat" 183 | git config --global alias.dsc "diff --stat --cached" 184 | git config --global alias.fpr '!sh -c "git fetch origin pull/$@/head:$@-pr"' 185 | git config --global alias.l "log" 186 | git config --global alias.lf "log --follow" 187 | git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %Cblue<%an>%Creset' --abbrev-commit --date=relative --all" 188 | git config --global alias.m "merge" 189 | git config --global alias.mb "merge-base master HEAD" 190 | git config --global alias.ms "merge --squash" 191 | git config --global alias.pl "pull" 192 | git config --global alias.ps "push" 193 | git config --global alias.psc '!sh -c "git push --set-upstream origin \$(git rev-parse --abbrev-ref HEAD)"' 194 | git config --global alias.psd '!sh -c "git push origin :\$(git rev-parse --abbrev-ref HEAD)"' 195 | git config --global alias.psf "push --force-with-lease" 196 | git config --global alias.r "reset HEAD" 197 | git config --global alias.rb "rebase" 198 | git config --global alias.rba "rebase --abort" 199 | git config --global alias.rbc "rebase --continue" 200 | git config --global alias.rbi "rebase --interactive --autosquash" 201 | git config --global alias.rbm "rebase --interactive --autosquash origin/master" 202 | git config --global alias.rh "reset --hard" 203 | git config --global alias.rs "reset --soft" 204 | git config --global alias.s "status" 205 | git config --global alias.sh "show" 206 | git config --global alias.shs "show --stat" 207 | git config --global alias.st "stash" 208 | ``` 209 | 210 | #### Global git ignore 211 | ```shell 212 | git config --global core.excludesfile ~/.gitignore 213 | ``` 214 | 215 | #### Global username & email 216 | ```shell 217 | git config --global user.name "Jakub Pawlowicz" 218 | git config --global user.email '' 219 | ``` 220 | 221 | #### Local username & email 222 | ```shell 223 | git config user.name "Jakub Pawlowicz" 224 | git config user.email '' 225 | ``` 226 | 227 | #### 228 | ```shell 229 | git config --global color.diff auto 230 | git config --global color.status auto 231 | git config --global color.branch auto 232 | ``` 233 | 234 | #### Branch name and merge status in bash prompt (should go to local or global bash profile) 235 | ```shell 236 | function parse_git_dirty { 237 | [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working directory clean" ]] && echo "*" 238 | } 239 | function parse_git_branch { 240 | git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/" 241 | } 242 | export PS1='\u@\h \[\033[0;36m\]\w \[\033[0;32m\]$(parse_git_branch)\[\033[0m\]$ ' 243 | ``` 244 | --------------------------------------------------------------------------------