Technology Blog Posts by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
469

Introduction

Git is a powerful version control system that helps developers track changes, collaborate efficiently, and manage project versions. It enables teams to work simultaneously on different features, maintain code history, and revert changes when necessary. When working with Bitbucket, Git commands allow you to clone repositories, create branches, commit changes, and push updates seamlessly. Bitbucket also offers features like pull requests, code reviews, and integrations with CI/CD pipelines to enhance development workflows. 

In this blog, we’ll cover the fundamental Git commands required to work with Bitbucket, along with simple definitions to help developers at all levels understand and use them effectively. Whether you're a beginner or an experienced developer, mastering these commands will help you streamline your version control process.

 

Essential Git Commands for Bitbucket

Here’s a list of fundamental Git commands with simple definitions to help you get started with version control in Bitbucket.

Command

Easy Definition

git initInitialize a new Git repository
git clone <repository-url>Copy an existing repository locally
git statusShow modified and untracked files
git branchList all branches in the repository
git branch <branch-name>Create a new branch
git checkout <branch-name>Switch to an existing branch
git checkout -b <branch>Create and switch to a new branch
git add <file>Stage a specific file for commit
git add .Stage all modified files for commit
git commit -m "message"Save changes with a commit message
git push origin <branch>Upload local changes to Bitbucket
git checkout mainSwitch back to the main branch
git merge <branch-name>Merge changes into another branch
git pull origin mainFetch latest changes from the remote repository
git push origin --delete <branch>Delete a remote branch from Bitbucket
rm .gitRemove git repository setup from your project

 

Overview of the Git Workflow in Bitbucket

Here is the step-by-step process of using Git with Bitbucket:

1. Start the Git Repository

🔹Initialize a new Git repository:

  • Definition: Start tracking project changes.
  • If you are starting a new project, initialize Git using the command: git init
  • Step 1: Go to your Terminal -> New Terminal

Myvizhipriya_Thangaraj_2810_0-1743147333746.png

Screenshot 1.1a

Navigate inside your project directory - Use the following command to change the directory: (Refer to the screenshot for your reference)

  • cd.. means Changes to the parent directory
  • cd <project-directory> means Changes to the specified project directory

Myvizhipriya_Thangaraj_2810_0-1743153253983.png

Screenshot 1.1b

PLEASE NOTE: Step 1 is commonly used to reach the respective project directory and start using all Git commands (e.g., git init, git status, git add, git commit, git push, git pull, git branch, git checkout, git clone, git log)

  • Step 2: Inside your project directory: Type git init and press Enter. Here, test_app is my current project directory.

Myvizhipriya_Thangaraj_2810_2-1743147333748.png

Screenshot 1.2

Great! You have successfully initialized your first git repository for your current project

 

🔹Clone an existing repository from Bitbucket:

  • Definition: Clone an existing repository to copy it to your local machine for further development.
  • If you are working in an existing project, Clone the repository from Bitbucket using the command: git clone <repository-url>

Note:

  1. Replace <repository-url>  with the actual URL of the repository you want to clone. The URL can be found on the Bitbucket repository page (e.g., https://bitbucket.org/username/repository-name.git)
  2. You can follow the screenshots below to find out your project repository.

          The following screenshot shows the Bitbucket workspace overview. After logging in to Bitbucket, you will see this page.

Myvizhipriya_Thangaraj_2810_3-1743147333749.png

Screenshot 1.3

Go to the repositories section, then select the corresponding project to clone.

Myvizhipriya_Thangaraj_2810_4-1743147333754.png

Screenshot 1.4

Once you have selected the corresponding project, you will see the following screen. In the top right corner, there is a Clone button. Click that.

Myvizhipriya_Thangaraj_2810_5-1743147333757.png

Screenshot 1.5

This below pop-up will appear, where you can copy the link for further use.

Myvizhipriya_Thangaraj_2810_6-1743147333758.png

Screenshot 1.6

Let's start the execution of the clone command:

  • Step 1: Open your terminal and navigate to the directory where you want to clone the repository.

Myvizhipriya_Thangaraj_2810_7-1743147333759.pngScreenshot 1.7

  • Step 2: Inside your terminal, type git clone <repository-url>  and press Enter

Myvizhipriya_Thangaraj_2810_8-1743147333760.png

Screenshot 1.8

Note: Once the cloning process is completed, you can check the cloned repository in your local directory.

Great! You have successfully cloned the repository from Bitbucket to your local machine.

 

2. Work on Changes

🔹Check the status of changes:

  • Definition: View modified, staged, and untracked files in the repository.

  • If you want to check the status of changes, Use the command: git status
  • Step 1: Open your terminal and navigate to the project directory (Refer Screenshot 1.1a)
  • Step 2: Inside your terminal, type git status and press Enter

Myvizhipriya_Thangaraj_2810_9-1743147333762.png

Screenshot 2.1

Note:

  1. When you run the git status command and it lists untracked files, it means that Git has detected files in your working directory that are not yet tracked by version control. These files are new and haven’t been added to Git’s staging area yet, so git doesn’t know if you want to include them in your commits. The possible other outputs are
    1. Untracked files: Lists the new files (git does not know about yet)

Myvizhipriya_Thangaraj_2810_10-1743147333764.png

Screenshot 2.1a

b. On branch development: This tells you that you're currently on the development branch.

Myvizhipriya_Thangaraj_2810_11-1743147333765.png

Screenshot 2.1b

              c. Nothing to commit, working tree clean: This means no changes have been made to tracked files, and there’s nothing new staged for commit.

Myvizhipriya_Thangaraj_2810_12-1743147333766.png

Screenshot 2.1c

Great! You have successfully checked the status of changes in your repository.

 

🔹Stage a specific file for commit:

  • Definition: Mark a file to be included in the next commit.
  • If you want to stage a specific file before committing, Use the command: git add <file>
  • Step 1: Open your terminal and navigate to the project directory (Refer Screenshot 1.1a)
  • Step 2: Inside your terminal, type git add <file> and press Enter

Myvizhipriya_Thangaraj_2810_13-1743147333766.png

Screenshot 2.2

Note:

  1. Replace <file> with the name of the file you want to stage. Here, View1 is my file name.
  2. I must ensure that I am in the correct directory (the view directory) before trying to execute this individual git add command.
  3. If it's for controller files, then make sure that I am inside the controller directory before executing the git add command

Great! You have successfully staged the specific file for commit.

 

🔹Stage all modified files:

  • Definition: Prepare all changed files to be included in the next commit.
  • If you want to stage all modified files at once, Use the command: git add .
  • Step 1: Open your terminal and navigate to the project directory (Refer Screenshot 1.1a)
  • Step 2: Inside your terminal, type git add . and press Enter

Myvizhipriya_Thangaraj_2810_14-1743147333770.png

Screenshot 2.3a

Myvizhipriya_Thangaraj_2810_15-1743147333773.png

 Screenshot 2.3b

 

Note: Here, . (dot operator) means all the modified files in the current directory.

Great! You have successfully staged all modified files for commit.

 

🔹Commit staged changes with message:                        

  • Definition: Save staged changes to the local repository with a descriptive message.
  • If you want to commit staged changes with a message, Use the command: git commit -m "Message"
  • Step 1: Open your terminal and navigate to the project directory (Refer Screenshot 1.1a)
  • Step 2: Inside your terminal, type git commit -m "Message" and press Enter

Myvizhipriya_Thangaraj_2810_16-1743147333774.png

Screenshot 2.4

Note: Replace "Message" with your descriptive commit message. Here, I used Initial Commit as my message during the commit.

Other way to commit in VS Code: On the left sidebar, click on the Source Control icon (it looks like a branch).  You’ll see a list of Changes that includes modified files, staged changes, and untracked files.

Myvizhipriya_Thangaraj_2810_17-1743147333776.png

Screenshot 2.5

Click on "Yes" to stage all your changes and commit them directly.

Myvizhipriya_Thangaraj_2810_18-1743147333777.png

Screenshot 2.6

Great! You have successfully committed the staged changes with a message.

 

3. Create & Manage Branches

🔹List all branches in repository:

  • Definition: Display all available branches in the repository.
  • If you want to get the list of all branches in an existing project, Use the command: git branch
  • Step 1: Open your terminal and navigate to the project directory (Refer Screenshot 1.1a)
  • Step 2: Inside your terminal, type git branch and press Enter

Note: The terminal will display all the branches available in your repository

Myvizhipriya_Thangaraj_2810_19-1743147333778.png

Screenshot 3.1

Note: If no branches have been created yet (e.g., in a newly initialized Git repository), this command will not output anything. In that case, make a commit and try the command again.

Myvizhipriya_Thangaraj_2810_20-1743147333780.png

Screenshot 3.2

 If the repository was cloned from Git, then the screenshots will look like the one below.

  1. git branch: Lists all local branches in the repository. The current branch will be highlighted with an asterisk (*)
  2. git branch -a: Lists all local and remote branches

Myvizhipriya_Thangaraj_2810_21-1743147333780.png

Screenshot 3.3

Myvizhipriya_Thangaraj_2810_22-1743147333782.png

Screenshot 3.4

Great! You have successfully listed all branches in your repository.

 

🔹Create a new branch:

  • Definition: Create a separate branch for development.
  • If you want to create a new branch in an existing project, Use the command: git branch <branch-name>
  • Step 1: Open your terminal and navigate to the project directory (Refer Screenshot 1.1a)
  • Step 2: Inside your terminal, type git branch <branch-name> and press Enter

Myvizhipriya_Thangaraj_2810_23-1743147333783.png

Screenshot 3.5

Note:

  1. Replace <branch-name> with the desired name of your new branch. Here development is my branch name.
  2. Before creating the branch, make sure that you have done the initial commit to remote repository
  3. In the screenshot below, you can see how the commands work both before and after creating the branch.

Myvizhipriya_Thangaraj_2810_24-1743147333786.png

Screenshot 3.6

To switch to the newly created branch, Use the command: git checkout <branch-name>

  • Step 3: Inside your terminal, type git checkout <branch-name> and press Enter

Myvizhipriya_Thangaraj_2810_25-1743147333787.png

Screenshot 3.7

Great! You have successfully created and switched to a new branch in your repository.

 

🔹Switch to a new/another branch:

  • Definition: Move between different repository branches.
  • If you want to switch to a new or existing branch in an existing project, Use the command: git checkout <branch-name> 
  • Step 1: Open your terminal and navigate to the project directory (Refer Screenshot 1.1a)
  • Step 2: Inside your terminal, type git checkout <branch-name> and press Enter

Myvizhipriya_Thangaraj_2810_26-1743147333788.png

Screenshot 3.8

Note: Replace <branch-name> with the name of the branch you want to switch to. Here development is my branch name.

Great! You have successfully switched to the new or existing branch in your repository.

 

🔹(Alternative) Create & switch in one step:

  • Definition: Create and change branch together.
  • If you want to create and switch to a new branch in an existing project, Use the command: git checkout -b <branch-name>
  • Step 1: Open your terminal and navigate to the project directory (Refer Screenshot 1.1a)
  • Step 2: Inside your terminal, type git checkout -b <branch-name> and press Enter

Myvizhipriya_Thangaraj_2810_58-1743148787782.png

Screenshot 3.9

Note: Replace <branch-name> with the desired name of your new branch. Here main is my current branch name.

Great! You have successfully created and switched to a new branch in your repository.

 

4. Push to Bitbucket

🔹Check the current branch:

  • Definition: Verify which branch is currently active.
  • If you want to check the branch you are working on, Use the command: git branch
  • Step 1: Open your terminal and navigate to the project directory (Refer Screenshot 1.1a)
  • Step 2: Inside your terminal, type git branch and press Enter

Myvizhipriya_Thangaraj_2810_59-1743148846960.png

Screenshot 4.1

Note: The current branch will be highlighted with an asterisk (*)

Great! You have successfully checked the current branch you are working on.

 

🔹Push changes to remote repository:

  • Definition: Upload local commits to the Bitbucket repository.
  • If you want to push changes to a specific branch in the remote repository, Use the command: git push origin <branch-name>
  • Step 1: Open your terminal and navigate to the project directory (Refer Screenshot 1.1a)
  • Step 2: Inside your terminal, type git push origin <branch-name> and press Enter. If this is your initial push, use the command git remote add origin <repository-link> first, and then proceed with git push. (Refer to the following screenshot for your reference)
    • To set or verify the remote URL, run the following command: 
      • git remote set-url origin <repository-link>
    • After updating, you can verify the remote URL with:
      • git remote -v
    • Once the URL is set correctly, you can proceed with pushing your changes, one branch at a time (e.g., the development branch, then the main branch, and so on)

Myvizhipriya_Thangaraj_2810_60-1743148917170.png

Screenshot 4.2

Here, You can check the current remote verbose (detailed information about the remote URL)

Myvizhipriya_Thangaraj_2810_61-1743148965002.png

Screenshot 4.3

Now, execute the push command as shown below:

Myvizhipriya_Thangaraj_2810_62-1743149006425.png

Screenshot 4.4

Note: Replace <branch-name> with name of the branch you want to push your changes to. Here, development is my current branch to push your changes.

Other case, If the remote isn't configured or is incorrect, then you will get the following error

 
4.5.png

Screenshot 4.5

Note:

  1. Replace <branch-name> with name of the branch you want to push your changes to. Here, main is my current branch to push your changes.
  2. Since no remote origin repository has been created for the project yet, you cannot push from the local repository to the remote repository. Once the remote repository (origin) is set up, you can try the same command again.

If the remote isn't configured or is incorrect, you need to add or update the remote URL. To add a remote repository (if you haven't done so already), run the following command:

         git remote add origin <repository-url>

Then, to push the changes to the remote repository, run:

        git push -u origin <branch-name>

Note: Replace <repository-url> with the URL of your remote repository (e.g., https://github.com/username/repository.git for GitHub, or https://bitbucket.org/username/repository.git for Bitbucket).

As shown in (Screenshot 4.2), you may be unsure of how to obtain the <repository-url>. To do this, go to Bitbucket, click on CreateRepository

Bitbucket 5.pngScreenshot 4.6

              The following page will open. Enter the repository name (test_app_ui), set the access level to either private or public, choose the default branch name as master, and click Create Repository.

(Note: The workspace has already been created by the Bitbucket administrator, and the project will be selected from the dropdown menu.)

Bitbucket 6.pngScreenshot 4.7

After this, you can see the Bitbucket repository under your workspace/project name.

Bitbucket 7.png

Screenshot 4.8

As usual, click on the Clone button at the top right, and you will see the repository link.

Note: Avoid using git clone and .git. Only use the central part of the repository URL. Refer to the screenshot before running the command.

Bitbucket 8.pngScreenshot 4.9

Great! You have successfully pushed your changes to the remote repository

 

5. Create a Pull Request (PR) on Bitbucket:

  • Definition: Request a code review and merge changes into the target branch.
  • If you want to create a Pull Request (PR) for review and merging, go to the Bitbucket UI and follow the steps (Step 6 & Step 7) to submit your request.

Note: Add reviewers and request feedback.

  • Step 1: Go to your Bitbucket repository in the browser.
  • Step 2: After pushing your changes to a branch, navigate to the "Pull requests" tab.
    Myvizhipriya_Thangaraj_2810_37-1743147333805.png

Screenshot 5.1

  • Step 3: Click the "Create pull request" button.

Myvizhipriya_Thangaraj_2810_38-1743147333808.png

Screenshot 5.2

  • Step 4: Select the source branch (your working branch) and the destination branch (usually main or development).

Myvizhipriya_Thangaraj_2810_39-1743147333809.png

Screenshot 5.3

  • Step 5: Add a title and description for your pull request.

Myvizhipriya_Thangaraj_2810_40-1743147333810.png

Screenshot 5.4

 Step 6: Add reviewers who should review your changes.

Reviewers.pngScreenshot 5.5

  • Step 7: Click "Create pull request" to submit your request for review and merging.

Myvizhipriya_Thangaraj_2810_42-1743147333815.png

Screenshot 5.6

Note: Replace <branch-name> with the desired name of your new branch

Great! You have successfully created a Pull Request (PR) on Bitbucket. Reviewers will now be able to provide feedback and approve the merge.

 

6. Code Review & Merging

🔹Switch to the main branch:

  • Definition: Move back to the main branch in the repository.
  • If you want to switch to the main branch, Use the command: git checkout main
  • Step 1: Open your terminal and navigate to the project directory (Refer Screenshot 1.1a)
  • Step 2: Inside your terminal, type git checkout main and press Enter

Myvizhipriya_Thangaraj_2810_43-1743147333816.png

Screenshot 6.1

Great! You have successfully switched to the main branch in your repository.

 

🔹Merge feature branch into main:

  • Definition: Combine changes from one branch into another.
  • If you want to merge a feature branch into the main branch, Use the command: git merge <branch-name>
  • Step 1: Open your terminal and navigate to the project directory (Refer Screenshot 1.1a)
  • Step 2:  Then, merge the feature branch into the main branch by typing: git merge <branch-name> and press Enter

Myvizhipriya_Thangaraj_2810_44-1743147333817.png

Screenshot 6.2

Note: Replace <branch-name> with the name of the feature branch you want to merge. Here development is another branch

Great! You have successfully merged the feature branch into the main branch.

 

🔹Push updated main branch to Bitbucket:

  • Definition: Upload the latest changes from the main branch to the remote repository.
  • If you want to push the updated main branch to Bitbucket, Use the command: git push origin main
  • Step 1: Open your terminal and navigate to the project directory (Refer Screenshot 1.1a)
  • Step 2: Inside your terminal, type git push origin main and press Enter

Myvizhipriya_Thangaraj_2810_45-1743147333821.png

Screenshot 6.3

Great! You have successfully pushed the updated main branch to Bitbucket.

 

7. Cleanup (After Merge)

🔹Delete a merged local branch:

  • Definition: Remove a merged (local outdated branch) branch from your local repository.
  • If you want to delete a feature branch locally after merging, Use the command: git branch -d <branch-name>
  • Step 1: Open your terminal and navigate to the project directory (Refer Screenshot 1.1a)
  • Step 2: Inside your terminal, type git branch -d <branch-name> and press Enter

Myvizhipriya_Thangaraj_2810_46-1743147333822.png

Screenshot 7.1

Note: Replace <branch-name> with the name of the branch you want to delete. In this case, development is the branch that I want to delete.

⚠ Warning: Deleting a local branch is irreversible. Ensure it has been merged and is no longer needed before deleting.

Great! You have successfully deleted the merged local branch from your repository.

 

🔹Delete a Remote Branch from Bitbucket:

  • Definition: Remove a branch from the remote Bitbucket repository.
  • If you want to delete a branch from Bitbucket, Use the command: git push origin --delete <branch-name>
  • Step 1: Open your terminal and navigate to the project directory (Refer Screenshot 1.1a)
  • Step 2: Inside your terminal, type git push origin --delete <branch-name> and press Enter

Myvizhipriya_Thangaraj_2810_47-1743147333823.png

Screenshot 7.2

Note: Replace <branch-name> with the name of the branch you want to delete

⚠ Warning: Deleting a branch from Bitbucket is permanent and cannot be undone. Ensure the branch is no longer needed before proceeding.

Great! You have successfully deleted the remote branch from Bitbucket.

 

8. Remove Git Initialization (Un-initialize a Git Repository):

🔹Remove Git Initialization:

  • Definition: Remove the Git tracking from a project.
  • If you want to remove the Git initialization to delete the .git directory, Use the command: rm .git
  • Step 1: Open your terminal and navigate to the project directory (Refer Screenshot 1.1a)
  • Step 2: Inside your terminal, type rm .git and press Enter or press Y (If you're asked to confirm, just press Y (the default option is Yes when you hit Enter)

Myvizhipriya_Thangaraj_2810_48-1743147333825.png

Screenshot 8.1

Note: Be cautious when using this command, as it permanently deletes all Git tracking and history for the project. Once executed, you cannot recover the Git information unless you have a backup.

Great! You have successfully uninitialized the Git repository for your project.

 

Pro Tip: How to Effortlessly Publish Your Branch!

  • Publish Your Branch to GitHub

              If you want to publish the branch, then click the "Publish Branch" button in the source control panel (you'll see it in the Source Control view). If you're not already logged into GitHub, Visual Studio Code will prompt you to sign in - A popup will appear asking you to allow access to your GitHub account.

Myvizhipriya_Thangaraj_2810_49-1743147333828.png

Publish Branch - Screenshot 1

Click Allow, and it will redirect you to the GitHub login page.

Myvizhipriya_Thangaraj_2810_50-1743147333829.png

Publish Branch - Screenshot 2

            Enter your GitHub credentials or authorize via GitHub if you've already set up the connection. After successful authentication, the branch will be pushed to GitHub.  

 

Publish 2.png

Publish Branch - Screenshot 3

  • Publish Your Branch to Bitbucket

             If you want to this option may appear as something like "Sign in to Bitbucket" or "Connect to Bitbucket" on the left sidebar or in the application’s menu.

Myvizhipriya_Thangaraj_2810_52-1743147333834.png

Publish Branch - Screenshot 4

After you click the sign-in option, a page or popup should appear asking you to log in to Bitbucket Cloud. Click on "LOGON TO BITBUCKET CLOUD" (or something similar).

Myvizhipriya_Thangaraj_2810_53-1743147333839.png

Publish Branch - Screenshot 5

Click Allow, and it will redirect you to the GitHub login page.

Myvizhipriya_Thangaraj_2810_54-1743147333840.png

Publish Branch - Screenshot 6

            If you are already logged in, the connection will be established automatically, and you should see your Bitbucket account listed in your Git GUI.

Myvizhipriya_Thangaraj_2810_55-1743147333843.png

Publish Branch - Screenshot 7

             If you aren’t logged in already, you will be prompted to enter your Bitbucket credentials (username and password) or use an OAuth token (if Bitbucket 2FA is enabled). After entering the credentials, try clicking Log In or Authorize to connect the account.

             After logging in and providing your credentials, your Bitbucket account will be linked to the VS Code You can then push, pull, and manage repositories directly from the Git GUI.

 

Workflow Summary in Order

A structured Git workflow ensures smooth collaboration and efficient version control. Below is the recommended workflow to follow when working with Git and Bitbucket.

  • Initialize/Clone repository – Start working on project
  • Create a branch – Work separately on new features
  • Switch to the branch – Move to development branch
  • Modify & track changes – Edit and check files
  • Stage & commit changes – Save modifications with messages
  • Push changes to Bitbucket – Upload updates to repository
  • Open a Pull Request (PR) – Request for code review
  • Code review & approval – Get feedback and approve changes
  • Merge branch into main – Combine changes into main
  • Delete the merged branch – Cleanup after merging

Myvizhipriya_Thangaraj_2810_56-1743147333846.png

Workflow Diagram 1.1

Myvizhipriya_Thangaraj_2810_57-1743147333849.png

Workflow Diagram 1.2

 

Key Recommendations for Using Git and Bitbucket

When using Git and Bitbucket:
✔ Always write clear commit messages.
✔ Follow your team's branching strategy.
✔ Keep your local repository updated.
✔ Avoid committing sensitive information (API keys, passwords).

Thank you for taking the time to read this blog! I hope this guide on Git commands for Bitbucket helps you streamline your workflow and enhances your version control knowledge.

If you found this helpful, feel free to share your thoughts, feedback, or questions in the comments! Let's keep learning and growing together. 🚀

Stay tuned for Part 2, where we’ll explore more advanced Git commands!

Happy coding! 💻 😊

This is my first blog, so experts, please feel free to correct me if any information is inaccurate. Thanks in advance 😊

Labels in this area