How to create and merge a repository on Bitbucket

0
1891
How to create and merge a repository on Bitbucket

These days, software developers are less and less likely to be working in the same country, much less the same room. With remote options, freelance work, and trends in globalization, today’s world offers endless options for IT professionals. But with the varying time zones and work spaces, complications are sure to occur and mistakes are bound to be made.

As version control system admin, we always think/plan to keep the file in single place and easy to sharable across the world/workplace(s) with team members. For that as admin, you should be organized. Here with bitbucket, lets add everything to a repository. Here we can go and create our own one.

Step1: Create a Git repository

Before we start create a repo, here we need to know some facts about repositories;

    • You have access to all files in your local repository, whether you are working on one file or multiple files, doesn’t matter.
    • You can view public repositories without a Bitbucket account if you have the URL for that repository.
    • The repository owner is the only person who can delete the repository. If the repository belongs to a team, an admin can delete the repository.
    • A code project can consist of multiple repositories across multiple accounts but can also be a single repository from a single account.

Prerequisites

Before we start, we have couple of Prerequisites, here is Prerequisites are

  1. GIT installed in local system
  2. You have bitbucket account.

Read More: How to make A successful Git branching model

Bitbucket repository will be the central repository for your files, which means that others can access that repository if you give them permission. After creating a repository, you’ll copy a version to your local system that way you can update it from one repo, then transfer those changes to the other. Let’s see the steps to create your own repository:

  1. From Bitbucket, click the + icon in the global sidebar and select Repository.

Create repoBitbucket displays the Create a new repository page. Take some time to review the dialog’s contents. everything you enter on this page you can later change.

  1. creating new repoEnter Repository name for the Name field. Bitbucket uses this Name in the URL of the repository. For example, if the user foxutech has a repository called terraform, the URL for that repository would be https://bitbucket.org/foxutech/terraform.
  2. For Access level, leave the This is a private repository box checked. A private repository is only visible to you and those you give access to. If this box is unchecked, everyone can see your repository.
  3. Pick Git for the Repository type. Keep in mind that you can’t change the repository type after you click Create repository. And if you need to add some advance options like hipchat and wiki pages, you can click advanced settings to setup this.
  4. Click Create repository. Bitbucket creates your repository and displays its Overview page.

Step2: Explore your new repository

Let’s take some time to explore the created repository.

repo detailsClick + from the global sidebar for common actions for a repository. Click items in the navigation sidebar to see what’s behind each one, including Settings to update repository details and other settings.

bitbucket sourceTips: To view the shortcuts available to navigate these items, press the? key on your keyboard.

Copy your Git repository and add files

To set that up, you want to copy the Bitbucket repository to your system. Git refers to copying a repository as “cloning” it. When you clone a repository, you create a connection between the Bitbucket server (which Git knows as origin) and your local system.

Step 1: Clone the repository to your local system

Open a browser and a terminal window from your desktop. After opening the terminal window, do the following:

  1. Create a directory to contain your repositories and change directory to created one using below command.
# mkdir gitrepos
# cd gitrepos
  1. From Bitbucket, go to your repository.
  2. Click the + icon in the global sidebar and select Clone this repository.

Bitbucket displays a pop-up clone dialog. By default, the clone dialog sets the protocol to HTTPS or SSH, depending on your settings.

  1. clone repositoryCopy and paste the highlighted clone command in your terminal.
  2. Enter your Bitbucket password when the terminal asks for it. If you created an account by linking to Google, use your password for that account.
# git clone https://foxutech@bitbucket.org/foxutech/terraform.git
Initialized empty Git repository in /root/gitrepos/terraform/.git/
warning: You appear to have cloned an empty repository. 
You already knew that your repository was empty right? 
Remember that you have added no source files to it yet.
  1. List the contents of your repos directory and you should see your repo directory in it.

Congratulations! Now we have cloned the repo successfully.

Step 2. Let Add a file to your local repo

With the repository on your local system, it’s time to get to work. You want to start keeping track of all your space station locations. To do so, let’s create a file about all your locations.

  1. Go to your terminal window and navigate to the top level of your local repository.
# cd gitrepos/terraform/
  1. Enter the following line into your terminal window to create a new file with content.
# echo "Let’s create EC2 instances using Terraform" >> Terraform.txt
  1. Get the status of your local repository. The git status command tells you about how your project is progressing in comparison to your Bitbucket repository.

