|
| 1 | +--- |
| 2 | +sidebar_position: 6 |
| 3 | +--- |
| 4 | + |
| 5 | +# Branch |
| 6 | + |
| 7 | +### Using Git branches |
| 8 | + |
| 9 | +A [branch](./Index.md/#branch) is a seperate version of the main repository.<br/> |
| 10 | +Branches are particuarly useful when a developer wants to update parts of a large project. |
| 11 | + |
| 12 | +Git branches allow development on a new features and designs independent from other work on the main branch.<br/> |
| 13 | +When the work is complete on a branch, it can be [merged](./Index.md/#merge) back into the main branch/project.<br/> |
| 14 | +With branches, it's also possible to work on different parts of a project simultaneously without any part impacting another. |
| 15 | + |
| 16 | +### Creating a new Branch |
| 17 | + |
| 18 | +Let's create a new [branch](./Index.md/#branch) to work on a feature without modifying the main project's working code: |
| 19 | + |
| 20 | +``` bash |
| 21 | +[user@localhost] $ git branch hello-world-pictures |
| 22 | +``` |
| 23 | +The command `git branch hello-world-pictures` will create a new [branch](./Index.md/#branch) called "hello-world-pictures".<br /> |
| 24 | +Let's confirm that the branch was created: |
| 25 | +``` bash |
| 26 | +[user@localhost] $ git branch |
| 27 | +hello-world-pictures |
| 28 | +* master |
| 29 | +``` |
| 30 | +> Important: Whenever creating new branches, the branch from which the *new* branch is sourced from is the branch which was mounted when the git branch command was called. In scenario above, the command was called from within the `master` branch. Hence, all the initial files and changes are sourced from the `master` branch for the `hello-world-pictures` branch. |
| 31 | +
|
| 32 | +A new branch called "hello-world-pictures" has been created, however the `*` symbol indicates that we are currently still on the `master` [branch](./Index.md/#branch). |
| 33 | +> When viewing the output of `git branch`, the `*` symbol indicates the current branch. |
| 34 | +
|
| 35 | + |
| 36 | + |
| 37 | +In order to leave the `master` [branch](./Index.md/#branch) and change to the "hello-world-pictures" [branch](./Index.md/#branch) we have to use the [`checkout`](./Index.md/#checkout) command. |
| 38 | + |
| 39 | +``` bash |
| 40 | +[user@localhost] $ git checkout hello-world-pictures |
| 41 | + Switched to branch 'hello-world-pictures' |
| 42 | +``` |
| 43 | +> Note: Adding the `-b` option on `git checkout` will create a new branch and move to it, if it does not exist. |
| 44 | +
|
| 45 | +Now let's make some edits now that we're in the correct branch. |
| 46 | +Let's add an [image](https://software.3metas.com/wp-content/uploads/2017/06/git.png)(needs to be downloaded) to our html page: |
| 47 | + |
| 48 | +First let's obtain the image: |
| 49 | +> In order to download the image via the CLI use one of the following commands from the same working directory as `index.html`: |
| 50 | +> - Windows: |
| 51 | + > ``` powershell |
| 52 | + > wget https://software.3metas.com/wp-content/uploads/2017/06/git.png > git.png |
| 53 | + > ``` |
| 54 | + > - Linux & Mac: |
| 55 | + > ``` bash |
| 56 | + > curl https://software.3metas.com/wp-content/uploads/2017/06/git.png > git.png |
| 57 | + > ``` |
| 58 | +
|
| 59 | +
|
| 60 | +Then let's update our html code: |
| 61 | +``` html |
| 62 | +<!DOCTYPE html> |
| 63 | +<html> |
| 64 | + <head> |
| 65 | + <title>Hello World!</title> |
| 66 | + <link rel="stylesheet" href="styles.css"> |
| 67 | + </head> |
| 68 | +
|
| 69 | + <body> |
| 70 | + <h1>Hello world!</h1> |
| 71 | + <div> |
| 72 | + <img src="git.png" alt="Git Logo" style="width:100%;max-width:960px"> |
| 73 | + </div> |
| 74 | + <p>This is the first file in my new Git Repo.</p> |
| 75 | + <p>A new line in our file!</p> |
| 76 | + </body> |
| 77 | +</html> |
| 78 | +``` |
| 79 | +
|
| 80 | +Let's [save and exit](./New%20Files.md/#basic-editing-on-vim) and check the status of the current [branch](./Index.md/#branch) |
| 81 | + |
| 82 | +``` bash |
| 83 | +[user@localhost] $ git status |
| 84 | +On branch hello-world-pictures |
| 85 | +Changes not staged for commit: |
| 86 | + (use "git add <file>..." to update what will be committed) |
| 87 | + (use "git restore <file>..." to discard changes in working directory) |
| 88 | + modified: index.html |
| 89 | + |
| 90 | +Untracked files: |
| 91 | + (use "git add <file>..." to include in what will be committed) |
| 92 | + git.png |
| 93 | + |
| 94 | +no changes added to commit (use "git add" and/or "git commit -a") |
| 95 | +``` |
| 96 | + |
| 97 | +The output indicates that there are tracked changes to `index.html`, however the file is not [staged](./Index.md/#stage) for a [commit](./Index.md/#commit). Moreover, there is also the image `git.png` that is currently untracked. |
| 98 | + |
| 99 | +So let's add all the changes to the [staging area](./Index.md/#staging-area): |
| 100 | +``` bash |
| 101 | +[user@localhost] $ git add --all |
| 102 | +``` |
| 103 | + |
| 104 | +Now let's check the status of the [branch](./Index.md/#branch): |
| 105 | +``` bash |
| 106 | +[user@localhost] $ git status |
| 107 | +On branch hello-world-pictures |
| 108 | +Changes to be committed: |
| 109 | +(use "git restore --staged <file>..." to unstage) |
| 110 | + new file: git.png |
| 111 | + modified: index.html |
| 112 | +``` |
| 113 | + |
| 114 | +Now all the changes are staged and can be committed. |
| 115 | + |
| 116 | +Since we're satifisied with our changes, let's [commit](./Index.md/#commit) them to our [branch](./Index.md/#branch): |
| 117 | +``` bash |
| 118 | +[user@localhost] $ git commit -m "feat: Added image to hello world" |
| 119 | +[hello-world-pictures 83d7fb0] feat: Added image to hello world |
| 120 | + 2 files changed, 3 insertions(+) |
| 121 | + create mode 100644 git.png |
| 122 | +``` |
| 123 | + |
| 124 | +### Switching between Branches |
| 125 | + |
| 126 | +Now let's see the power of branches! <br /> |
| 127 | +Let's check content within our branch: |
| 128 | + |
| 129 | +``` bash |
| 130 | +[user@localhost] $ ls |
| 131 | +git.png index.html README.md styles.css |
| 132 | +``` |
| 133 | +Notice how the `hello-world-pictures` [branch](./Index.md/#branch) has the image `git.png`. As per the edits we've made, `index.html` should also have modified code to include the image. |
| 134 | + |
| 135 | +Now's let's change to the `master` branch using the `checkout` command: |
| 136 | +``` bash |
| 137 | +[user@localhost] $ git checkout master |
| 138 | + Switched to branch 'master' |
| 139 | +``` |
| 140 | + |
| 141 | +Finally let's check the content of this [branch](./Index.md/#branch): |
| 142 | +``` bash |
| 143 | +[user@localhost] $ ls |
| 144 | +index.html README.md styles.css |
| 145 | +``` |
| 146 | +`git.png` is gone! Moreover, if we open and check `index.html` we can see that it no longer has the edits we made recently! |
| 147 | + |
| 148 | +Branches enable you to work on parts of a project independently. |
| 149 | + |
| 150 | +--- |
| 151 | +All rights belong to the author and/or owner of the [image](https://software.3metas.com/wp-content/uploads/2017/06/git.png). |
0 commit comments