Guest User

Untitled

a guest
Jul 8th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.94 KB | None | 0 0
  1. (Several of these git examples have been extracted from the book 'Pragmatic guide to GIT' of Travis Swicegood )
  2.  
  3. # Git tips
  4.  
  5. ## Global git user
  6. git config --global user.name "Fernando Guillen"
  7. git config --global user.email "fguillen.mail+spam@gmail.com"
  8.  
  9. ## Repository git user
  10. cd /develop/myrepo
  11. git config user.name "Fernando Guillen"
  12. git config user.email "fguillen.mail+spam@gmail.com"
  13.  
  14. ## Git editor
  15. git config --global core.editor /usr/bin/vim
  16.  
  17. ## Init a local repo and first commit
  18. cd /develop/myrepo
  19. git init
  20. git touch README.md
  21. git add README.md
  22. git commit -m "first commit"
  23.  
  24. ## Clone a remote repo
  25. git clone git@gist.github.com:747678.git myrepo
  26.  
  27. ## Clone but taken only the las 50 commits
  28. git clone --depth 50 git@gist.github.com:747678.git my_repo
  29.  
  30. ## Status
  31. git status
  32. git status --short # status decorated
  33.  
  34. ## Stagging
  35. git add path/to/file
  36. git add path/to/folder
  37. git add .
  38. git add -A|--all
  39. git add -u|--update
  40. git add -p|--patch [path/to/file] # choose the changes
  41. git add -e [path/to/file] # edit the diff
  42.  
  43. ## Commiting
  44. git commit -m "Message"
  45. git commit -a -m "Message" # adding all changed files
  46. git commit # open editor for Message
  47. git commit --amend
  48. git commit --amend -C HEAD
  49.  
  50. ## Stashing
  51. git stash
  52. git stash apply
  53. git stash pop # apply and remove from the stack
  54. git stash list
  55. git stash save --patch
  56. git stash drop <stash_name> # remove
  57. git stash clear # remove all
  58. git stash <branch_name> [<stash_name>] # create a branch from an stash
  59.  
  60. ## Global ignore
  61. echo ".DS_STORE" >> ~/.gitignore
  62. git config --global core.excludesfile ~/.gitignore
  63.  
  64. ## Unstage a file
  65. git reset HEAD -- path/to/file
  66.  
  67. ## Undo all uncommited changes
  68. git checkout -- path/to/file # posible data lost!!
  69.  
  70. ## Git file commands
  71. git mv path/to/file path/to/file2
  72. git rm -- path/to/file
  73. git rm -r -- path/to/folder
  74. git rm -f -- path/to/file
  75. git clean -n # secure remove not stashed files
  76.  
  77. ## Branching
  78. git branch <branch_name>
  79. git branch <branch_name> 99a0de8 # from an starting point
  80. git branch <branch_name> <other_branch>
  81. git branch --track <branch_name>
  82. git branch --track <branch_name> <repository>/<branch_name>
  83. git branch --no-track <branch_name>
  84.  
  85. git checkout <branch_name>
  86. git checkout -b <branch_name> # create branch and checkout into it
  87. git checkout --track -b <branch_name> <repository>/<branch_name>
  88.  
  89. git branch # only locals
  90. git branch -r # only remotes
  91. git branch -a # all
  92. git branch --merged
  93. git branch --no-merged
  94. git branch --contains 99a0de8 # branchs that contain this commit
  95.  
  96. git branch -d <other_branch>
  97. git branch -D <other_branch>
  98.  
  99. git push <remote_repo> :<remote_branch> # deleting a remote branch
  100.  
  101. git show-branch # show the branch history
  102.  
  103. ## Cherry picking
  104. git cherry-pick 99a0de8
  105. git cherry-pick --edit 99a0de8 # edit the message
  106. git cherry-pick --no-commit 99a0de8
  107.  
  108.  
  109. ## Tagging
  110. git tag
  111. git tag v1.0
  112. git tag beta1 99a0de8
  113.  
  114. git push <remote_repo> v1.0
  115. git push --tags <remote_repo>
  116.  
  117. git fetch --tags <remote_repo>
  118.  
  119. git tag -d <tag_name> # remove local tag
  120. git push origin :refs/tags/<tag_name> # remove remote tag
  121.  
  122. ## Mergin
  123. git merge <other_branch>
  124. git merge --no-commit <other_branch> # no create a commit
  125. git merge --no-ff <other_branch> # force create a merge commit
  126. git merge --log <other_branch> # one-line log for each merged commit
  127. git merge -m "Message" <other_branch> # message for this merge commit log
  128.  
  129. ## Rebasing
  130. git rebase <other_branch>
  131. git rebase 99a0de8
  132. git reset --hard ORIG_HEAD # undo last rebase after it completes
  133.  
  134. git rebase --continue
  135. git rebase --abort
  136.  
  137. git rebase -i 99a0de8
  138. git rebase -i <other_branch>
  139. git rebase -i HEAD~5
  140. git rebase -i HEAD^^^^^
  141.  
  142. ## logging
  143. git log
  144. git log --oneline
  145.  
  146. git log -5
  147. git log HEAD^^^^^..HEAD
  148. git log HEAD~5..HEAD
  149. git log -1 -p HEAD # see changes in last commit
  150.  
  151. git log -- path/to/folder # commits affecting path/to/folder
  152. git log -- path/to/file
  153. git log --patch -- path/to/file # commits affecting path/to/file in diff format
  154.  
  155. git log --since="1 week"
  156. git log --after="7 days"
  157. git log --before="1 week"
  158. git log --until="7 days"
  159.  
  160. git log --grep="some [Rr]eg[Ee]x"
  161. git log --grep="some [Rr]eg[Ee]x" --regexp-ignore-case
  162.  
  163. git log --author="<user_name>"
  164. git log -S"my string" # 'my string' commits matching
  165. git log -S"my string" --patch # 'my string' commits matching in diff format
  166.  
  167. git log ..67aab84 # commits in 67aab84 that are not in HEAD
  168.  
  169. git reflog # history log of the branches changes
  170.  
  171. ### Find the commits that have modified a file
  172. git log --raw --abbrev=40 --pretty=oneline | grep -B 1 `git hash-object <filename>`
  173.  
  174. ### Commits in branchA that are not in branchB
  175. git log branchA ^branchB
  176.  
  177. ## Comparing
  178. git diff
  179. git diff --staged
  180. git diff HEAD
  181. git diff 99a0de8
  182. git diff 99a0de8..67aab84
  183.  
  184. git diff -- path/to/file
  185.  
  186. git log <branch1>..<branch2> # commits in <branch2> those are not in <branch1>
  187.  
  188. ## Remote repositories
  189. git remote add <remote_repo> git@gist.github.com:747678.git
  190. git remote rm <remote_repo>
  191.  
  192. git fetch <remote_repo>
  193. git fetch <remote_repo> <local_branch>:remotes/<remote_repo>/<remote_branch>
  194. git fetch --all
  195.  
  196. git pull
  197. git pull <remote_repo> <remote_branch>
  198. git pull <remote_repo> <remote_branch>:<local_branch>
  199. git pull -f
  200. git pull --rebase <remote_repo> <remote_branch> # fetch and rebase
  201.  
  202. git push
  203. git push <remote_repo> <remote_branch>
  204. git push <remote_repo> <local_branch>:<remote_branch>
  205. git push -f
  206.  
  207. ## Statistics
  208. git diff --stat HEAD~10
  209. git diff --stat HEAD~10 HEAD
  210. git diff --stat 99a0de8 67aab84
  211.  
  212. git diff --shortstat HEAD~10
  213. git diff --numstat HEAD~10
  214.  
  215. git diff --stat -p HEAD
  216.  
  217. git log --stat
  218. git log --shortstat
  219.  
  220. ## Fixing commits
  221. git commit --amend
  222. git commit --amend -C HEAD
  223.  
  224. git reset --hard HEAD^
  225. git reset --soft HEAD^ # removing last commit without removing the changes
  226.  
  227. ## Reverting
  228. git revert 99a0de8
  229. git revert --no-commit 99a0de8
  230.  
  231. ## Removing commits
  232. git reset --hard HEAD~3 # removing the last 3 commits
  233. git reset --hard origin/master # reseting to the remote state
  234. git rebase -i 99a0de8^ # using editor to remove the line with 99a0de8
  235.  
  236. ## Bisect
  237. git bisect start
  238. git bisect bad
  239. git bisect good 99a0de8
  240. # bisect process
  241. git bisect reset
  242.  
  243. git bisect start HEAD 99a0de8
  244. git bisect run /path/to/test/script
  245. git bisect reset
  246.  
  247. ## Blame
  248. git blame path/to/filename
  249.  
  250. ## Init a remote (bare) repository
  251. git init --bare path/to/repo.git
  252.  
  253. ## Creating and Applying patches
  254.  
  255. ### Patch with diff
  256. git diff --no-prefix > path/to/patch
  257. patch -p0 < path/to/patch
  258.  
  259. ### Patch with format-patch
  260. git format-patch -M 99a0de8 # creating one patch file for each commit
  261. git format-patch master --stdout > path/to/patch.patch # creating one full patch file
  262. git apply --stat path/to/patch.patch # check differences before appling
  263. git apply --check path/to/patch.patch # checking problems before appling
  264. git am path/to/patch.patch # apply the patch
  265. git am --signoff path/to/patch.patch # apply the patch and sign off it
  266. git apply path/to/patch.patch # apply the patch but not stage the changes
  267.  
  268. ## Playing with index
  269.  
  270. git update-index --assume-unchanged .rvmrc
Add Comment
Please, Sign In to add comment