At this point, Git is aware that you created a new file, and you’ll see something like this:

# git status

nothing added to commit but untracked files present (use “git add” to track)

The file is untracked, meaning that Git sees a file not part of a previous commit. Now let’s track the added file(Terraform.txt) by using git add command.

# git add Terraform.txt

The git add command moves changes from the working directory to the Git staging area. The staging area is where you prepare a snapshot of a set of changes before committing them to the official history.

  1. Now check the status of the file.
# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   Terraform.txt
#

Now you can see the new file has been added (staged) and you can commit it when you are ready. The git status command displays the state of the working directory and the staged snapshot.

  1. Issue the git commit command with a commit message, as shown on the next line. The -m indicates that a commit message follows.
# git commit -m "first commit"
[master (root-commit) 058cb38] first commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 Terraform.txt

The git commit takes the staged snapshot and commits it to the project history. Combined with git add, this process defines the basic workflow for all Git users.

Up until this point, everything you have done is on your local system and invisible to your Bitbucket repository until you push those changes.

  • Learn a bit more about Git and remote repositories
    • Git’s ability to communicate with remote repositories (in your case, Bitbucket is the remote repository) is the foundation of every Git-based collaboration workflow.
    • Git’s collaboration model gives every developer their own copy of the repository, complete with its own local history and branch structure. Users typically need to share a series of commits rather than a single changeset. Instead of committing a changeset from a working copy to the central repository, Git lets you share entire branches between repositories.
    • You manage connections with other repositories and publish local history by “pushing” branches to other repositories. You see what others have contributed by “pulling” branches into your local repository.
  1. Go back to your local terminal window and send your committed changes to Bitbucket using git push origin master. This command specifies that you are pushing to the master branch (the branch on Bitbucket) on origin (the Bitbucket server).

You should see something similar to the following response:

# git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 257 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://foxutech@bitbucket.org/foxutech/terraform.git
 * [new branch]      master -> master

Your commits are now on the remote repository (origin).

  1. initial commitGo to your terraform repository on Bitbucket.
  2. If you click Commits in the sidebar, you’ll see a single commit on your repository. Bitbucket combines all the things you just did into that commit and shows it to you. You can see that the Author column shows the value you used when you configured the Git global file ( ~/.gitconfig).
    If you click Source in the sidebar, you’ll see that you have a single source file in your repository, the txt file you just added.

add filePull changes from your Git repo on Bitbucket

Next on your list of space station administrator activities, you need a file with more details about your locations. Since you don’t have many locations at the moment, you are going to add them right from Bitbucket.

Step 1. Create a file in Bitbucket

To add your new locations file, do the following:

  1. From your Terraform repository, click Source to open the source directory. Notice you only have one file, txt , in your directory.
  2. Branch selection:Pick the branch you want to view.
    B. Source page:Click the link to open this page.
    C. New file button: Edit and create a file in Bitbucket.
    D. Source file area: View the directory of files in Bitbucket.
  3. From the Source page, click New file in the top right corner. This button only appears after you have added at least one file to the repository.
  4. Enter Terraform in the filename
  5. Add the following HTML code into the text box:
<html>
This is Foxutech's Terraform webpage
</html>
  1. Click Commit. The Commit message field appears with the message terraform.html created online with Bitbucket
  2. Click Commit under the message field.

You now have a new file in Bitbucket! You are taken to a page with details of the commit, where you can see the change you just made:

new file commitIf you want to see a list of the commits you’ve made so far, click Commits in the sidebar.

Step 2. Pull changes from a remote repository

Now we need to get that new file into your local repository. The process is pretty straight forward, basically just the reverse of the push you used to get the Terraform.txt file into Bitbucket.

To pull the file into your local repository, do the following:

  1. Open your terminal window and navigate to the top level of your local repository.
# cd repos/terraform/
  1. Enter the git pull –all command to pull all the changes from Bitbucket. Enter your Bitbucket password when asked for it. Your terminal should look similar to the following:
# git pull –-all
Fetching origin
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://bitbucket.org/foxutech/terraform
   058cb38..100fc30  master     -> origin/master
Updating 058cb38..100fc30
Fast-forward
 terraform.html |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
 create mode 100644 terraform.html

your remote repository (Bitbucket) into your local repository with a single command.

Now, you can see terraform.html files in the directory.

Merge a file

