Showing posts with label Commits. Show all posts
Showing posts with label Commits. Show all posts

Git: Delete commits from a branch

Using Revert feature in Git will keeps the older commit in the branch. It just revert the changes what we have done in a commit. The Revert also will shown as a separate commit in the branch.
But if you want to remove a commit from a branch permanently, you need to use git reset command as below
git reset --hard HEAD~1
Here HEAD~1 means, the commit before the head.
If you want to reset up to a particular commit, use
git reset --hard 
If you already pushed the commits to the repo, you need to use as 
git push origin HEAD --force
This will reset your branch up to the given commit. 
Note: Be careful before doing the above as these will remove the changes in the working directory. Better to keep a backup for the working folder.

Example

The following process will show how it works
1) I have a repo named "test" in my bit bucket. 




It has two branches "master" and "my-branch". "master" branch contains 2 commits and "my-branch" contains 8 commits (along with two master branch commits). 
2) Now I want to reset my-branch to the commit "666f88d" ("fourth.html added") commit. So I just reset my-branch with the command
git reset --hard 666f88d

3) It will reset the changes in your local folder only. Push the changes to repo using
git push origin HEAD --force

4) Now those commits will also be removed from the bitbucket branch


Note: Actually, the commits will not remove from the repo. They just removed from the tree. If you have the direct link to the deleted commit and if you open that link, you can sees the changes you made on that commit

Create branch from a particular commit: BitBucket

Sometimes we may need to create a branch from a particular commit. Many people know how to create a branch from another branch. Some people may know how to create new branch from a particular commit using Git. But in Bitbucket UI, there is not direct provision for creating new branch from a particular commit of another branch.
For example if a branch contains 10 commits and we require to create a branch up to 5th commit of that branch. We don't want remaining 5 commits in the new branch. In this case many people will create new branch from the old branch and revert the commits which are not required. Instead of thise large process we can  branch from a particular commit of another branch in BitBucket UI using Tags concept. Following steps will illustrate it.

Step1: 

Open the commit and create a tag for it
Suppose I have 3 commits and I want to create repository from the second commit

Open the commit and add tag (click on the + symbol after Tag icon on the right side)

Step2:

Once added the tag, Click on Branches -> Create Branch -> Select the tag in the "From Branch" column.


















Note: Sometimes the tag will not be displayed in the below list if the Branches and Tags list is very lengthy. In that case type the first characters of the tag name will filter it.

Give the name of the new branch and click on "Create" button. It will create new branch and the new branch will have the commits up to the selected commit only.

Happy Coding!😊