Back to Git & Dev Tools
2025-12-196 min read

Step 2. Add a file to your local repository and put it on Bitbucket (Git & Dev Tools)

Learn Step 2. Add a file to your local repository and put it on Bitbucket (Git & Dev Tools) step by step with clear examples and exercises.

Title: Adding a File to Your Local Repository and Pushing it to Bitbucket (Git & Dev Tools)

Why This Matters

In software development, version control systems like Git help manage changes to your codebase efficiently. By learning how to add files to your local repository and push them to Bitbucket, you'll be able to collaborate effectively with others, track changes, and easily revert back to previous versions of your code. This skill is essential for real-world development projects, interviews, and bug fixing.

Importance in the Real World

  • Collaboration: Work seamlessly with team members on a shared codebase.
  • Tracking Changes: Keep a record of every change made to the project, making it easier to identify issues and roll back if necessary.
  • Efficiency: Save time by avoiding manual file management and redundant work.

Prerequisites

Before diving into adding files to your local repository and pushing them to Bitbucket, it's essential to have a basic understanding of the following:

  • The command line (terminal or Git Bash)
  • Creating directories, navigating through them, and creating files
  • Basic Git commands such as git init, git status, git add, and git commit

Core Concept

  1. Initialize a local repository: Navigate to the directory containing your project files using the command line. Then, initialize a Git repository with the following command:
git init
  1. Stage (add) files: To stage or add files to the staging area, use the git add command followed by the file name(s). For example, to add a new file named example.txt, run:
git add example.txt
  1. Create and Stage New Files: If you create a new file after initializing your repository, Git will not automatically recognize it. To stage the new file, use the following command:
touch new_file.txt # Create a new file
git add new_file.txt # Stage the new file
  1. Commit changes: After staging your files, you'll need to commit them with a descriptive message using the git commit command:
git commit -m "Initial commit"
  1. Create a New Branch (Optional): If you want to work on a new feature or fix without affecting the main branch, create a new branch and switch to it:
git checkout -b new_branch
  1. Make Changes: Make your desired changes to the files in the new branch.
  1. Stage and Commit Changes: Stage and commit changes as usual.
  1. Merge Branch (Optional): Once you're satisfied with your changes, merge the new branch back into the main branch:
git checkout master # Switch to the main branch
git merge new_branch # Merge the new branch into the main branch
  1. Connect to Bitbucket: To connect your local repository to Bitbucket, first create a new repository on Bitbucket or use an existing one. Then, add the remote repository URL to your local repository using the following command:
git remote add origin <Bitbucket_repository_URL>
  1. Push changes: Finally, push your committed changes to the remote (Bitbucket) repository with the git push command:
git push -u origin master

Common Mistakes and Best Practices

1. Forgetting to stage files before committing

To avoid this mistake, always use git add before git commit.

2. Committing unstaged changes

Before committing, make sure all necessary files are staged using git add.

3. Failing to specify the remote repository URL when pushing

Always include the remote repository URL when pushing changes with the git push command:

git push -u origin master

4. Pushing a new branch directly without merging it into the main branch

If you create a new branch and push it to Bitbucket without merging it into the main branch, others won't be able to see your changes until they merge your branch themselves. To avoid this, always merge your branches into the main branch before pushing.

5. Ignoring large files in Git repositories

When dealing with large files, consider using Git LFS (Large File Storage) to manage these files more efficiently. Git LFS stores pointers to large files on a remote server and fetches them when needed, reducing the size of your Git repository and improving performance.

Worked Example

Assuming you have a project directory named my_project containing an example.txt file, follow these steps to add the file to your local Git repository and push it to Bitbucket:

  1. Navigate to the my_project directory using the command line:
cd my_project
  1. Initialize a Git repository:
git init
  1. Create a new file called new_file.txt and stage it:
touch new_file.txt # Create a new file
git add new_file.txt # Stage the new file
  1. Make changes to both example.txt and new_file.txt.
  1. Stage, commit, and merge the changes back into the main branch:

Stage and commit the changes in the local repository

git add .

git commit -m "Initial commit"

Create a new branch called feature-branch and switch to it

git checkout -b feature-branch

Make additional changes in the feature-branch

Stage, commit, and merge the changes back into the main branch

git add .

git commit -m "Changes in the feature-branch"

git checkout master

git merge feature-branch


6. Add the Bitbucket repository URL as a remote:

Replace `` and `` with your Bitbucket username and the name of your repository, respectively:

git remote add origin https://bitbucket.org//.git


7. Push the changes to the remote (Bitbucket) repository:

git push -u origin master

Common Mistakes

6. Not using Git LFS for large files

When dealing with large files, consider installing and configuring Git LFS to improve performance and reduce the size of your Git repository:

  1. Install Git LFS:
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.sh | bash
  1. Configure Git LFS:
git lfs install
  1. Add large files to Git LFS:
git lfs track "*.jpg" # For example, to track .jpg images
  1. Commit and push the changes as usual.

Practice Questions

  1. You have a project directory named my_project. Initialize a Git repository, stage and commit an existing file called example.txt, and push the changes to Bitbucket.
  2. You've made some changes to your local repository, but you forgot to add and commit them before pushing. What should you do?
  3. Your teammate has pushed changes to the remote repository that conflict with your local changes. How can you resolve this issue?
  4. You created a new branch called feature-branch and made some changes. How would you merge these changes back into the main branch?
  5. What is Git LFS, and how does it help when dealing with large files in Git repositories?
  6. What are some best practices for using Git LFS to manage large files in your repository?

FAQ

Q: Can I push changes directly from a single file instead of the entire directory?

A: Yes, you can use the git add command followed by the specific file name to stage and commit only that file. Then, push it to the remote repository using the git push command.

Q: How do I handle large files when pushing to Bitbucket?

A: To avoid issues with large files, consider using Git LFS (Large File Storage) or compressing your files before committing and pushing them to the remote repository.

Q: What is Git LFS, and how does it help when dealing with large files in Git repositories?

A: Git Large File Storage (LFS) is a Git extension that addresses the issue of managing large binary files such as images, videos, or databases within Git repositories. Instead of storing these files directly in the repository, Git LFS stores pointers to the files on a remote server and fetches them when needed. This helps reduce the size of your Git repository and speeds up the push and pull operations.

Q: How do I install and configure Git LFS?

A: To install Git LFS, run the following command:

curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.sh | bash

Then, configure Git LFS with this command:

git lfs install

Q: How do I add large files to Git LFS?

A: To track large files using Git LFS, use the following command:

git lfs track "*.jpg" # For example, to track .jpg images
Step 2. Add a file to your local repository and put it on Bitbucket (Git & Dev Tools) | Git & Dev Tools | XQA Learn