Git is a powerful version control system that allows developers to collaborate on code. When you make changes to your local Git repository, you can push those changes to GitHub to share with others.
Adding a new file to GitHub using Git Bash is straightforward, but does require a few steps. In this comprehensive guide, we‘ll walk through the entire process.
Prerequisites
Before you can add a file to GitHub, you need:
- Git installed locally
- A GitHub account
- An existing remote GitHub repository to push to
You‘ll also need to have Git Bash installed if you want to perform these steps from the command line.
Step 1: Create or Edit the File Locally
First, navigate in your terminal or file explorer to the local directory tracked by Git where you want to add the file.
You can create a brand new file here using your text editor or IDE. Or, you can edit an existing file that isn‘t yet tracked by Git.
For example, to create a new HTML file called index.html, run:
touch index.html
Open this file in your editor to add some initial code before committing it.
Step 2: Stage the New File
Next, stage the new or edited file to prepare it for committing using git add:
git add index.html
This adds the file to the staging area in Git.
You can also add all untracked files with:
git add .
Step 3: Commit the Change Locally
With the file staged, you need to commit it to record a snapshot permanently in the local repository.
Use the git commit command, including a descriptive commit message:
git commit -m "Add index.html file"
Step 4: Push the Commit to GitHub
Now you can share these changes by pushing to GitHub using git push:
git push origin main
- Replace "main" here with whatever your default branch name is.
This will push the commit containing the new file to GitHub.
Refresh your GitHub repository page, and you should see the new file there!
Recap and Tips
That covers the basic Git workflow to add new files to GitHub:
- Create/edit the file locally
- Stage with
git add - Commit with
git commit - Push with
git push
Here are some additional tips when adding files on GitHub with Git Bash:
- Write clear commit messages summarizing the changes
- Fetch and merge upstream changes before pushing
- Create a new branch first if working on experimental changes
And that‘s it! Now you know how to easily add local files to your GitHub repositories using Git Bash.


