]> rtime.felk.cvut.cz Git - hubacji1/oneflow.git/blob - branching.md
Add checkout and reset to branching
[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 # Create and switch branches
29 When decided to create new branch `git branch NAME` can be used. The branch
30 will be created from the current state called *HEAD*. Switching between
31 branches is done by `git checkout NAME` command. Checkout to specific commit is
32 also possible by `git checkout COMMIT_ID`.
33
34 If branch creation from specified commit is prefered, use `git branch NAME
35 COMMIT_ID`.
36
37 # Merge multiple branches
38 To the current branch, any other branch may be merged. The result is history
39 that contains all commits in both branches. Command `git merge --no-ff NAME`
40 will merge the branch `NAME` to itself. `--no-ff` avoids melting the branch
41 being merged. It's hard to explain, better to show.
42
43 # Change the base commit of branch
44 Each branch start at some commit. This starting commit of the branch can be
45 also changed. The usual case is keeping clean history. The command `git rebase
46 COMMIT_ID` takes current branch and put it on top of the `COMMIT_ID`. Also,
47 `git rebase NAME` could be used for rebasing current branch on top of other.
48
49 # Cheat sheet
50 ## Explore
51 - `git branch`
52 - `git branch --all`
53
54 ## Create
55 - `git branch NAME`
56 - `git branch NAME COMMIT_ID`
57 - `git checkout NAME`
58 - `git checkout COMMIT_ID`
59
60 ## Merge
61 - `git merge --no-ff NAME`
62
63 ## Rebase
64 - `git rebase COMMIT_ID`
65 - `git rebase -i COMMIT_ID`
66 - `git rebase NAME`