In this lesson, we will do the following:
- Create a remote repository
- Create a local copy of the remote repository
- Create two branches in the local copy
- Push one branch to the remote repository
- Delete local branches
- Delete remote branches
The lesson should give you an overall understanding of the branch creation and deletion process, so you have a good command over the necessary steps when you need to delete a local or remote branch.
Let’s get started.
1. Creating a Remote Repository
Let’s create a folder called project.git and initialize to be the remote repository:
$ cd project.git
$ git init –bare
Initialized empty Git repository in /Users/zak/_work/LearnGIT/git_delete_branch/project.git/
2. Creating a Local Copy of the Remote Repository
In a new location, let’s create a local copy called project_local of the remote repository using the clone command.
Note: If you are working with the GitHub or BitBucket, you’ll follow the same process to clone the repository. In that case, you will have an SSH link instead of the full file path used here.
Cloning into ‘project_local’…
warning: You appear to have cloned an empty repository.
done.
3. Creating Branches Inside the Local Copy
Let’s first add a file to the local copy and then push it to the remote repository:
$ touch ReadMe.txt
$ git add -A
$ git commit -m "Initializing the Module"
[master (root-commit) 81eb2a3] Initializing the Module
1 file changed, 0 insertions(+), 0 deletions(–)
create mode 100644 ReadMe.txt
$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 221 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /Users/zak/_work/LearnGIT/git_delete_branch/project.git
* [new branch] master –> master
In the above commands, we created a file called ReadMe.txt, added it to the local copy, committed it to the local copy, and then pushed the changes to the remote repository or origin’s master branch.
If you check the branches, you’ll see the master branch in the local copy:
* master
If you check the remote branches, you will see the master branch there also:
origin/master
Hint: You can use the ‘-a’ option to see all branches in local and remote repositories together.
* master
remotes/origin/master
Let’s create two branches called b1 and b2 from the master branch:
$ git branch b2
Let’s check if the branches were created:
b1
b2
* master
Now we are going to make some modifications to the branches:
Switched to branch ‘b1’
$ touch branch1.txt
$ git add -A
$ git commit -m "Branch1 modification"
[b1 a2f488e] Branch1 modification
1 file changed, 0 insertions(+), 0 deletions(–)
create mode 100644 branch1.txt
$ git checkout b2
Switched to branch ‘b2’
$ touch branch2.txt
$ git add -A
$ git commit -m "Branch2 modification"
[b2 2abb723] Branch2 modification
1 file changed, 0 insertions(+), 0 deletions(–)
create mode 100644 branch2.txt
Let’s check local and remote branch statuses:
b1
* b2
master
$ git branch -r
origin/master
We can see locally we have three branches master, b1, and b2. But we have only the master branch in the remote repository.
4. Pushing Branches to Remote Repository
Let’s push the b1 branch to the remote repository:
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 249 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To /Users/zakh/_work/LearnGIT/git_delete_branch/project.git
* [new branch] b1 –> b1
You can check the local and remote branch statuses:
b1
* b2
master
$ git branch -r
origin/b1
origin/master
From the above branch statuses, we can see that the b1 branch is available remotely also.
5. Deleting Branches Locally
You can delete branches locally with the -d or -D option.
Let’s first check into the master branch, so we can delete the b1 and b2 branches.
Switched to branch ‘master’
Your branch is up-to-date with ‘origin/master’.
Let’s try the -d option first to delete the branch b1:
error: The branch ‘b1’ is not fully merged.
If you are sure you want to delete it, run ‘git branch -D b1’.
The error is telling you that you have to merge the changes from branch b1. This is a safeguard, so you don’t mistakenly lose your work on branches. You can use the -D option to force delete the merge. But in this case, let’s merge the changes from b1 and b2 into master and push it to the remote repository.
Updating 81eb2a3..a2f488e
Fast-forward
branch1.txt | 0
1 file changed, 0 insertions(+), 0 deletions(–)
create mode 100644 branch1.txt
$ git merge b2
Merge made by the ‘recursive’ strategy.
branch2.txt | 0
1 file changed, 0 insertions(+), 0 deletions(–)
create mode 100644 branch2.txt
$ git push origin master
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 454 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
To /Users/zak/_work/LearnGIT/git_delete_branch/project.git
81eb2a3..34db496 master –> master
Now try to delete the branches again:
b1
b2
* master
$ git branch -d b1
Deleted branch b1 (was a2f488e).
$ git branch -d b2
Deleted branch b2 (was 2abb723).
$ git branch
* master
You have successfully deleted the b1 and b2 branches locally.
6. Deleting Remote Branches
When you check the remote branches, you still see b1 present:
origin/b1
origin/master
You can use the following command to delete a remote branch:
So you can delete the remote b1 branch with the following:
To /Users/zakh_eecs/_work/LearnGIT/git_delete_branch/project.git
– [deleted] b1
Now if you check your remote branches, you shouldn’t see b1 anymore:
origin/master
Congratulations! You have successfully deleted all the branches you created. Practice making more branches and deleting them to master the Git branch deletion process.