Home > Blockchain >  Will deleting one git branch affect other?
Will deleting one git branch affect other?

Time:09-14

I wanted to create a new branch ('branch1') from main branch, and i got this as a result:

git branches

both of the branches now have same code (033d3ba0) which is not what i wanted. I wanted to create branch1 that has the parent main. I would like to delete branch1 now, but i want to be sure that deleting branch1 will not affect main.

So my question is: will deleting branch 1 affect main branch?

CodePudding user response:

You could see branches as labels or pointers for a commit. So removal of one branch won't remove the other.

Also a commit should have a changeset (there are exceptions, but in general) . So no changes is no new commit(-hash).

I wanted to create a new branch ('branch1') from main branch, and i got this as a resul

This looks good and expected. Just make some file changes, commit them to branch1 (and maybe push) and see the difference!

It's recommended to check a tutorial for the basics. I personally think this is a good one: https://github.com/git-guides

CodePudding user response:

For git,
if you delete a branch B created from branch A
even when there are no new commits on branch B (so they still both point to the same commit)
then branch A is not affected;
please see the experiment below.

However, there is a nuance here.

You're interacting with some web interface built on top of git (e.g. Azure DevOps, GitLab, GitHub, BitBucket, etc.) so what that layer on top of git will do when you delete a branch created from another branch depends on the creators of that product.

For example, in GitHub when your PR is merged in a remote repo you are asked if you want to delete the source branch in the source repo and even if you want to delete the source repository. This has nothing really to do with git; it's GitHub's functionality.

Having said all that it would be highly unusual for a on-top-of-git product to delete the source branch in your scenario, especially without a prompt.

The experiment

repo666 on main
❯ git log
commit 5b2ed4372077461d77da92415ddb9fd95e0eff83 (HEAD -> main)
Author: Jean-Baptiste Emanuel Zorg
Date:   Wed Sep 14 09:18:14 2022  1000

    First commit

repo666 on main
❯ git checkout -b branchB
Switched to a new branch 'branchB'

repo666 on branchB
❯ git log
commit 5b2ed4372077461d77da92415ddb9fd95e0eff83 (HEAD -> branchB, main) <----- both branches point to the same commit 
Author: Jean-Baptiste Emanuel Zorg
Date:   Wed Sep 14 09:18:14 2022  1000

    First commit

repo666 on branchB
❯ git checkout main                                     <----- switch to another branch, you cannot delete the checked out branch  
Switched to branch 'main'

repo666 on main
❯ git branch -D branchB
Deleted branch branchB (was 5b2ed43).                   <----- 5b2ed43 matches the commit 

repo666 on main
❯ git branch
* main                                                  <------ main is still here

repo666 on main
  • Related