You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

782 lines
33 KiB

  1. ################################################################################
  2. ┏━╸╻╺┳╸ ╻ ╻┏━┓┏━┓╻┏ ┏━┓╻ ╻┏━┓┏━┓
  3. ┃╺┓┃ ┃ ╺━╸ ┃╻┃┃ ┃┣┳┛┣┻┓┗━┓┣━┫┃ ┃┣━┛
  4. ┗━┛╹ ╹ ┗┻┛┗━┛╹┗╸╹ ╹┗━┛╹ ╹┗━┛╹
  5. 27. September 2022
  6. Pranger Stefan
  7. ################################################################################
  8. ┏━┓┏━┓┏━┓╺┳┓┏┳┓┏━┓┏━┓
  9. ┣┳┛┃ ┃┣━┫ ┃┃┃┃┃┣━┫┣━┛
  10. ╹┗╸┗━┛╹ ╹╺┻┛╹ ╹╹ ╹╹
  11. - git basics, what is git capable of
  12. - what do to in what situations
  13. - project management
  14. - pull requests and feature branches
  15. - bug tracking
  16. - continuous integration
  17. - git gadgets (grep, bisect, etc.)
  18. ################################################################################
  19. ┏┳┓┏━┓╻┏┓╻ ┏━┓┏━┓╻┏┓╻╺┳╸┏━┓
  20. ┃┃┃┣━┫┃┃┗┫ ┣━┛┃ ┃┃┃┗┫ ┃ ┗━┓
  21. ╹ ╹╹ ╹╹╹ ╹ ╹ ┗━┛╹╹ ╹ ╹ ┗━┛
  22. - Staging Area and
  23. - Interactive Rebasing for well crafted commits,
  24. - Merging and Rebasing,
  25. - Pull Request workflow and
  26. - Git as inspection tool.
  27. ################################################################################
  28. ┏━╸╻╺┳╸ ┏┓ ┏━┓┏━┓╻┏━╸┏━┓
  29. ┃╺┓┃ ┃ ┣┻┓┣━┫┗━┓┃┃ ┗━┓
  30. ┗━┛╹ ╹ ┗━┛╹ ╹┗━┛╹┗━╸┗━┛
  31. - _distributed_ version control
  32. - staging area
  33. - git add, git reset, etc. instead of only commiting
  34. - branching
  35. - "killer feature"
  36. - content-addressable filesystem
  37. ################################################################################
  38. ┏━╸╻╺┳╸ ╻┏┓╻╻╺┳╸
  39. ┃╺┓┃ ┃ ┃┃┗┫┃ ┃
  40. ┗━┛╹ ╹ ╹╹ ╹╹ ╹
  41. Create or clone a repository via
  42. - $ git init
  43. - $ git clone
  44. Basic configuration:
  45. - $ git config --global user.name "Foo Bar"
  46. - $ git config --global user.email foobar@example.com
  47. ################################################################################
  48. ┏━┓╺┳╸┏━┓┏━╸╻┏┓╻┏━╸ ┏━┓┏━┓┏━╸┏━┓
  49. ┗━┓ ┃ ┣━┫┃╺┓┃┃┗┫┃╺┓ ┣━┫┣┳┛┣╸ ┣━┫
  50. ┗━┛ ╹ ╹ ╹┗━┛╹╹ ╹┗━┛ ╹ ╹╹┗╸┗━╸╹ ╹
  51. - first main difference to SVN
  52. - advantage of git
  53. Files can be:
  54. ┌─────────┐ ┌──────────┐ ┌────────┐ ┌──────┐ ┌────────┐
  55. │Untracked│ │Unmodified│ │Modified│ │Staged│ │Commited│
  56. └─────────┘ └──────────┘ └────────┘ └──────┘ └────────┘
  57. Git manages these states as:
  58. ┌─────────────────┐ ┌─────┐ ┌────┐
  59. │Working Directory│ │Index│ │HEAD│
  60. └─────────────────┘ └─────┘ └────┘
  61. Your Sandbox The proposed The next
  62. next commit parent
  63. ################################################################################
  64. ┏━┓╺┳╸┏━┓┏━╸╻┏┓╻┏━╸ ┏━┓┏━┓┏━╸┏━┓
  65. ┗━┓ ┃ ┣━┫┃╺┓┃┃┗┫┃╺┓ ┣━┫┣┳┛┣╸ ┣━┫
  66. ┗━┛ ╹ ╹ ╹┗━┛╹╹ ╹┗━┛ ╹ ╹╹┗╸┗━╸╹ ╹
  67. - git add
  68. - git add --patch (-p)
  69. - add individual lines (hunks) to commits
  70. - "interactive staging"
  71. - very useful to craft commits that are easy to understand
  72. - git restore --staged/--worktree
  73. - git reset --soft/--mixed/--hard
  74. - git rm --cached
  75. ################################################################################
  76. ┏━┓╺┳╸┏━┓┏━╸╻┏┓╻┏━╸ ┏━┓┏━┓┏━╸┏━┓
  77. ┗━┓ ┃ ┣━┫┃╺┓┃┃┗┫┃╺┓ ┣━┫┣┳┛┣╸ ┣━┫
  78. ┗━┛ ╹ ╹ ╹┗━┛╹╹ ╹┗━┛ ╹ ╹╹┗╸┗━╸╹ ╹
  79. Check your modifications or what you are about to commit
  80. - git diff / git diff --staged
  81. Should help to have a clean process of commiting changes
  82. Finalizing step: git commit
  83. What if I messed up my current commit?
  84. - $ git commit --amend
  85. ################################################################################
  86. ┏━╸╻╺┳╸ ┏━┓┏┓ ┏┓┏━╸┏━╸╺┳╸┏━┓
  87. ┃╺┓┃ ┃ ┃ ┃┣┻┓ ┃┣╸ ┃ ┃ ┗━┓
  88. ┗━┛╹ ╹ ┗━┛┗━┛┗━┛┗━╸┗━╸ ╹ ┗━┛
  89. ┌────────────────┐ ┌──────────────────────┐main.c┌────┐
  90. │commit (b3aacfe)│───>│tree (think directory)│─────>│blob│
  91. └────────────────┘ └──────────────────────┘ └────┘
  92. | parent \
  93. v \
  94. ┌────────────────┐ ┌──────────────────────┐ \
  95. ┌──>│commit (a21fh1a)│───>│ tree │ \
  96. | └────────────────┘ └──────────────────────┘ \
  97. ┌───┐ / | \ \
  98. │tag│ main.c config.h main.h \
  99. └───┘ / | \ \
  100. ┌────┐ ┌────┐ ┌────┐<--°
  101. │blob│ │blob│<-┐ │blob│ |
  102. └────┘ └────┘ \ └────┘ |
  103. └───────────┘
  104. ################################################################################
  105. ┏━┓┏━╸╻ ╻╻┏━┓╻┏━┓┏┓╻ ┏━┓┏━╸╻ ┏━╸┏━╸╺┳╸╻┏━┓┏┓╻
  106. ┣┳┛┣╸ ┃┏┛┃┗━┓┃┃ ┃┃┗┫ ┗━┓┣╸ ┃ ┣╸ ┃ ┃ ┃┃ ┃┃┗┫
  107. ╹┗╸┗━╸┗┛ ╹┗━┛╹┗━┛╹ ╹ ┗━┛┗━╸┗━╸┗━╸┗━╸ ╹ ╹┗━┛╹ ╹
  108. ┌────────────────┐
  109. │commit (b3aacfe)│
  110. └────────────────┘
  111. - $ git show <commit-ish> -- <glob-pattern>
  112. - commits are referencable via
  113. - their SHA1
  114. - HEAD
  115. - tags/branchnames
  116. - Syntax:
  117. - HEAD~<n> // select the n-th ancestor of HEAD
  118. - <commit-ish>..<commit-ish>
  119. - HEAD-history:
  120. - HEAD@{<n>}
  121. ################################################################################
  122. ┏┓ ┏━┓┏━┓┏┓╻┏━╸╻ ╻╻┏┓╻┏━╸
  123. ┣┻┓┣┳┛┣━┫┃┗┫┃ ┣━┫┃┃┗┫┃╺┓
  124. ┗━┛╹┗╸╹ ╹╹ ╹┗━╸╹ ╹╹╹ ╹┗━┛
  125. - git's "killer feature"
  126. - A new branch is essentially just opening a new history
  127. (i.e. no copying involved!)
  128. - Highly encouraged to use many (shortlived) branches
  129. Create a new branch with
  130. - $ git switch --create/-c <branch-name>
  131. ################################################################################
  132. ┏┓ ┏━┓┏━┓┏┓╻┏━╸╻ ╻╻┏┓╻┏━╸
  133. ┣┻┓┣┳┛┣━┫┃┗┫┃ ┣━┫┃┃┗┫┃╺┓
  134. ┗━┛╹┗╸╹ ╹╹ ╹┗━╸╹ ╹╹╹ ╹┗━┛
  135. Switching between branches with
  136. - $ git switch <branch-name>
  137. - $ git switch -
  138. Cannot switch if that would overwrite modifications
  139. Untracked files will simply "move along"
  140. Delete or rename a branch:
  141. - $ git branch -d <branch-name>
  142. - $ git branch -m <oldbranch> <newbranch>
  143. ################################################################################
  144. ┏━╸╻╺┳╸ ┏━┓╺┳╸┏━┓┏━┓╻ ╻
  145. ┃╺┓┃ ┃ ┗━┓ ┃ ┣━┫┗━┓┣━┫
  146. ┗━┛╹ ╹ ┗━┛ ╹ ╹ ╹┗━┛╹ ╹
  147. Very helpful to quickly safe modifications
  148. - $ git stash push --message/-m "..."
  149. - $ git stash list
  150. - $ git stash apply/pop
  151. ################################################################################
  152. ┏━╸╻╺┳╸ ┏━┓╺┳╸┏━┓┏━┓╻ ╻
  153. ┃╺┓┃ ┃ ┗━┓ ┃ ┣━┫┗━┓┣━┫
  154. ┗━┛╹ ╹ ┗━┛ ╹ ╹ ╹┗━┛╹ ╹
  155. $ git stash push -m "WIP some frontend feature"
  156. $ git switch stable
  157. ...
  158. $ git commit
  159. $ git switch frontend_feature
  160. $ git stash pop
  161. ################################################################################
  162. ╻┏┓╻╺┳╸┏━╸┏━┓┏━┓┏━╸╺┳╸╻╻ ╻┏━╸ ┏━┓┏━╸┏┓ ┏━┓┏━┓╻┏┓╻┏━╸
  163. ┃┃┗┫ ┃ ┣╸ ┣┳┛┣━┫┃ ┃ ┃┃┏┛┣╸ ┣┳┛┣╸ ┣┻┓┣━┫┗━┓┃┃┗┫┃╺┓
  164. ╹╹ ╹ ╹ ┗━╸╹┗╸╹ ╹┗━╸ ╹ ╹┗┛ ┗━╸ ╹┗╸┗━╸┗━┛╹ ╹┗━┛╹╹ ╹┗━┛
  165. - $ git rebase -i <commit-ish>
  166. Can be used to cleanup commit history
  167. - allows to basically change the history of your commits
  168. - each project should have a guideline
  169. - small feature may for example be _squashed_ together
  170. - WIP- or unnecessary commits can be removed
  171. - remove _checkpoint commits_
  172. -> $ git rebase -i my_current_feature@{24.hours.ago}~1
  173. ################################################################################
  174. ╻┏┓╻╺┳╸┏━╸┏━┓┏━┓┏━╸╺┳╸╻╻ ╻┏━╸ ┏━┓┏━╸┏┓ ┏━┓┏━┓╻┏┓╻┏━╸
  175. ┃┃┗┫ ┃ ┣╸ ┣┳┛┣━┫┃ ┃ ┃┃┏┛┣╸ ┣┳┛┣╸ ┣┻┓┣━┫┗━┓┃┃┗┫┃╺┓
  176. ╹╹ ╹ ╹ ┗━╸╹┗╸╹ ╹┗━╸ ╹ ╹┗┛ ┗━╸ ╹┗╸┗━╸┗━┛╹ ╹┗━┛╹╹ ╹┗━┛
  177. # Commands:
  178. # p, pick <commit> = use commit
  179. # r, reword <commit> = use commit, but edit the commit message
  180. # e, edit <commit> = use commit, but stop for amending
  181. # s, squash <commit> = use commit, but meld into previous commit
  182. # f, fixup <commit> = like "squash", but discard this commit's log message
  183. ...
  184. #
  185. # These lines can be re-ordered; they are executed from top to bottom.
  186. #
  187. # If you remove a line here THAT COMMIT WILL BE LOST.
  188. ################################################################################
  189. ┏━┓┏━╸┏┳┓┏━┓╺┳╸┏━╸┏━┓
  190. ┣┳┛┣╸ ┃┃┃┃ ┃ ┃ ┣╸ ┗━┓
  191. ╹┗╸┗━╸╹ ╹┗━┛ ╹ ┗━╸┗━┛
  192. Remote server to share code base
  193. - Gets setup with $ git clone
  194. - $ git remote add <name> <url>
  195. - $ git fetch
  196. - $ git pull <remote> <branch>
  197. - $ git push <remote> <branch>
  198. ################################################################################
  199. ┏┳┓┏━╸┏━┓┏━╸╻┏┓╻┏━╸
  200. ┃┃┃┣╸ ┣┳┛┃╺┓┃┃┗┫┃╺┓
  201. ╹ ╹┗━╸╹┗╸┗━┛╹╹ ╹┗━┛
  202. Multiple parallel workflows are encouraged
  203. -> unavoidable conflicts
  204. - Project guideline on how to handle conflicts
  205. - For more advanced usage:
  206. - $ git rerere
  207. - different merging strategies
  208. ################################################################################
  209. ┏┳┓┏━╸┏━┓┏━╸╻┏┓╻┏━╸ ┏━╸┏━╸
  210. ┃┃┃┣╸ ┣┳┛┃╺┓┃┃┗┫┃╺┓ ╺━╸ ┣╸ ┣╸
  211. ╹ ╹┗━╸╹┗╸┗━┛╹╹ ╹┗━┛ ╹ ╹
  212. - _fast-forward_ merge possible if HEAD direct ancestor
  213. - This allows for a clean, linear history on stable branches
  214. - helps with git blame, git bisect, git rebase -i
  215. ┌─────────────────────────────────────────────────────────────┐
  216. │[spranger@gitWorkshop/stagingArea] git merge more_int_changes│
  217. │Updating eff74ed..a2c365a │
  218. │Fast-forward │
  219. │ testfile1 | 4 ++-- │
  220. │ 1 file changed, 2 insertions(+), 2 deletions(-) │
  221. └─────────────────────────────────────────────────────────────┘
  222. - --ff, --no-ff, --ff-only
  223. ################################################################################
  224. ┏━┓┏━╸┏┓ ┏━┓┏━┓╻┏┓╻┏━╸
  225. ┣┳┛┣╸ ┣┻┓┣━┫┗━┓┃┃┗┫┃╺┓
  226. ╹┗╸┗━╸┗━┛╹ ╹┗━┛╹╹ ╹┗━┛
  227. Allows to move bases of branches (and more)
  228. - Most common usage: Update the base of your branch if outdated
  229. - Common strategy to _always_ ensure fast-forward merges
  230. - Very helpful in complicated branching settings
  231. Important Note:
  232. ┌─────────────────────────────────────────────────────────┐
  233. │Do not rebase commits that exist outside your repository │
  234. │ and that people (may) have based work on. │
  235. └─────────────────────────────────────────────────────────┘
  236. ################################################################################
  237. ┏━╸┏━┓┏┓╻┏━╸╻ ╻┏━╸╺┳╸┏━┓
  238. ┃ ┃ ┃┃┗┫┣╸ ┃ ┃┃ ┃ ┗━┓
  239. ┗━╸┗━┛╹ ╹╹ ┗━╸╹┗━╸ ╹ ┗━┛
  240. Git tracks (per hunk-) conflicts over three files:
  241. - base/common
  242. - ours
  243. - theirs
  244. - $ git config --global merge.conflictstyle diff3
  245. - Often helpful to have common state
  246. ################################################################################
  247. ┏━╸┏━┓┏┓╻┏━╸╻ ╻┏━╸╺┳╸┏━┓
  248. ┃ ┃ ┃┃┗┫┣╸ ┃ ┃┃ ┃ ┗━┓
  249. ┗━╸┗━┛╹ ╹╹ ┗━╸╹┗━╸ ╹ ┗━┛
  250. ┌────────────────────────┐ ┌────────────────────────┐
  251. │ #include <algorithms> │ │ int main() { │
  252. │ <<<<<<< HEAD │ │ int a = 5; │
  253. │ #include <sstream> │ │ <<<<<<< HEAD │
  254. │ ||||||| 9275968 │ │ int b = 4; │
  255. │ ======= │ │ ||||||| 9275968 │
  256. │ #include <string> │ │ int b = 8; │
  257. │>>>>>>> new_feature │ │ ======= │
  258. └────────────────────────┘ │ int b = 9; │
  259. │ >>>>>>> new_feature │
  260. └────────────────────────┘
  261. - Conflict markers appear in the file itself
  262. - Simplest way to handle:
  263. -> directly modify the file
  264. - Git difftool for visual conflict resolving
  265. ################################################################################
  266. ╺┳╸┏━╸┏━┓┏┳┓ ╻ ╻┏━┓┏━┓╻┏ ┏━╸╻ ┏━┓╻ ╻┏━┓
  267. ┃ ┣╸ ┣━┫┃┃┃ ┃╻┃┃ ┃┣┳┛┣┻┓┣╸ ┃ ┃ ┃┃╻┃┗━┓
  268. ╹ ┗━╸╹ ╹╹ ╹ ┗┻┛┗━┛╹┗╸╹ ╹╹ ┗━╸┗━┛┗┻┛┗━┛
  269. Team Workflows:
  270. - Merge vs. Rebase
  271. - Only fast-forward?
  272. - Keep merge commits?
  273. Branching Strategies:
  274. - Main - Development - Feature branches - Stable branches
  275. - Main - Feature branches
  276. - etc.
  277. ################################################################################
  278. ┏━╸┏━┓╻ ╻ ┏━┓┏┓ ┏━┓┏━┓┏━┓╺┳╸╻┏━┓┏┓╻
  279. ┃ ┃ ┃┃ ┃ ┣━┫┣┻┓┃ ┃┣┳┛┣━┫ ┃ ┃┃ ┃┃┗┫
  280. ┗━╸┗━┛┗━╸┗━╸╹ ╹┗━┛┗━┛╹┗╸╹ ╹ ╹ ╹┗━┛╹ ╹
  281. Git usage in the bigger picture.
  282. - Issue tracking,
  283. - Pull Request handling,
  284. - Assigning work,
  285. - Milestones, Projects, etc.
  286. Example frontends:
  287. - GitHub
  288. - Gitlab
  289. - Gitea
  290. - Gogs
  291. ################################################################################
  292. ╻┏━┓┏━┓╻ ╻┏━╸ ╺┳╸┏━┓┏━┓┏━╸╻┏ ┏━╸┏━┓
  293. ┃┗━┓┗━┓┃ ┃┣╸ ┃ ┣┳┛┣━┫┃ ┣┻┓┣╸ ┣┳┛
  294. ╹┗━┛┗━┛┗━┛┗━╸ ╹ ╹┗╸╹ ╹┗━╸╹ ╹┗━╸╹┗╸
  295. - Note-tacking,
  296. - feature requests,
  297. - track bug reports from clients.
  298. - Use labels to categorize
  299. - bug, good-first-issue, enhancement, etc.
  300. - Associate milestones and assignees
  301. An issue should eventually be turned into a pull request (PR).
  302. ################################################################################
  303. ┏━┓╻ ╻╻ ╻ ┏━┓┏━╸┏━┓╻ ╻┏━╸┏━┓╺┳╸┏━┓
  304. ┣━┛┃ ┃┃ ┃ ┣┳┛┣╸ ┃┓┃┃ ┃┣╸ ┗━┓ ┃ ┗━┓
  305. ╹ ┗━┛┗━╸┗━╸ ╹┗╸┗━╸┗┻┛┗━┛┗━╸┗━┛ ╹ ┗━┛
  306. At the heart of your team's workflow
  307. - Accompanies a branch
  308. - Gives the team
  309. - a plattform to discuss progress,
  310. - the branches history and
  311. - a list of all introduces changes.
  312. Project lead can review and ask for changes one a line-by-line basis
  313. Basis for Continuous Integration
  314. ################################################################################
  315. ┏━╸┏━┓┏┓╻╻ ╻┏━╸┏┓╻╺┳╸╻┏━┓┏┓╻┏━┓╻ ┏━╸┏━┓┏┳┓┏┳┓╻╺┳╸┏━┓
  316. ┃ ┃ ┃┃┗┫┃┏┛┣╸ ┃┗┫ ┃ ┃┃ ┃┃┗┫┣━┫┃ ┃ ┃ ┃┃┃┃┃┃┃┃ ┃ ┗━┓
  317. ┗━╸┗━┛╹ ╹┗┛ ┗━╸╹ ╹ ╹ ╹┗━┛╹ ╹╹ ╹┗━╸ ┗━╸┗━┛╹ ╹╹ ╹╹ ╹ ┗━┛
  318. Syntax:
  319. ┌─────────────────────────────────────┐
  320. │<type>[optional scope]: <description>│
  321. │ │
  322. │[optional body] │
  323. │ │
  324. │[optional footer(s)] │
  325. └─────────────────────────────────────┘
  326. - They enable
  327. - automated CHANGELOGs and,
  328. - automated semantic version bump and
  329. - greatly help with readability.
  330. ################################################################################
  331. ┏━╸┏━┓┏┓╻╻ ╻┏━╸┏┓╻╺┳╸╻┏━┓┏┓╻┏━┓╻ ┏━╸┏━┓┏┳┓┏┳┓╻╺┳╸┏━┓
  332. ┃ ┃ ┃┃┗┫┃┏┛┣╸ ┃┗┫ ┃ ┃┃ ┃┃┗┫┣━┫┃ ┃ ┃ ┃┃┃┃┃┃┃┃ ┃ ┗━┓
  333. ┗━╸┗━┛╹ ╹┗┛ ┗━╸╹ ╹ ╹ ╹┗━┛╹ ╹╹ ╹┗━╸ ┗━╸┗━┛╹ ╹╹ ╹╹ ╹ ┗━┛
  334. ┌──────────────────────────────────────────────────────────┐
  335. │feat: allow provided config object to extend other configs│
  336. │ │
  337. │BREAKING CHANGE: `extends` key in config file is now │
  338. │used for extending other config files │
  339. └──────────────────────────────────────────────────────────┘
  340. ################################################################################
  341. ┏┳┓╻┏━┓┏━╸┏━╸╻ ╻ ┏━┓┏┓╻┏━╸┏━┓╻ ╻┏━┓
  342. ┃┃┃┃┗━┓┃ ┣╸ ┃ ┃ ┣━┫┃┗┫┣╸ ┃ ┃┃ ┃┗━┓
  343. ╹ ╹╹┗━┛┗━╸┗━╸┗━╸┗━╸╹ ╹╹ ╹┗━╸┗━┛┗━┛┗━┛
  344. - Protect master branch
  345. - Repository setting for PRs help with team workflow
  346. - e.g. can be set to create merge commits.
  347. ################################################################################
  348. ┏━┓┏┓╻┏━┓╻ ╻ ╻┏━┓╻┏━┓ ╻┏┓╻┏━┓┏━┓┏━╸┏━╸╺┳╸╻┏━┓┏┓╻
  349. ┣━┫┃┗┫┣━┫┃ ┗┳┛┗━┓┃┗━┓ ╺━╸ ┃┃┗┫┗━┓┣━┛┣╸ ┃ ┃ ┃┃ ┃┃┗┫
  350. ╹ ╹╹ ╹╹ ╹┗━╸ ╹ ┗━┛╹┗━┛ ╹╹ ╹┗━┛╹ ┗━╸┗━╸ ╹ ╹┗━┛╹ ╹
  351. - Search through: code, modifications, commits or commit messages.
  352. - Look at history of lines of code
  353. - Binary search for bugs
  354. - List of most recent changes
  355. ################################################################################
  356. ┏━╸╻╺┳╸ ╻ ┏━┓┏━╸
  357. ┃╺┓┃ ┃ ┃ ┃ ┃┃╺┓
  358. ┗━┛╹ ╹ ┗━╸┗━┛┗━┛
  359. - $ git log
  360. - --patch, --after, --since, --before, --committer
  361. - -S"<search-pattern>" // code
  362. - -L <start>,<end>:<filename> // history of lines of code
  363. - -L :<function-name>:<filename> // history of a function
  364. - --grep "<search-pattern>" // commit messages
  365. - $ git log --oneline
  366. - $ git log --graph
  367. ################################################################################
  368. ┏━╸╻╺┳╸ ┏┓ ╻ ┏━┓┏┳┓┏━╸
  369. ┃╺┓┃ ┃ ┣┻┓┃ ┣━┫┃┃┃┣╸
  370. ┗━┛╹ ╹ ┗━┛┗━╸╹ ╹╹ ╹┗━╸
  371. - $ git blame <filename>
  372. ┌────────────────────────────────────────────────────────────────┐
  373. │[spranger@gitWorkshop/stagingArea] git blame testfile1 │
  374. │2 days ago Stefan ^3b4a655│ 1 │ #include <stdio.h> │
  375. │ │ 2 │ #include <algorithms> │
  376. │a day ago Stefan 08b88afb│ 3 │ #include <sstream> │
  377. │2 days ago Stefan ^3b4a655│ 4 │ │
  378. │ │ 5 │ int main() { │
  379. │3 hours ago Stefan a2c365ad│ 6 │ int a = 1; │
  380. │ │ 7 │ int b = 2; │
  381. │2 days ago Stefan ^3b4a655│ 8 │ │
  382. │2 days ago Stefan a0bd6d8c│ 9 │ │
  383. │ │ 10 │ return a+b; │
  384. │2 days ago Stefan ^3b4a655│ 11 │ } │
  385. └────────────────────────────────────────────────────────────────┘
  386. ################################################################################
  387. ┏━╸╻╺┳╸ ┏━╸┏━┓┏━╸┏━┓
  388. ┃╺┓┃ ┃ ┃╺┓┣┳┛┣╸ ┣━┛
  389. ┗━┛╹ ╹ ┗━┛╹┗╸┗━╸╹
  390. Powerful tool to search for code snippets
  391. - $ git grep "<search-pattern>"
  392. - -A <n>, -B <n>, -C <n>
  393. - --show-function/-p
  394. - --function-context/-W
  395. ################################################################################
  396. ┏━╸╻╺┳╸ ┏┓ ╻┏━┓┏━╸┏━╸╺┳╸
  397. ┃╺┓┃ ┃ ┣┻┓┃┗━┓┣╸ ┃ ┃
  398. ┗━┛╹ ╹ ┗━┛╹┗━┛┗━╸┗━╸ ╹
  399. Binary search to find commit that introduced a bug.
  400. ┌──────────────────────────────────────────┐
  401. │$ git bisect start │
  402. │$ git bisect bad <commit-ish> │
  403. │$ git bisect good <commit-ish> │
  404. └──────────────────────────────────────────┘
  405. ┌──────────────────────────────────────────┐
  406. │$ git bisect start HEAD HEAD~10 -- │
  407. │$ git bisect run make test │
  408. │$ git bisect reset │
  409. └──────────────────────────────────────────┘
  410. ################################################################################
  411. ┏━┓┏━┓╺┳╸┏━╸╻ ╻╻┏┓╻┏━╸
  412. ┣━┛┣━┫ ┃ ┃ ┣━┫┃┃┗┫┃╺┓
  413. ╹ ╹ ╹ ╹ ┗━╸╹ ╹╹╹ ╹┗━┛
  414. - Revert already commited changes
  415. - Pick a range of commits from different branches
  416. - Turn a commit into a git-readable file
  417. ################################################################################
  418. ┏━╸╻╺┳╸ ┏━┓┏━╸╻ ╻┏━╸┏━┓╺┳╸
  419. ┃╺┓┃ ┃ ┣┳┛┣╸ ┃┏┛┣╸ ┣┳┛ ┃
  420. ┗━┛╹ ╹ ╹┗╸┗━╸┗┛ ┗━╸╹┗╸ ╹
  421. Revert commits via new commits
  422. - Should be used when the commits are already public
  423. - $ git revert <commit-ish>..
  424. ################################################################################
  425. ┏━╸╻╺┳╸ ┏━╸╻ ╻┏━╸┏━┓┏━┓╻ ╻ ┏━┓╻┏━╸╻┏
  426. ┃╺┓┃ ┃ ┃ ┣━┫┣╸ ┣┳┛┣┳┛┗┳┛╺━╸┣━┛┃┃ ┣┻┓
  427. ┗━┛╹ ╹ ┗━╸╹ ╹┗━╸╹┗╸╹┗╸ ╹ ╹ ╹┗━╸╹ ╹
  428. Specify commits and apply them onto your HEAD
  429. - $ git cherry-pick <commit-ish>..
  430. - $ git cherry-pick ..master
  431. - essentially the same as $ git rebase master
  432. - Useful if e.g. a hotfix has been pushed to a stable branch
  433. - Might need to clean such commits before merging
  434. ################################################################################
  435. ┏━╸┏━┓┏━┓┏┳┓┏━┓╺┳╸ ┏━┓┏━┓╺┳╸┏━╸╻ ╻ ┏┓ ┏━┓┏━┓┏━┓╻ ╻ ╻
  436. ┣╸ ┃ ┃┣┳┛┃┃┃┣━┫ ┃ ╺━╸┣━┛┣━┫ ┃ ┃ ┣━┫ ┃╺╋╸ ┣━┫┣━┛┣━┛┃ ┗┳┛
  437. ╹ ┗━┛╹┗╸╹ ╹╹ ╹ ╹ ╹ ╹ ╹ ╹ ┗━╸╹ ╹ ┗━┛ ╹ ╹╹ ╹ ┗━╸ ╹
  438. In certain situations it might be sensible to send commits per mail
  439. - $ git format-patch -1 <commit-ish>
  440. - colleague does not need to checkout your branch
  441. - $ git apply <filename>
  442. ################################################################################
  443. ┏┳┓┏━┓┏━┓┏━╸ ┏━┓┏┓╻╺┳┓ ┏┳┓┏━┓┏━┓┏━╸
  444. ┃┃┃┃ ┃┣┳┛┣╸ ┣━┫┃┗┫ ┃┃ ┃┃┃┃ ┃┣┳┛┣╸
  445. ╹ ╹┗━┛╹┗╸┗━╸ ╹ ╹╹ ╹╺┻┛ ╹ ╹┗━┛╹┗╸┗━╸
  446. - git notes,
  447. - commit signing,
  448. - merge strategies,
  449. - git worktree,
  450. - refs,
  451. - git reflog,
  452. - porcelain commands and scripting,
  453. - the power of .gitconfig,
  454. - hooks on the server,
  455. - multiple remotes,
  456. - etc.