Regularly deleting local references to remote branches is a good practice for maintaining a usable Git repository. Don't slow yourself down by forcing yourself to sift through data you no longer need. Keeping your Git repository tidy may not seem urgent at first, but the more a repository grows, the more important it becomes to prune unnecessary data. That's it! Maintaining your Git repository Next, prune the local reference to the remote branch: $ git remote prune origin Delete the local reference to the remote branch.įirst, list all the branches that you can delete or prune on your local repository: $ git remote prune origin -dry-run If you want to delete all merged local branches except master/main, use the following command. To delete a single branch, use the following command. We can have many local branches that we might want to clean up in one go. In the branch FNB-90, I made 2 add, commits and push them to the remote.Then, check out another branch named FNB-90-CHILD from the FNB-90. To execute deletion, we can pass another argument for the delete command. In this example, test-branch is the name of the topic branch that you deleted in the remote repository.ģ. I check out a branch named FNB-90 from the master. To delete a branch locally, we can use the Git bash application or a Command Window in the root of the Git repository. Checkout the central branch of your repository (such as main or master). If you want more details, we have a separate tutorial on the topic.1. Deleting a remote branchĭeleting a remote git branch is not the same as deleting the local ones. Now, you can see that the test-lhb branch is restored with its contents intact. So, in my case, if I want to restore the test-lhb branch, I would run the following command. Now, use the following syntax to restore the deleted branch. Using the same command (and output) as above, as you can see, SHA1 sum is provided at the end - which is d2ec7fe. ![]() When you delete a branch, Git shows the SHA1 sum of that branch. In case you want to undo the action, please make sure that you have the SHA1 sum of the branch you deleted. Well, let's say you deleted the wrong branch by mistake. So, when I want to forcefully delete a branch, I would run the following command: $ git branch -D test-lhbĪs you can see, the test-lhb branch is now deleted forcefully. When you are faced with this situation, and if you are sure about deleting said branch, use the -D flag instead of -d flag using the same syntax: git branch -D ![]() If you are sure you want to delete it, run 'git branch -D test-lhb'. If the commits in that branch are not pushed to a remote repository.īelow is an example of how it might look when deleting such a branch: $ git branch -d test-lhbĮrror: The branch 'test-lhb' is not fully merged.Either the commits in that branch are not merged with 'master' branch. ![]() To do so, run this from within the specific branch, and then delete the branch name.diff file when done using git rm branch name. So, when I want to delete the test-lhb branch, I would run the following command: $ git branch -d team-lhbīut, when the branch you are deleting has either one of the following conditions met, Git will not allow you to delete that branch with the -d flag. These steps can be very useful, if your goal is set the state of a specific branch to be equal with the master branch. The easiest way to delete a Git branch is using the -d flag along with the git branch command and also specifying the branch name. This way, you get the exact branch names. The one with asterisk (*) is the current branch: $ git branch It shows the following two branches for me. To check which branch you are on and all the available local branches, use this command: git branch Your branch will always be the "master" branch (unless specified otherwise). Learn about deleting them in this one.ĭelete a branch with -d option of git branch command: git branch -dįorce delete a branch (if there are comments to the branch) with option -D: git branch -D In the previous article, you learned about creating branches in Git. Which, in turn helps you work with multiple people at once. ![]() It allows you to have multiple branches to isolate your code from the main branch.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |