3.6 KiB
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.
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.
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
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.
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:
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:
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
git pull
git checkout demo-branch
- You should see the following confirmation
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
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:
# 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:
git push origin --delete demo-branch
Now if you check back in the web gui you'll see only the main branch exists.