Branches are most powerful when you’re working on a team. You can work on your own part of a project from your own branch, pull updates from Bitbucket, and then merge all your work into the main branch when it’s ready. A branch represents an independent line of development for your repository. Think of it as a brand-new working directory, staging area, and project history. Before you create any new branches, you automatically start out with the main branch (called master). For a visual example, this diagram shows the master branch and the other branch with a bug fix update.

Step 1. Create a branch

In some requirement.  developer required to work separate branch apart from master, this may be for features, future, dev or any case. This branch isn’t to commit when under planning. When you are ready to make those plans known to all, you can merge the changes into your Bitbucket repository and then delete the no-longer-needed branch.

Branches are just pointers to commits and when you create a branch, all GIT needs to do is create new pointer. It doesn’t create a whole new set of files or folders.

Steps to Create a branch:

  1. Go to your terminal, and change to your repo folder,
# cd gitrepos/terraform/
  1. Create a branch from your terminal window.
# git branch first-branch

This command just creates a branch. And note this command doesn’t switch to that branch.

  1. Checkout the new branch you just created to start using it.
# git checkout first-branch
D       netstat
Switched to branch 'first-branch'

The git checkout command works hand-in-hand with git branch. Because you are creating a branch to work on something new, every time you create a new branch (with git branch), you want to make sure to check it out (with git checkout) if you’re going to use it. Now that you’ve checked out the new branch, your Git workflow looks something like this

  1. Search for the terraform folder on your local system and open it.
  2. Create the html file using your favorite text editor. Add following content;
# vim branch.html
<html>
This is first bitbucket branch
</html>

Save and close the file.

  1. Enter git status in the terminal window. You will see something like this:
# git status
# On branch first-branch
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
##       branch.html
nothing added to commit but untracked files present (use "git add" to track)
  1. Stage your file.
# git add branch.html
  1. Enter the git commit command in the terminal window, as shown with the following:
# git commit branch.html -m "this is branch file"
[first-branch 65e5c0f] this is branch file 
1 files changed, 3 insertions(+), 0 deletions(-) 
create mode 100644 branch.html

Now it’s time to merge the change that you just made back into the master branch.

Step 2. Merge your branch: fast-forward merging

Now it is time to merge the branch to master. For now we have only one branch, use fast-forward branch method to merge the branches.

Your space station is growing, and its time for the opening ceremony of your Mars location. Now that your future plans are becoming a reality, you can merge your first-branch branch into the main branch on your local system. Instead of “actually” merging the branches, all Git has to do to integrate the histories is move (i.e., “fast-forward”) the current branch tip up to the target branch tip. This effectively combines the histories, since all of the commits reachable from the target branch are now available through the current one.

Steps:

  1. Go to your repo directory
# cd gitrepos/terraform/
  1. Check “git status” for get the which branch you have checked out
# git status

On branch first-branch   nothing to commit, working directory clean

  1. Let switch to the master
# git checkout master
Switched to branch 'master'   
Your branch is up-to-date with 'origin/master'.
  1. Merge changes from the first-branch branch into the master It will look something like this:
# git merge first-branch
Updating 100fc30..65e5c0f
Fast-forward 
  branch.html |    3 +++ 
  1 files changed, 3 insertions(+), 0 deletions(-) 
  create mode 100644 branch.html
  1. If you don’t have a plan on using first-branch anymore, you can delete the branch using,
# git branch -d first-branch     
Deleted branch first-branch (was e3b7732).

When you delete first-branch, you can still access the branch from master using a commit id. For example, if you want to undo the changes added from first-branch, use the commit id you just received to go back to that branch.

  1. Enter git status to see the results of your merge, which show that your local repository is one ahead of your remote repository. It will look something like this:
# git status

Next, we need to push all this work back up to Bitbucket, your remote repository.

Step 3. Push your Change

Here’s how to push your change to the remote repository:

  1. From the repository directory in your terminal window, enter git push origin master to push the changes. It will result in something like this:
# git push origin master
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 349 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://foxutech@bitbucket.org/foxutech/terraform.git   
                100fc30..65e5c0f  master -> master
  1. Click Commits and you can see the commit you made on your local system. Notice that the change keeps the same commit id as it had on your local system.
  1. git branch commitClick Branches and notice that the page has no record of the branch either.
  2. Click Source, and then click the html file. You can see the last change to the file has the commit id you just pushed.
  3. Click the file history list to see the changes committed for this file, which will look similar to the following figure.

NO COMMENTS