How to delete a branch in git: 1. Use the "git branch --delete dev" command to delete the local branch; 2. Use the "git push origin --delete branch" command to delete the remote branch; 3. Use The "git branch --delete --remotes" command deletes tracking branches.

The operating environment of this article: Windows 10 system, Git version 2.30.0, Dell G3 computer.
How to delete a branch in git
1. Delete local branch
When deleting a branch, we will use
git branch --delete dev
to execute. Sometimes it is replaced by the abbreviation
git branch -d dev
. During use, we found that there is also a way to write git branch -D dev. What is the difference between them?
-d is the abbreviation of --delete. When using --delete to delete a branch, the branch must be completely merged with its upstream branch (to learn about upstream branches, you can click to view the link). If there is no upstream branch, it must be merged with HEAD Complete merge
-D is the abbreviation of --delete --force. This way you can delete the branch without checking the merge status
--force, abbreviated as -f, is to reset the current branch to the initial point (startpoint). If --force is not used, the git branch cannot modify an existing branch.
2. Delete the remote branch
Command git push origin --delete branch, this command will also delete the tracking branch
3. Delete the tracking branch
Through the command git branch --delete --remotes
is as above. Using the command line git push origin --delete branch will delete the remote branch and the tracking branch. There is no need to delete the tracking branch separately, but if you delete the remote branch through the web page , the tracking branch will not be deleted.
After git version 1.6.6, you can delete the tracking branch individually through git fetch origin --prune or its abbreviation git fetch origin -p
Recommended learning: "Git Tutorial"
The above is the detailed content of How to delete a branch in git. For more information, please follow other related articles on the PHP Chinese website!
How do I create a merge commit even if a fast-forward is possible?Jul 21, 2025 am 02:22 AMUse gitmerge --no-ff to force Git to create merge commits, even if you can fast forward. 1. Use the --no-ff parameter to preserve branch history and clarify the source of change; 2. This method is particularly useful during code review or auditing, and is often used in strict branch strategies such as GitFlow; 3. This operation can be automated by configuring alias or scripts, such as gitconfig--globalalias.merge-noff'!gitmerge-no-ff', thereby simplifying the process.
How do I set the upstream branch for my local branch?Jul 21, 2025 am 02:01 AMThe reason for setting up the upstream branch is to let Git know the default pull and push targets, simplifying operations and avoiding confusion. When you create a new local branch, Git will not automatically associate the remote branch, resulting in manually specifying the remote branch every time you execute gitpull or gitpush; by running gitpush-uoriginfeature-branch, you can set up the upstream branch on the first push, where the -u parameter makes Git remember the connection, and then pull or push can be performed without additional parameters; if it has been pushed but has not set up the upstream or needs to change, you can use the gitbranch--set-upstream-to=origin/feature-branch command to adjust it.
How do I add a submodule to my Git repository?Jul 21, 2025 am 12:48 AMThe steps to add submodules to the Git repository are as follows: 1. Use the gitsubmoduleadd[URL][Path] command to add submodules, which will clone the repository and create the .gitmodules file; 2. When cloning the repository of the submodule, you need to run gitsubmoduleupdate-init-recursive to initialize and update the submodule; 3. You can specify the trace of a specific branch through the -b option, and use gitsubmoduleupdate--remote to update to the latest commit of the branch. In addition, submodules have independent .git directories. Modifications need to be submitted within the submodule. Deletion needs to be initially
How do I use Git with a team of developers?Jul 21, 2025 am 12:27 AMThe key to using Git on a team is to follow basic practices and maintain good communication. 1. Determine the shared repository and branch policies, the main branch is used to stabilize the code, and the feature branch is used to develop new functions or fix problems; 2. Communicate before merging or pushing changes, pulling the latest code first to avoid conflicts; 3. Use pull requests (PR) and code review process to ensure that all changes have been discussed and confirmed; 4. Actively handle merge conflicts, pulling updates regularly, and carefully resolving and testing when conflicts are encountered. These practices help to improve teamwork efficiency and reduce errors.
What is git reflog, and how can it be used to recover lost commits?Jul 20, 2025 am 03:51 AMGit'sreflogisalocaljournalthattrackschangestobranchtipsandreferences,helpingrecoverlostcommits.Itrecordsactionslikecommits,resets,rebases,andbranchswitches,allowinguserstorestoreseeminglydeletedwork.Torecoveralostcommit,rungitreflog,findthedesiredcom
How do I revert a specific commit (create a new commit that undoes the changes)?Jul 20, 2025 am 01:41 AMTo revoke a commit that has been pushed to a repository but keeps history, use gitrevert to create a new commit to reverse the changes to the specified commit. 1. Use gitlog--oneline to find the hash value of the target commit; 2. Execute gitrevert or such as gitrevertHEAD~2 to revoke a specific commit; 3. If there is a conflict, manually resolve it and continue with gitrevert--continue, or abort; 4. Submission information can be edited and confirmed; 5. For merged commits, the -m1 parameter needs to be added. This method is safe for shared branches and avoids problems caused by rewriting history.
How do I fetch changes from a remote repository?Jul 20, 2025 am 01:31 AMGitfetchdownloadsupdatesfromaremoterepositorywithoutmergingthem.1.Rungitfetchorigintogetallchangesfromthedefaultremote.2.Usegitfetchoriginmaintofetchaspecificbranch.3.Reviewchangeswithgitlogorigin/mainorgitdiffmainorigin/main.4.Mergemanuallywithgitme
How do I delete a stashed change?Jul 20, 2025 am 12:28 AMTo delete stashes that are no longer needed in Git, use the command to accurately delete specific stashes or clear all. 1. Delete a specific stash: Delete the stash of the specified index through gitstashdropstash@{n}, such as gitstashdropstash@{1} to delete the second entry; 2. Clear all stash: Use gitstashclear to delete all stash at once, but this operation is irreversible; 3. Recover the wrong stash: If you just deleted, try to use gitfsck--unreachable to find danglingcommit and create branch recovery, but it is only valid before the object is garbage collected. Be sure to operate


Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software








