Addin branching images and doc

This commit is contained in:
Ric Harvey 2024-05-03 16:53:17 +01:00
parent ad377aa397
commit 65fd405063
Signed by: ric
GPG key ID: CD63CE502B962F16
5 changed files with 84 additions and 1 deletions

View file

@ -1,4 +1,87 @@
## Branches # Working with Branches
## Creating a branch with Gitlab
In the Gitlab site when you open a project you'll have a little menu that pops up on the left hand side of the code like the image below.
![Just the main Branch](./img/main-branch.png)
You may have also noticed there is a little plus icon near this menu. Click this and select **new branch** from the menu that pops up.
![Select new Branch](./img/new-branch.png)
Now you are going to want to name the new branch to something sensible. Many people use the feature name or a ticket number to populate this. Once named click **create branch**
![Name and Create the Branch](./img/new-branch1.png)
If you now look in the drop down on the page you'll see this time there are two branches. The one in the box shows you which one you are currently viewing in the gui.
![Two Branches now avaliable](./img/2-branches.png)
## Creating a branch on the CLI
You can also create branches directly from the command line with just a couple of commands. Lets say you are working on *issue 99* from your ticket system and want to create a branch called **issue99** to track the work done on this, simply run the following:
```bash
git branch issue99
git checkout issue99
```
Now once you've added and committed code to this branch you may want to push it to the remote repository so others can work on. When you try and run ```git push``` you may get an error like this:
```
fatal: The current branch issuse99 has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin issuse99
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
```
Don't worry the answers is right in the text. because the branch doesn't exist remotely we need to tell git what to do. We will only have to do this once and it'll be sorted. Run the following:
```bash
git push --set-upstream origin issuse99
```
Now git will push the code to the remote repository and will also know what to do next time you run ```git push```
## Switching branches in the CLI
- Open the terminal
- run the following command
```bash
git pull
git checkout demo-branch
```
- You should see the following confirmation
```bash
branch 'demo-branch' set up to track 'origin/demo-branch'.
Switched to a new branch 'demo-branch'
```
Now you'll see that you're in a directory that currently matches your main branch because this is where you've **branched** from. However, if you go ahead and add new files and then commit them you'll find that when you switch back to the main branch those files won't be there.
- Run the following commands
```bash
touch NEWFILE.md
git add NEWFILE.md
git commit -m "adding file in demo-branch"
# optional if you want to see it in gitlab
git push
# Now switch back to the main branch
git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
# list the files to see NEWFILE.md isn't in the main directory
ls
```
--- ---
##### Follow me for more guides ##### Follow me for more guides

BIN
img/2-branches.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

BIN
img/main-branch.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

BIN
img/new-branch.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

BIN
img/new-branch1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB