Git How to create/remove branches in Local and Remote repository

eye-catch Other techs

Creating a branch is a starting point to develop a new feature or fix a bug. This is a basic command that we need to use in our daily work.

I will use my flutter sample repository to explain.

Sponsored links

How to create a branch

There is only main branch in the repository at the moment.

Firstly, we need to create a branch. A new branch can be created in the following command.

$ git checkout -b my-test-branch
Switched to a new branch 'my-test-branch'

This command is a shortcut command of the following two commands.

$ git branch my-test-branch
$ git checkout my-test-branch 
Switched to branch 'my-test-branch'

This means that a new branch is created based on the current branch. Make sure that you are in the right place to create a new branch. Execute git pull first to update the current repository to the latest state.

Check the following post if you are not familiar with git commands. It shows the most used commands in daily work.

Sponsored links

How to push a new branch to the remote branch

The previous command just creates a new branch in the local repository. It is not pushed to the remote repository. Only we can see it.

Let’s add a commit. It creates a file and commits it.

$ echo "test-file data" > test_file.txt
$ git add test_file.txt 
$ git commit -m "Add test_file.txt"
[my-test-branch 1d6c1d2] Add test_file.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test_file.txt

The new commit is added as expected.

$ git log --oneline 
1d6c1d2 (HEAD -> my-test-branch) Add test_file.txt
3665d26 (origin/main, origin/HEAD, main) Update select row with shift key

Let’s push it to the remote repository since it’s still only in the local. We can upload the new repository with git push but it shows an error because the destination is unknown yet.

$ git push
fatal: The current branch my-test-branch has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin my-test-branch

I always execute git push first and then copy the command and paste it.

$ git push --set-upstream origin my-test-branch
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 298 bytes | 99.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote: 
remote: Create a pull request for 'my-test-branch' on GitHub by visiting:
remote:      https://github.com/yuto-yuto/flutter_samples/pull/new/my-test-branch
remote: 
To https://github.com/yuto-yuto/flutter_samples.git
 * [new branch]      my-test-branch -> my-test-branch
Branch 'my-test-branch' set up to track remote branch 'my-test-branch' from 'origin'.

Now, the new branch is created in the remote repository.

The commit is shown only in my-test-branch.

How to remove a branch from local repository

We sometimes create a branch from a different commit or repository. The repository can keep in the local because it isn’t harmful but we want to delete it if it’s in the remote repository to keep the repository tidy.

The command is the following. -d is shortcut for --delete

git branch -d branch-name
git branch --delete branch-name

However, the command shows the following error.

$ git branch -d my-test-branch 
error: Cannot delete branch 'my-test-branch' checked out at '/home/yuto/root/development/flutter_samples'

Delete command can’t be executed as long as the current branch is the same branch. The current branch must be changed to something else in this case.

Let’s change the branch and try it again.

$ git checkout -
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
$ git branch -d my-test-branch 
warning: deleting branch 'my-test-branch' that has been merged to
         'refs/remotes/origin/my-test-branch', but not yet merged to HEAD.
Deleted branch my-test-branch (was 1d6c1d2).

git checkout - goes to the last branch. Then, the delete command works as expected.

Execute one of the following commands if you run into an error that says you need to add --force option to delete the branch due to some reason.

git branch --delete --force branch-name
git branch -D branch-name

How to remove a branch from local repository

The previous command deletes only from the local repository but not from the remote repository. We need to execute an additional command to delete it from the remote.

The command is as follows.

git push -d remote_name branch-name

remote_name is origin in most cases.

$ git push -d origin my-test-branch 
To https://github.com/yuto-yuto/flutter_samples.git
 - [deleted]         my-test-branch

The remote branch is deleted as expected. The branch no longer exists in the GitHub repository.

How to remove multiple branches at once

Let’s try to remove multiple branches at once. Let’s create two branches

$ git checkout -b test-branch1
Switched to a new branch 'test-branch1'
$ git checkout -b test-branch2
Switched to a new branch 'test-branch2'
$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
$ git branch
* main
  test-branch1
  test-branch2

We can remove multiple branches at once if the names are provided to the arguments.

$ git branch -d test-branch1 test-branch2
Deleted branch test-branch1 (was 3665d26).
Deleted branch test-branch2 (was 3665d26).

How to remove multiple branches with regex

Let’s try to do it in another way. Create two branches.

$ git branch test-branch3
$ git branch test-branch4
$ git branch
* main
  test-branch3
  test-branch4

To extract the desired branches, grep can be used with regex.

$ git branch | grep -E test-br[a-z]+[0-9]$
  test-branch3
  test-branch4

We can pass the result to xargs to execute git branch -d.

$ git branch | grep -E test-br[a-z]+[0-9]$ | xargs git branch -d
Deleted branch test-branch3 (was 3665d26).
Deleted branch test-branch4 (was 3665d26).

Check the following post if you want to know more about xargs command.

Git How to update all repos in a directory
Are there many git repositories to update? This post is for you if you don't want to change the directory and git pull many times. Let's update them at once with find and xargs commands.

Related Articles

Are you git beginner? Then, check the following posts too.

Git How to undo a commit to remove some modifications
Everyone can make a mistake. How can we undo a commit when we push a commit by mistake? How can we revert a commit from the middle of the history when the commit is no longer needed?

Comments

Copied title and URL