2 ![Kodi Logo](resources/banner_slim.png)
4 # Kodi's git-fu reference
5 This guide is intended as a **simple, non-exhaustive, git reference source**. It will help you get acquainted with command-line `git`, even if you never imagined yourself touching the dreaded beast with a 3m pole (that's 10 feet for those across the pound).
7 It is streamlined to Kodi's **[fork and pull](https://help.github.com/articles/about-collaborative-development-models/)** development workflow: create feature branch, write awesome code, open a pull request, get it reviewed by Kodi team members, merge pull request. Loop!
10 1. **[Introduction](#1-introduction)**
11 2. **[Document conventions](#2-document-conventions)**
12 3. **[Prerequisites](#3-prerequisites)**
13 4. **[Clone Kodi's repo into your machine](#4-clone-kodis-repo-into-your-machine)**
14 5. **[Working with branches](#5-working-with-branches)**
15 5.1. **[Create feature branch](#51-create-feature-branch)**
16 5.2. **[Push feature branch to origin](#52-push-feature-branch-to-origin)**
17 5.3. **[List branches](#53-list-branches)**
18 5.4. **[Switch between branches](#54-switch-between-branches)**
19 5.5. **[Backup branch](#55-backup-branch)**
20 5.6. **[Check branch status](#56-check-branch-status)**
21 5.7. **[Delete branch](#57-delete-branch)**
22 5.8. **[Rebase branch](#58-rebase-branch)**
23 6. **[Syncing branches](#6-syncing-branches)**
24 6.1. **[Fetch master branch from upstream and sync local master branch with it](#61-fetch-master-branch-from-upstream-and-sync-local-master-branch-with-it)**
25 6.2. **[Fetch feature branch from origin and sync local feature branch with it](#62-fetch-feature-branch-from-origin-and-sync-local-feature-branch-with-it)**
26 6.3. **[Push local feature branch to origin and sync remote feature branch with it](#63-push-local-feature-branch-to-origin-and-sync-remote-feature-branch-with-it)**
27 7. **[Working with commits](#7-working-with-commits)**
28 7.1. **[Create commit](#71-create-commit)**
29 7.2. **[Amend last commit](#72-amend-last-commit)**
30 7.3. **[Rename last commit](#73-rename-last-commit)**
31 7.4. **[Delete last commit](#74-delete-last-commit)**
32 7.5. **[Fix commit](#75-fix-commit)**
33 7.6. **[List commits](#76-list-commits)**
34 7.7. **[Cherry-pick commits](#77-cherry-pick-commits)**
35 8. **[Examples with nice screenies](#8-examples-with-nice-screenies)**
36 8.1. **[Rebase branch](#81-rebase-branch)**
37 8.2. **[Fix commits](#82-fix-commits)**
38 9. **[Going pro](#9-going-pro)**
39 9.1. **[Fetch an upstream pull request](#91-fetch-an-upstream-pull-request)**
40 9.2. **[Fetch a pull request someone submitted to your repository](#92-fetch-a-pull-request-someone-submitted-to-your-repository)**
41 9.3. **[Fetch a pull request or commit and apply them to another branch](#93-fetch-a-pull-request-or-commit-and-apply-them-to-another-branch)**
42 10. **[Other resources](#10-other-resources)**
43 10.1. **[Using git alias](#101-using-git-alias)**
46 **[git](https://en.wikipedia.org/wiki/Git)** is a distributed version control system, originally written by a brilliant, self-described **[bastard](https://lkml.org/lkml/2000/9/6/65)**. It is not the most easy version control system (VCS for short) to work with but it is very intuitive **once** you understand some concepts. Some of the most important ones are outlined below.
48 * **repository**: a place where files and their versioned history live. Also known as **repo**.
49 * **upstream**: Kodi's **[main repository](https://github.com/xbmc/xbmc)**. It's where everything you code will end up after review and a pull request (PR for short) is merged.
50 * **master**: the repository's main branch. It is where all the code integration happens.
51 * **remote**: the remote location of a repository, usually on some central server. Both Kodi's **main repository** and your **personal repository** are remotes.
52 * **origin**: your **personal repository**. Everything you code will end up in your personal repository after you `push`. You're master and commander.
53 * **fork**: create an exact copy of a remote repository, at that point in time, in your personal GitHub account.
54 * **clone**: grab an exact copy of a remote repository, usually from your personal repository, and place it on your local machine.
55 * **branch**: a magical place where what you code doesn't interfere with anything else. Think of it as an house compartment. You cook in the kitchen, sleep in the bedroom, have fun with friends while watching football in the living room, etc. Unlike house compartments, they are free and you can have as many as you like.
56 * **commit**: chunk of code you committed to. You think it's the best code you've ever written, decided to hang on to it and maybe rule the world one day.
57 * **pull request**: a request for someone to review your code and include it in Kodi's main repository.
58 * **fetch**: grab code from an outside source, usually from the main repository.
59 * **push**: send code to a remote repository, usually to your personal repository.
60 * **merge**: merge a feature branch with another branch, usually master branch.
62 ## 2. Document conventions
63 This guide assumes you are using `terminal`, also known as `console`, `command-line` or simply `cli`. Commands need to be run at the terminal, one at a time and in the provided order.
65 This is a comment that provides context:
67 this is a command // this is an inline comment
68 this is another command // you should not copy them
69 and yet another one // to the terminal
72 **Example:** Clone Kodi's current master branch:
74 git clone https://github.com/xbmc/xbmc kodi
77 Commands that contain strings enclosed in angle brackets denote something you need to change to suit your needs.
79 git clone -b <branch-name> https://github.com/xbmc/xbmc kodi
82 **Example:** Clone Kodi's current Krypton branch:
84 git clone -b Krypton https://github.com/xbmc/xbmc kodi
87 Several different strategies are used to draw your attention to certain pieces of information. In order of how critical the information is, these items are marked as a note, tip, or warning. For example:
90 > Linux is user friendly... It's just very particular about who its friends are.
93 > Algorithm is what developers call code they do not want to explain.
96 > Developers don't change light bulbs. It's a hardware problem.
98 **[back to top](#table-of-contents)** | **[back to section top](#2-document-conventions)**
101 This guide assumes you have a **[GitHub account](https://github.com/join)** and that you already **[forked Kodi's repository](https://github.com/xbmc/xbmc/fork/)** into it.
103 It also assumes `git` is installed and an SSH key is **[properly configured](https://help.github.com/articles/connecting-to-github-with-ssh/)**. How to install `git` can be found on our specific OS/distribution **[build guides](README.md)**.
105 Configure `git` globally so it knows who you are:
107 git config --global user.email "<your-email>"
108 git config --global user.name "<your-username>"
111 If you're a Windows user you also need:
113 git config --global core.autocrlf true
116 **[back to top](#table-of-contents)**
118 ## 4. Clone Kodi's repo into your machine
119 Your forked Kodi's repository will hold everything you do when developing. For safety, you should push every branch and commit you make during development to your remote repository (your GitHub's Kodi fork). You should also be careful not to push sensitive data, credentials, etc. Once online, always online.
121 Clone your personal repository into your machine:
123 cd $HOME // change to your `home` directory
124 git clone git@github.com:<github-username>/xbmc.git kodi // clone your fork of Kodi's repo
125 cd kodi // change to the newly cloned repo directory
126 git remote add upstream https://github.com/xbmc/xbmc.git // assign Kodi's main repo to a remote
130 > Windows users should use `cd %userprofile%` instead.
132 From this point forward, every command shown assumes you're inside `$HOME/kodi` or `%userprofile%\kodi` if you're a Windows user. To get there, issue:
134 cd $HOME/kodi // or cd %userprofile%\kodi
137 **[back to top](#table-of-contents)**
139 ## 5. Working with branches
140 Never ever work on master branch. **Ever!** It will get you and probably us into unnecessary trouble. Always create a feature branch.
142 ### 5.1. Create feature branch
144 git checkout master // make sure you're on master branch
145 git checkout -b <feature-branch> // create feature branch
148 ### 5.2. Push feature branch to origin
149 Pushing for the first time:
151 git push --set-upstream origin <feature-branch>
164 ### 5.3. List branches
170 List local and remote branches:
175 ### 5.4. Switch between branches
177 git checkout <feature-branch> // switch to feature branch
178 git checkout master // switch back to master branch
181 ### 5.5. Backup branch
183 git checkout <feature-branch> // switch to feature branch (the branch you want to backup)
184 git checkout -b <feature-branch>-backup // create a new branch based on feature branch
185 git checkout <feature-branch> // switch back to feature branch
188 ### 5.6. Check branch status
189 Shows branch name, status in relation to remote origin branch, unstaged changes, etc:
194 ### 5.7. Delete branch
197 git checkout master // make sure you're not on feature branch to delete
198 git branch -D <feature-branch> // delete feature branch
201 Delete remote branch:
203 git push origin -d <feature-branch>
207 > Be careful deleting branches. Make sure you don't need them anymore.
209 ### 5.8. Rebase branch
211 git checkout <feature-branch> // switch to feature branch in need of rebasing
212 git fetch upstream // fetch upstream changes
213 git rebase upstream/master // rebase feature branch on top of upstream master branch
214 git push -f origin // force push updated feature branch to your personal remote repo
217 **NOTE**: If you have a **pull request** open from that feature branch, this will also update it.
219 **[back to top](#table-of-contents)** | **[back to section top](#5-working-with-branches)**
221 ## 6. Syncing branches
222 Keeping branches up-to-date is an excellent base for successful, stress-free coding. You should always start coding a new feature on top of the latest master branch. If you work on more than one machine, this also ensures your feature branch is always up-to-date with the latest changes.
224 ### 6.1. Fetch master branch from upstream and sync local master branch with it
225 If you cloned a while ago, you probably want to sync your local master branch with upstream's master branch. This is specially useful to ensure that your new feature branch is based on the latest code version:
227 git checkout master // switch to master branch
228 git fetch upstream // fetch upstream changes
229 git merge upstream/master // merge upstream changes
230 git push origin // push updated master branch to your personal remote repo (optional)
233 ### 6.2. Fetch feature branch from origin and sync local feature branch with it
235 git checkout <feature-branch> // switch to feature branch
236 git fetch origin // fetch origin changes
237 git reset --hard origin/<feature-branch> // reset feature branch
240 ### 6.3. Push local feature branch to origin and sync remote feature branch with it
242 git checkout <feature-branch> // switch to feature branch
243 git push origin // push updated feature branch to your personal remote repo
247 > Be **very careful** updating to and from origin. It can cause loss of work, specially if you work on more than one machine. Make sure your remote origin repo **always** holds the most up-to-date version of your code. **No, seriously**. Make a mental rule: *remote origin repo always holds the most up-to-date version of my code!* and **stick to it!** It's almost always possible to recover lost work with `git` but it's hard and unnecessary work if you follow some simple rules.
249 **[back to top](#table-of-contents)** | **[back to section top](#6-syncing-branches)**
251 ## 7. Working with commits
252 All commit manipulating operations (commit, amend or fix a commit, etc.), start by adding your changes before committing:
254 git add path/to/file/filename // add single file
255 git add . // add all files on current directory
256 git add * // add all files in current directory and subdirectories
259 ### 7.1. Create commit
261 git commit -m "<commit-description>"
264 ### 7.2. Amend last commit
269 ### 7.3. Rename last commit
271 git commit --amend // change commit message in your editor and save
274 ### 7.4. Delete last commit
275 Delete last commit but keep all the changes it encompasses:
280 Delete last commit, **including all the changes** it encompasses:
282 git reset --hard HEAD~
287 git commit --fixup <commit-sha>
290 ### 7.6. List commits
292 git log --pretty=oneline --abbrev-commit
295 ### 7.7. Cherry-pick commits
297 git checkout <feature-branch> // switch to branch with commit(s) you want to cherry-pick
298 git log --pretty=oneline --abbrev-commit // list commits. take note of the commit(s) `sha`
299 git checkout <other-feature-branch> // switch to branch that will receive cherry-picked commit(s)
300 git cherry-pick <sha> // cherry-pick commit
301 git cherry-pick <sha> // cherry-pick another commit
302 git cherry-pick <sha> // you get the point
305 **[back to top](#table-of-contents)** | **[back to section top](#7-working-with-commits)**
307 ## 8. Examples with nice screenies
308 A picture is worth a thousand words.
310 ### 8.1. Rebase branch
312 Check if branch is clean and has no uncommitted changes:
313 ![Rebase branch 01](resources/gitfu/rebase_branch_01.png)
315 Fetch upstream changes and rebase feature branch on top of upstream master branch:
316 ![Rebase branch 02](resources/gitfu/rebase_branch_02.png)
318 Force push branch to remote origin:
319 ![Rebase branch 03](resources/gitfu/rebase_branch_03.png)
321 **[back to top](#table-of-contents)** | **[back to section top](#8-examples-with-nice-screenies)** | **[back to subsection top](#81-rebase-branch)**
325 Check unstaged changes:
326 ![Fix commits 01](resources/gitfu/fix_commits_01.png)
328 Add changes and check again:
329 ![Fix commits 02](resources/gitfu/fix_commits_02.png)
331 Print a nice commits log and copy `b2184ab68a`, the `sha` of the commit we want to fix:
332 ![Fix commits 03](resources/gitfu/fix_commits_03.png)
334 Commit the changes with `git commit --fixup b2184ab68a` to reference the commit in need of fixing:
335 ![Fix commits 04](resources/gitfu/fix_commits_04.png)
337 Check if everything is OK and count how many commits we need to use for an `autosquash` rebase (we need nine commits because commit in need of fixing is the ninth commit, including the `fixup` commit):
338 ![Fix commits 05](resources/gitfu/fix_commits_05.png)
340 Due to the nature of the `git rebase -i --autosquash HEAD~9` command (opens editor right away), command doesn't show up on this screenshot. See next screenshot.
341 How it looks after `git` re-orders commits automagically:
342 ![Fix commits 06](resources/gitfu/fix_commits_06.png)
344 **NOTE**: These examples use `nano` as default `git` editor to keep things simple. Press `Ctrl+X` to exit and accept the changes.
346 A successful message after rebase with `autosquash`:
347 ![Fix commits 07](resources/gitfu/fix_commits_07.png)
349 Finally, force push branch to remote origin:
350 ![Fix commits 08](resources/gitfu/fix_commits_08.png)
352 **TIP**: It doesn't matter how many `fixup` commits you have on your feature branch. The only thing that matters is the number of the **first** commit with a `fixup` commit. If you're unsure of how many commits that is, issue `git log --pretty=oneline --abbrev-commit`, count from your **first commit** on the feature branch and use that number instead. In this example, that would be 28, e.g. `git rebase -i --autosquash HEAD~28`. `git` will take care of the rest automagically.
354 **[back to top](#table-of-contents)** | **[back to section top](#8-examples-with-nice-screenies)** | **[back to subsection top](#82-fix-commits)**
357 Got this far? Congrats! Now it gets fun!
359 ### 9.1. Fetch an upstream pull request
360 The ability to test pull requests before merging them is very useful. This is how to do it:
362 git fetch upstream pull/<pull-request-ID>/head:<branch-name-or-pull-request-ID-or-whatever>
363 git checkout <branch-name-or-pull-request-ID-or-whatever>
366 Build Kodi as usual, following our **[build guides](README.md)**.
368 ### 9.2. Fetch a pull request someone submitted to your repository
369 Yes, someone might open a pull request to your repository. Be happy that folks want to work with you.
371 Same thing as above except that `upstream` is now `origin`. They are both remote repositories, remember?
373 git fetch origin pull/<pull-request-ID>/head:<branch-name-or-pull-request-ID-or-whatever>
374 git checkout <branch-name-or-pull-request-ID-or-whatever>
377 **TIP**: Every pull request has an unique ID. For instance, the original pull request for this guide is **[PR14072](https://github.com/xbmc/xbmc/pull/14072)**. Its ID is **14072**.
379 If you want to try it, issue:
381 git fetch upstream pull/14072/head:PR14072 // branch name can be anything
382 git checkout PR14072 // switch to branch
385 and see the original version of this documentation in all its glorious might.
387 ### 9.3. Fetch a pull request or commit and apply them to another branch
388 Also very useful is the ability to fetch a complete pull request or a single commit and apply it to your **current** branch, instead of creating a new branch like the method outlined above.
390 Similarly to the method above, find the PR you want to fetch, **[PR14072](https://github.com/xbmc/xbmc/pull/14072)** for instance, and append `.patch` to the URL. GitHub will generate a formatted patch and show you the output. Copy that URL and choose your next move below.
392 Apply the changes to your branch but do not commit them (changes will be kept unstaged):
394 curl -L https://patch-diff.githubusercontent.com/raw/xbmc/xbmc/pull/14072.patch | git apply -
397 Apply the changes to your branch with the original commit(s) message(s):
399 curl -L https://patch-diff.githubusercontent.com/raw/xbmc/xbmc/pull/14072.patch | git am -
402 The same tricks work for a single commit. Just find a commit you'd like to fetch, **[[README] Kodi's codebase new face](https://github.com/xbmc/xbmc/pull/14072/commits/bb62d22f0d62b20ebc2325f7820a37759839efe0)** for instance, append `.patch` to the URL and follow the instructions above.
404 **[back to top](#table-of-contents)** | **[back to section top](#9-going-pro)**
406 ## 10. Other resources
407 `git` is an incredibly powerful VCS. It's next to impossible to document everything it can do because it's so versatile, many tasks can be achieved in more than one way, and what works for a developer might not work for another. How one uses it heavily depends on the implemented workflow and, to some extent, personal taste. With that in mind, below are listed some external git resources worth a look with many exotic examples. Some you might only need once in a lifetime.
409 * **[git-tips](https://github.com/git-tips/tips)**
410 * **[git-cheat-sheet](https://github.com/arslanbilal/git-cheat-sheet)**
412 ### 10.1. Using git alias
413 Aliases are a great `git` feature. Instead of typing - and remembering - a long and boring command, one can instruct `git` to perform an action with a much shorter command. Hence the name aliases.
415 The command to add an alias to `git` is simple: `git config --global --add alias.<alias> <command>`.
417 For example, add an alias `st` to `status`:
419 git config --global --add alias.st status
422 Now you can just type `git st` instead of `git status`. Saves you a mammoth four keystrokes.
424 Some of my favorite aliases are:
426 `slog` instead of `log --pretty=oneline --abbrev-commit`:
428 git config --global --add alias.slog "log --pretty=oneline --abbrev-commit"
431 `ema` instead of `cherry-pick`:
433 git config --global --add alias.ema cherry-pick
436 `lg` instead of `log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative`:
438 git config --global --add alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
442 `fix` instead of `commit --fixup`:
444 git config --global --add alias.fix "commit --fixup" // git fix <commit-sha>
447 `fixer` instead of `rebase -i --autosquash`:
449 git config --global --add alias.fixer "rebase -i --autosquash" // git fixer HEAD~10 will autosquash the last 10 commits
452 **[back to top](#table-of-contents)** | **[back to section top](#10-other-resources)**