Git tutorial with Bitbucket and Sourcetree (Git & Dev Tools)
Learn Git tutorial with Bitbucket and Sourcetree (Git & Dev Tools) step by step with clear examples and exercises.
Why This Matters
Learning how to use Bitbucket and Sourcetree effectively is essential for any developer looking to streamline their workflow, collaborate with others, and stay organized in a Git-based development environment. Understanding these tools will help you:
- Collaborate efficiently with other developers on the same project without overwriting each other's changes.
- Keep track of changes and easily rollback to previous versions if needed.
- Debug common issues that may arise during development.
- Prepare for job interviews by demonstrating proficiency in essential developer tools.
- Gain a better understanding of Git and its core concepts, making you more versatile as a developer.
- Work seamlessly with cloud-based repositories, which are becoming increasingly popular in modern development workflows.
- use additional features offered by Bitbucket, such as pipelines and Bitbucket Pipelines, to automate tasks like testing, linting, and deployment.
Prerequisites
To follow this tutorial, you should have a basic understanding of:
- The concept of version control systems (VCS) and Git.
- Basic command-line navigation (Windows, Linux, or macOS).
- Familiarity with text editors like Visual Studio Code, Sublime Text, or Atom.
- A working internet connection to access Bitbucket and Sourcetree.
- Knowledge of a programming language such as C++, Python, JavaScript, or Ruby, which are commonly used for developing projects in Git repositories.
- Familiarity with the file structure and build process of your chosen programming language.
- Basic understanding of common development practices like writing tests, linting code, and deploying applications.
Core Concept
Setting up Bitbucket and Sourcetree
- Sign up for a free Bitbucket account at bitbucket.org.
- Download and install Sourcetree from sourcetreeapp.com.
- Launch Sourcetree, click on "Account," then "Add Account" to connect your Bitbucket account.
- In the repository browser, click on "Clone," enter the URL of a public or private Bitbucket repository, and choose a local directory to store the project files.
- Click "Clone" to download the repository to your local machine.
- If you want to create a new repository in Bitbucket:
a. Log into Bitbucket.
b. Click on "Create" in the top navigation bar, then select "Repository."
c. Choose the desired options (e.g., repository name, visibility, and initial files).
d. Click "Create Repository."
- If you want to create a new project locally:
a. Create a new directory for the project on your local machine (e.g., my_project).
b. Initialize a Git repository inside the project directory by running git init in the terminal.
c. Add a file with some initial code or content.
d. Stage and commit the new file by running git add followed by git commit -m "Initial commit" in the terminal.
- Push your local repository to Bitbucket:
a. In Sourcetree, select your newly created local repository.
b. Click on the "Push" button in the upper right corner.
c. Choose the remote Bitbucket repository you want to push to and click "OK."
d. If prompted, enter your Bitbucket username and password or use SSH keys if you have set them up.
Basic Git commands in Sourcetree
- Commit: Create a new commit with changes by clicking on the "Commit" button in the lower left corner, writing a commit message, and selecting the files to be committed.
- Push: Send your local commits to the remote Bitbucket repository by clicking on the "Push" button in the upper right corner.
- Pull: Fetch and merge any changes from the remote Bitbucket repository into your local repository by clicking on the "Pull" button in the upper right corner.
- Branching and Merging: Create a new branch, switch between branches, and merge branches using the respective buttons in the lower left corner.
- Stashing: Temporarily save your changes to a stash if you need to switch to another branch or task without committing.
- Rebasing: Interactively rebase your local commits onto another branch using the "Rebase" option in the context menu (right-click on a commit).
- Merging: Merge branches by selecting the destination branch, clicking on the "Merge" button in the lower left corner, and choosing the source branch to merge from.
- Cherry-picking: Cherry-pick individual commits from another branch using the "Cherry-pick" option in the context menu (right-click on a commit).
- Tagging: Create tags to mark specific points in your repository's history by right-clicking on a commit and selecting "Create Tag."
- Fetching: Update your local repository with any new commits, branches, or tags from the remote Bitbucket repository using the "Fetch" option in the context menu (right-click on the remote repository).
Common Mistakes
- Forgetting to commit changes: Always remember to commit your changes before pushing them to the remote repository to avoid losing work due to unexpected issues or interruptions.
- Ignoring merge conflicts: When merging branches, resolve any merge conflicts that may arise by manually editing the conflicting files and committing the resolved version.
- Not using descriptive commit messages: Writing clear and concise commit messages helps other developers understand the purpose of each commit and makes it easier to track changes over time.
- Misusing branches: Using too many small branches or merging branches prematurely can lead to confusion and make it difficult to manage the project effectively.
- Not stashing changes before switching branches: If you switch branches without stashing your changes, you may accidentally lose work if the new branch has unrelated changes that conflict with yours.
- Not pulling before merging: Always pull any changes from the remote repository before merging branches to ensure that you have the latest version of the code and avoid merge conflicts.
- Merging without resolving conflicts: If a merge results in conflicts, resolve them manually by editing the conflicting files and committing the resolved version.
- Not using descriptive branch names: Using clear and concise names that describe the purpose of the branch (e.g.,
feature/new_login_page,bugfix/login_error) helps other developers understand the purpose of each branch and makes it easier to manage the project effectively. - Merging master into feature branches: Instead, merge feature branches into
masterto keep the main line of development clean and up-to-date. - Not using feature branches for new features or bug fixes: Using separate feature branches allows you to isolate changes and test them independently before merging them into the mainline.
Worked Example
In this example, we will create a simple "Hello, World!" project using Bitbucket and Sourcetree:
- Create a new directory for the project on your local machine (e.g.,
hello_world). - Initialize a Git repository inside the project directory by running
git initin the terminal. - Add a file named
main.cppwith the following content:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
- Stage and commit the new file by running
git add main.cppfollowed bygit commit -m "Initial commit"in the terminal. - Create a new repository on Bitbucket with the same name as your local project directory (e.g.,
hello_world) and make it public or private depending on your needs. - In Sourcetree, click on "Clone," enter the URL of your newly created Bitbucket repository, and choose the local project directory as the target location.
- Click "Clone" to download the repository to your local machine.
- Switch to the
masterbranch in Sourcetree and pull any changes from the remote repository by clicking on the "Pull" button in the upper right corner. - Make some changes to the
main.cppfile (e.g., add a comment or change the message) and commit them using Sourcetree's commit interface. - Push your local commits to the remote Bitbucket repository by clicking on the "Push" button in the upper right corner.
- Create a new branch named
feature/greeting_change:
a. Right-click on the master branch and select "Create Branch."
b. Enter the name feature/greeting_change and click "OK."
- Switch to the newly created branch by clicking on it in the repository browser.
- Modify the
main.cppfile again (e.g., change the message). - Commit and push the changes to the remote Bitbucket repository.
- Merge the
feature/greeting_changebranch into themasterbranch:
a. Right-click on the feature/greeting_change branch and select "Merge."
b. Choose the destination branch (master) and click "OK."
- Pull any changes from the remote repository to ensure that your local repository is up-to-date.
Common Mistakes
- Not pulling before merging: Always pull any changes from the remote repository before merging branches to ensure that you have the latest version of the code and avoid merge conflicts.
- Merging without resolving conflicts: If a merge results in conflicts, resolve them manually by editing the conflicting files and committing the resolved version.
- Not using descriptive branch names: Using clear and concise names that describe the purpose of the branch (e.g.,
feature/new_login_page,bugfix/login_error) helps other developers understand the purpose of each branch and makes it easier to manage the project effectively. - Merging master into feature branches: Instead, merge feature branches into
masterto keep the main line of development clean and up-to-date. - Not using feature branches for new features or bug fixes: Using separate feature branches allows you to isolate changes and test them independently before merging them into the mainline.
- Ignoring merge conflicts: When merging branches, resolve any merge conflicts that may arise by manually editing the conflicting files and committing the resolved version.
- Not using descriptive commit messages: Writing clear and concise commit messages helps other developers understand the purpose of each commit and makes it easier to track changes over time.
- Misusing branches: Using too many small branches or merging branches prematurely can lead to confusion and make it difficult to manage the project effectively.
- Not stashing changes before switching branches: If you switch branches without stashing your changes, you may accidentally lose work if the new branch has unrelated changes that conflict with yours.
Practice Questions
- How do you create a new branch in Sourcetree?
- What is the purpose of stashing changes in Git?
- How would you resolve merge conflicts in a file using Sourcetree?
- What is the difference between
git pullandgit fetch? - How do you revert to a specific commit in your local repository using Sourcetree?
- How can you view the history of commits in your local repository using Sourcetree?
- What is the purpose of tags in Git, and how would you create one using Sourcetree?
- How do you merge branches in Sourcetree, and what happens if there are merge conflicts?
- How can you view the differences between files before committing them in Sourcetree?
- What is rebasing in Git, and how would you perform a rebase using Sourcetree?
FAQ
- Do I need to use Bitbucket for Git repositories if I already have a GitHub account?
While both services offer similar functionality, there are differences between them, such as pricing plans and additional features offered by Bitbucket (like pipelines and Bitbucket Pipelines). You can use either service depending on your needs.
- Can I push changes to a private Bitbucket repository without using SSH keys?
Yes, you can use HTTPS instead of SSH for pushing changes to a private Bitbucket repository, but it is less secure and may require additional permissions from the repository owner.
- What are some best practices for naming branches in Git?
Use clear and concise names that describe the purpose of the branch (e.g., feature/new_login_page, bugfix/login_error). Avoid using short or ambiguous names like fix1 or feat2.
- How do I handle large files in Git repositories?
Large files can cause problems when working with Git, such as slow performance and increased storage requirements. To manage large files, consider splitting them into smaller pieces, compressing them using tools like Gzip, or storing them outside the repository and referencing them instead.
- What is a Git hook