Showing posts with label BitBucket. Show all posts
Showing posts with label BitBucket. 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

Bitbucket Pipeline to zip the source code and upload to S3 bucket

Bitbucket Pipelines is an integrated CI/CD service, built into Bitbucket. It allows us to automatically build, test and even deploy our code, based on a configuration file in our repository. To set up Pipelines we need to create and configure the bitbucket-pipelines.yml file in the root directory of our repository.

Continuous Delivery Vs Continuous Integration

Continuous delivery is a software development methodology where the release process is automated. Every software change is automatically built, tested, and deployed to production. Before the final push to production, a person, an automated test, or a business rule decides when the final push should occur. Although every successful software change can be immediately released to production with continuous delivery, not all changes need to be released right away.

Continuous integration is a software development practice where members of a team use a version control system and frequently integrate their work to the same location, such as a master branch. Each change is built and verified to detect integration errors as quickly as possible. Continuous integration is focused on automatically building and testing code, as compared to continuous delivery, which automates the entire software release process up to production.

Example: Bitbucket Pipeline to zip the source code and upload to S3 bucket.

Following are the steps for creating a pipeline in Bitbucket. In this example, the pipeline will zip the source code and upload the zip file to AWS S3 bucket.

Note: For creating the code pipeline in Bitbucket, you must be either the owner of the repo or you have the necessary permissions on the repo.
  • Open the repository you want to create a pipeline
  • Click on Settings
  • Under Pipelines Section -> Go to Settings


  • Click on Enable Pipelines

  • Go to Repository variables under Pipelines and enter the following
    • Name: AWS_SECRET_ACCESS_KEY    Value: <Your AWS Secret access key>
    • Name: AWS_ACCESS_KEY_ID               Value: <Your AWS Access key>
    • Name: AWS_DEFAULT_REGION            Value: <Your AWS Region>
  • Come back to Pipelines -> Settings and click on “Configure bitbucket-pipelines.yml” button 
  • Scroll down and Select “Other” in the dropdown

  • Paste the following in the code in the editor

  • Replace the S3 bucket path and click on commit button and watch your pipeline build.
Now every time you push your changes to your repository, BitBucket Pipelines will automatically zip the total source code and upload to Amazon S3 bucket.

A Big Note:-  there are a limited number of free build minutes per month depending on the type of account you have.To check your remaining build minutes, Go to Bitbucket settings -> Select Plan details under PLANS AND BILLING

Happy Coding ! 😊

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!😊