DEV Community

S I D D A R T H
S I D D A R T H

Posted on • Originally published at Medium on

How to Add Files to GitHub Using Git

GitHub is a powerful platform for version control and collaborative development. In this guide, we will cover the step-by-step process of adding files to GitHub using Git.

Reach me at github @guider23

1. Install Git

Before you begin, make sure you have Git installed on your system.

  • Windows: Download and install Git from git-scm.com.
  • Mac: Install Git using Homebrew:
brew install git
Enter fullscreen mode Exit fullscreen mode
  • Linux: Install Git via package manager:
sudo apt install git # Debian/Ubuntu sudo yum install git # Fedora
Enter fullscreen mode Exit fullscreen mode

Verify the installation by running:

git --version
Enter fullscreen mode Exit fullscreen mode

2. Create a GitHub Repository

  1. Go to GitHub.
  2. Click on New Repository.
  3. Provide a name for your repository.
  4. Choose “Public” or “Private” as per your requirement.
  5. Click Create Repository.

After creating the repository, GitHub will provide a URL (e.g., https://github.com/username/repository.git). Keep this handy.

3. Initialize a Local Repository

If you are working on a new project, navigate to your project folder in the terminal and initialize Git:

cd path/to/your/project

git init
Enter fullscreen mode Exit fullscreen mode

This will create a hidden .git folder, making it a Git repository.

4. Set Up Your Identity

Configure your Git identity (only required once per machine):

git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
Enter fullscreen mode Exit fullscreen mode

5. Add a Remote Repository

Link your local repository to the remote repository on GitHub:

git remote add origin https://github.com/username/repository.git
Enter fullscreen mode Exit fullscreen mode

Verify the remote URL:

git remote -v
Enter fullscreen mode Exit fullscreen mode

6. Add Files to Git

If you have files to upload, use the following command to add them to Git’s tracking system:

git add . # Adds all files in the directory
Enter fullscreen mode Exit fullscreen mode

To add specific files:

git add filename.ext
Enter fullscreen mode Exit fullscreen mode

Check the status of your changes:

git status
Enter fullscreen mode Exit fullscreen mode

7. Commit the Changes

Once files are added, commit them with a message:

git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

This message should be descriptive of the changes made.

8. Push Files to GitHub

Push the committed files to your GitHub repository:

git push -u origin main
Enter fullscreen mode Exit fullscreen mode

If you get an error, ensure your branch is named main (or master) and rename it if necessary:

git branch -M main
Enter fullscreen mode Exit fullscreen mode

Then, push again:

git push -u origin main
Enter fullscreen mode Exit fullscreen mode

9. Handling Errors and Fixes

  • Remote Already Exists :
git remote remove origin 
git remote add origin https://github.com/username/repository.git
Enter fullscreen mode Exit fullscreen mode
  • Updates Rejected (Push Failed):
git pull origin main --rebase git push -u origin main
Enter fullscreen mode Exit fullscreen mode
  • Force Push (Use with Caution):
git push --force
Enter fullscreen mode Exit fullscreen mode

10. Verify on GitHub

Go to your repository on GitHub and check if your files are uploaded. You should see your project files listed.

Conclusion

By following these steps, you can successfully upload files to GitHub using Git. Understanding Git commands helps you manage code efficiently and collaborate effectively. Happy coding! 🚀

Top comments (0)