git-guide/branches.md
2024-05-03 17:06:33 +01:00

127 lines
4.4 KiB
Markdown

# Working with Branches
Branches are a way of working on a copy of the code without interfering with the main repository and running into merge conflicts with other developers constantly. Other people can still commit to the main branch or merge in other features but you will be free from all those distractions and free to work on your feature or issue. Lets visualise this below where you make a new branch called **FeatureA**
```mermaid
gitGraph
commit
commit
branch develop
checkout develop
commit
commit
commit
checkout main
commit
commit
commit
```
As you can see several more commits have been made to the **main** branch whilst you've been working in the **FeatureA** branch. We'll talk more about **merging** these changes together later, but for now lets look at creating and managing 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
```
## Deleting a local and remote branch
To delete a local branch you simple run:
```bash
# Make sure you are in the main branch
git checkout main
# Delete a local branch
git branch -D demo-branch
```
You'll get a confirmation that the local branch has been deleted. But now is a good time to clean up the remote branch, you can do this by running the following command:
```bash
git push origin --delete demo-branch
```
Now if you check back in the web gui you'll see only the main branch exists.
---
##### Follow me for more guides
[<img src="./img/mastodon.png" width="4%">](https://awscommunity.social/@Ric) [<img src="./img/linkedin.png" width="4%">](https://www.linkedin.com/in/richarvey/)