What exactly happens when deleting a branch in Git

I have been looking for the answer to this question for a while, but I am still not quite sure of its answer. Most of the information I found about deleting branches was nothing more than a copy of what the manual says (for example, here on SO ). I think the main problem is that I don’t know exactly what a branch is in Git (although there are many articles that claim to explain this). The only useful information I found is this SO answer and documents installed with git.

My question is:

When I started git branch -d BRANCH_NAME,

  • What happens under the hood?
  • what changes from the outside, i.e. How does my interaction with the repository change?
  • Can any of these changes be considered a change in history?

and also the same subqueries for git branch -d BRANCH_NAME.

My real understanding:

First, my perception of a branch: depending on the context, the term branch refers either to a pointer to a certain commit (strictly called a branch of a branch), or in the list of commits leading to this

What I think is happening (but I'm not sure) for git branch -d BRANCH_NAME:

  • the pointer to the head of the branch is deleted and no more
  • I can no longer see a branch in any list, and I cannot switch to or from it from her anymore (although, I think, I could create a new branch in this commit to effectively achieve these goals).
  • maybe not: commits still exist, they are no longer marked with a branch name.

What I think for git branch -d BRANCH_NAME:

  • , , ,
  • , - .
  • :
+4
2

, git branch -d git branch -d, git , . , , .

.

$ touch initial ; git add initial ; git commit -m 'Initial commit'
[master (root-commit) 2182bb2] Initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 initial

$ git checkout -b mybranch
Switched to a new branch 'mybranch'

master mybranch commit, , , .

$ git lola
* 2182bb2 (HEAD -> mybranch, master) Initial commit

, git lola , ,

$ git log --graph --decorate --pretty=oneline --abbrev-commit --all
* 2182bb2 (HEAD -> mybranch, master) Initial commit

mybranch.

$ touch mybranch ; git add mybranch ; git commit -m 'My branch'
[mybranch 7143aa4] My branch
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 mybranch

.

$ git lola
* 7143aa4 (HEAD -> mybranch) My branch
* 2182bb2 (master) Initial commit

, git .

$ ls -R .git/refs
.git/refs:
heads  tags

.git/refs/heads:
master  mybranch

.git/refs/tags:

, , .

$ cat .git/refs/heads/master .git/refs/heads/mybranch
2182bb2d5a0a7f57d0b74e95d37e208dac41f95b
2182bb2d5a0a7f57d0b74e95d37e208dac41f95b

, git refs , SHA1 . , git lola (2182bb2) cat .

git refs , .

, master zap mybranch

$ git checkout master ; git branch -D mybranch
Switched to branch 'master'
Deleted branch mybranch (was 7143aa4).

, ref

$ ls -R .git/refs
.git/refs:
heads  tags

.git/refs/heads:
master

.git/refs/tags:

.

$ git show --pretty=oneline 7143aa4
7143aa477735382e7a0ed11c9e4b66c1f27583df My branch
diff --git a/mybranch b/mybranch
new file mode 100644
index 0000000..e69de29

mybranch ,

$ git checkout -b mybranch 7143aa4
Switched to a new branch 'mybranch'

$ git branch mybranch 7143aa4

, , . , , git lola

$ git lola
* 7143aa4 (mybranch) My branch
* 2182bb2 (HEAD -> master) Initial commit

, , . . . git reflog git gc.


, SHA1 , , , .

-d -d .

-d
--delete

. HEAD, --track --set-upstream.

-d

--delete --force.

-f
--force

Reset branchname , branchname. -f git . -d ( --delete) & hellip;

+5

- , . git branch -d ( -D) , git log git checkout git branch, .

-D -D , git branch -d (.. ).

+1

Source: https://habr.com/ru/post/1624989/


All Articles