]> rtime.felk.cvut.cz Git - hubacji1/oneflow.git/blob - branching.md
Add coffe
[hubacji1/oneflow.git] / branching.md
1 # Branching
2 It's not a good idea to multitask when working. However, some projects just
3 need it. The reason can be multiple features you are developing in parallel or
4 more than one guy working on the project. To keep track of different changes
5 that are related, the concept of branches was introduced.
6
7 Branch is just alternative history that can be merged one day back to the main
8 history. The main history branch is named `master` by default.
9
10 # Checkout and reset to history
11 NOTE: Commit all the changes before playing around with git history. It means
12 that `git status` says *nothing to commit, working tree clean*.
13
14 `git checkout COMMIT_ID` changes *HEAD* (where we are in git history right now)
15 to some commit. Working tree is still clean.
16
17 `git reset COMMIT_ID` again changes *HEAD* to some commit. However, it also put
18 all the affected *patches* (patches that was commited after) to working
19 directory (not staged, nor in git history).
20
21 `git reset --hard COMMIT_ID` do the same as `git reset COMMIT_ID` except it
22 *deletes all the affected patches*.
23
24 # Explore branches
25 `git branch` command show all the local branches and the active branch you are
26 currently on. Show remote branches by `git branch --all` command.
27
28 For `git log` command, add `--branches` or `--all` parameters:
29
30 `git log --oneline --graph --decorate --all`
31
32 # Create and switch branches
33 When decided to create new branch `git branch NAME` can be used. The branch
34 will be created from the current state called *HEAD*. Switching between
35 branches is done by `git checkout NAME` command.
36
37 If branch creation from specified commit is prefered, use `git branch NAME
38 COMMIT_ID`.
39
40 # Merge multiple branches
41 To the current branch, any other branch may be merged. The result is history
42 that contains all commits in both branches. Command `git merge --no-ff NAME`
43 will merge the branch `NAME` to itself. `--no-ff` avoids melting the branch
44 being merged. It's hard to explain, better to show.
45
46 # Change the base commit of branch
47 Each branch start at some commit. This starting commit of the branch can be
48 also changed. The usual case is keeping clean history. The command `git rebase
49 COMMIT_ID` takes current branch and put it on top of the `COMMIT_ID`. Also,
50 `git rebase NAME` could be used for rebasing current branch on top of other.
51
52 # Cheat sheet
53 ## Checkout and reset
54 - `git checkout COMMIT_ID`
55 - `git reset COMMIT_ID`
56 - `git reset --hard COMMIT_ID`
57
58 ## Explore
59 - `git branch`
60 - `git branch --all`
61 - `git log --oneline --graph --decorate --branches`
62 - `git log --oneline --graph --decorate --all`
63
64 ## Create
65 - `git branch NAME`
66 - `git branch NAME COMMIT_ID`
67 - `git checkout NAME`
68
69 ## Merge
70 - `git merge --no-ff NAME`
71
72 ## Rebase
73 - `git rebase COMMIT_ID`
74 - `git rebase -i COMMIT_ID`
75 - `git rebase NAME`