└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # git-useful 2 | > a collection of incredibly useful git commands 3 | 4 | ## Table of Contents 5 | - [Add](#add) 6 | - [add files interactively](#add-files-interactively) 7 | - [add all changes ignoring whitespace changes](#add-all-changes-ignoring-whitespace-changes) 8 | - [Log](#log) 9 | - [shows commit frequency for each user in the repo](#shows-commit-frequency-for-each-user-in-the-repo) 10 | - [pretty log (one line with graphic and colors)](#pretty-log-one-line-with-graphic-and-colors) 11 | - [logs commits that added or removed a certain keyword](#logs-commits-that-added-or-removed-a-certain-keyword) 12 | - [lists already-merged branches](#lists-already-merged-branches) 13 | - [list authors by commits](#list-authors-by-commits) 14 | - [list tags by date](#list-tags-by-date) 15 | - [Diff](#diff) 16 | - [diff word-by-word](#diff-word-by-word) 17 | - [short infos about changes in a commit](#short-infos-about-changes-in-a-commit) 18 | - [show changed files in a commit](#show-changed-files-in-a-commit) 19 | - [list every changed file between two commits](#list-every-changed-file-between-two-commits) 20 | - [show modifications in a file in a commit](#show-modifications-in-a-file-in-a-commit) 21 | - [show files with conflicts](#show-files-with-conflicts) 22 | - [Branch](#branch) 23 | - [deletes already-merged branches](#deletes-already-merged-branches) 24 | - [add new branch and switches to this branch](#add-new-branch-and-switches-to-this-branch) 25 | - [Misc](#misc) 26 | - [sync fork](#sync-fork) 27 | - [assume file as unchanged](#assume-file-as-unchanged) 28 | - [undo assume file as unchanged](#undo-assume-file-as-unchanged) 29 | - [list files assumed as unchanged](#list-files-assumed-as-unchanged) 30 | - [Clean](#clean) 31 | - [remove untracked files](#remove-untracked-files) 32 | - [Auditing](#auditing) 33 | 34 | - [Others](#others) 35 | 36 | ## Add 37 | 38 | #### add files interactively 39 | ```bash 40 | git add --patch 41 | 42 | # working example 43 | git add . --patch 44 | ``` 45 | Very useful when you need to commit different lines of a file. See more at the docs for [Interactive Mode](http://git-scm.com/docs/git-add#_interactive_mode). 46 | 47 | #### add all changes ignoring whitespace changes 48 | ```bash 49 | git diff -w --no-color | git apply --cached --ignore-whitespace 50 | ``` 51 | That time when your editor removed all trailing spaces 52 | 53 | source: http://stackoverflow.com/questions/3515597/git-add-only-non-whitespace-changes 54 | 55 | ## Log 56 | 57 | #### shows commit frequency for each user in the repo 58 | ```bash 59 | git log --format='%an' . | \ 60 | sort | uniq -c | sort -rn | head 61 | ``` 62 | source: http://mislav.uniqpath.com/2014/02/hidden-documentation/ 63 | 64 | #### pretty log (one line with graphic and colors) 65 | ```bash 66 | git log \ 67 | --graph \ 68 | --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' 69 | ``` 70 | 71 | #### logs commits that added or removed a certain keyword 72 | ```bash 73 | git log -S '' 74 | ``` 75 | source: http://mislav.uniqpath.com/2014/02/hidden-documentation/ 76 | 77 | ### lists already-merged branches 78 | ```bash 79 | git branch --merged [] 80 | 81 | # working examples 82 | git branch --merged master 83 | 84 | # in the current HEAD 85 | git branch --merged 86 | ``` 87 | 88 | source: http://stevenharman.net/git-clean-delete-already-merged-branches 89 | 90 | 91 | #### list authors by commits 92 | ```bash 93 | git shortlog -sn 94 | 95 | # working example 96 | git shortlog -sn master 97 | ``` 98 | 99 | source: http://codeinthehole.com/writing/command-line-tips-for-effective-release-announcements/ 100 | 101 | #### list commits by authors between revisions 102 | ```bash 103 | git shortlog .. --no-merges 104 | 105 | # working example 106 | git shortlog master..HEAD --no-merges 107 | ``` 108 | 109 | source: http://codeinthehole.com/writing/command-line-tips-for-effective-release-announcements/ 110 | 111 | #### list tags by date 112 | ```bash 113 | git for-each-ref --sort=taggerdate --format '%(refname) %(taggerdate)' refs/tags 114 | ``` 115 | 116 | source: http://stackoverflow.com/questions/6269927/how-can-i-list-all-tags-in-my-git-repository-by-the-date-they-were-created 117 | 118 | ## Diff 119 | 120 | #### diff word-by-word 121 | ```bash 122 | git diff --word-diff 123 | ``` 124 | 125 | source: http://idnotfound.wordpress.com/2009/05/09/word-by-word-diffs-in-git/ 126 | 127 | #### short infos about changes in a commit 128 | ```bash 129 | git diff-tree --no-commit-id --shortstat -r 130 | 131 | # working example 132 | git diff-tree --no-commit-id --shortstat -r HEAD 133 | ``` 134 | 135 | #### show changed files in a commit 136 | ```bash 137 | git diff-tree --no-commit-id --name-only -r 138 | 139 | # working example 140 | git diff-tree --no-commit-id --name-only -r HEAD 141 | ``` 142 | 143 | If you want a more detailed version, run with the `--stat`, or `--numstat` or `--dirstat` flags instead of `--name-only`. 144 | 145 | source: http://stackoverflow.com/questions/424071/list-all-the-files-for-a-commit-in-git 146 | 147 | #### list every changed file between two commits 148 | ```bash 149 | git diff --name-only 150 | 151 | # working example 152 | git diff --name-only HEAD~3 HEAD 153 | ``` 154 | 155 | source: http://stackoverflow.com/questions/1552340/git-show-all-changed-files-between-two-commits 156 | 157 | #### show modifications in a file in a commit 158 | ```bash 159 | git diff ~1.. [] 160 | 161 | # working example 162 | git diff HEAD~1..HEAD 163 | ``` 164 | 165 | You can optionally add a file name to show only changes in a specific file. 166 | 167 | #### show files with conflicts 168 | ```bash 169 | git diff --name-only --diff-filter=U 170 | ``` 171 | 172 | source: http://stackoverflow.com/questions/3065650/whats-the-simplest-way-to-git-a-list-of-conflicted-files 173 | 174 | ## Branch 175 | 176 | #### deletes already-merged branches 177 | 178 | ```bash 179 | git branch --merged | grep -v "\*" | xargs -n 1 git branch -d 180 | ``` 181 | 182 | source: http://stevenharman.net/git-clean-delete-already-merged-branches 183 | 184 | #### add new branch and switches to this branch 185 | 186 | ```bash 187 | git checkout -b 188 | 189 | # working example 190 | git checkout -b new_branch 191 | ``` 192 | 193 | source: http://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging 194 | 195 | ## Misc 196 | 197 | #### sync fork 198 | ```bash 199 | # Requires an "upstream" remote, pointing to original repo 200 | # e.g. `git remote add upstream git@github.com:user/repo.git` 201 | git fetch upstream; git checkout master; git rebase upstream/master 202 | ``` 203 | source: https://help.github.com/articles/syncing-a-fork 204 | 205 | #### assume file as unchanged 206 | ```bash 207 | git update-index --assume-unchanged 208 | 209 | # working example 210 | git update-index --assume-unchanged . 211 | ``` 212 | 213 | #### undo assume file as unchanged 214 | ```bash 215 | git update-index --no-assume-unchanged 216 | 217 | # working example 218 | git update-index --no-assume-unchanged . 219 | ``` 220 | 221 | source: http://stackoverflow.com/questions/17195861/undo-a-git-update-index-assume-unchanged-file 222 | 223 | #### list files assumed as unchanged 224 | ```bash 225 | git ls-files -v|grep '^h' 226 | ``` 227 | 228 | source: http://stackoverflow.com/questions/17195861/undo-a-git-update-index-assume-unchanged-file 229 | 230 | ## Clean 231 | 232 | #### remove untracked files 233 | ```bash 234 | # list files that would be removed 235 | git clean -f -n 236 | 237 | # remove untracked files 238 | git clean -f 239 | ``` 240 | 241 | source: http://stackoverflow.com/questions/61212/how-do-i-remove-local-untracked-files-from-my-current-git-branch 242 | 243 | ## Auditing 244 | 245 | 246 | ## Others 247 | More than one line command useful things 248 | 249 | - [Git cheat sheet](https://github.com/tiimgreen/github-cheat-sheet) 250 | - [Git ahead and behind info](https://gist.github.com/hugobessaa/8788821) 251 | - [git-overwritten](https://github.com/mislav/dotfiles/blob/7ac8cbfcd56cfa6c39b5719ea183e87878ea6ed5/bin/git-overwritten) 252 | --------------------------------------------------------------------------------