Uploading Projects to GitHub: A Step-by-Step Guide
Git is a free, open-source distributed version control system designed to track changes in files across projects. It allows developers to stage changes and commit them when ready, enabling rollback to previous states. GitHub is a cloud-based platform built on Git, providing hosting for repositories. It serves as a hub for open-source collaboration and offers paid private repositories for proprietary code management.
Installing Git
Download Git from the official website: https://git-scm.com/. Follow the installation prompts, accepting default settings. After installation, right-clicking on the desktop or within a folder should reveal Git Bash and Git GUI options. Git Bash, the command-line tool, is used for GitHub operations. Note that Git's default text editor is Vim, which has a unique interface. Basic Vim navigation will be covered as needed.
Creating a GitHub Account
Visit GitHub at https://github.com/ to sign up. Access may require a VPN depending on your location. Use a valid email address for registration. Once registered, Git allows local operations without constant GitHub login.
Project Upload Workflow
The upload process involves three key stages:
- Staging files to a local buffer.
- Committing files to the local repository.
- Pushing files to the remote GitHub repository. Prior configuration of the local repository is required.
Creating a Remote Repository
- From your GitHub homepage, click the + icon in the top-right corner and select New repository.
- Fill in the repository name, description (optional), and choose public or private visibility. Initialize with a README is optional.
- Click Create repository.
Upon creation, note the default branch name (typically main). The repository page provides the URL needed for linking, available in both HTTPS and SSH formats.
Configuring the Local Repository
- Create a new folder on your computer to serve as the local project directory. Avoid using Chinese chaarcters in the path.
- Navigate into this folder, right-click, and select Git Bash Here to open the terminal.
- Initialize a Git repository in this directory:
This creates a hiddengit init.gitfolder. - Configure your Git identity. This can be done by editing the
configfile inside the.gitfolder or via the command line.- Method A (Edit config file): Open
.git/configin a text editor and add:[user] name = YourName email = your.email@example.com - Method B (Command line):
git config --global user.email "your.email@example.com" git config --global user.name "YourName"
- Method A (Edit config file): Open
- Link the local repository to the remote GitHub repository. Copy the HTTPS URL from your GitHub repo page and run:
No output indicates success.git remote add origin https://github.com/your-username/repo-name.git
Handling Branch Names
Older Git setups may default to master as the primary branch name, while GitHub uses main. To synchronize:
- Check existing branches:
git branch -a - Fetch the remote
mainbranch:git fetch - Switch to the
mainbranch:
Agit checkout mainREADME.mdfile from the remote may appear in you're folder.
Uploading Files for the First Time
- Stage Files: Copy your project files into the local repository folder. To stage all files:
To stage a specific file:git add .
Verify status withgit add example.pygit status. - Commit Files: Commit the staged files with a descriptive message.
git commit -m "Initial project commit" - Push to Remote: For the first push, specify the remote and branch:
Subsequent pushes can use simplygit push -u origin maingit push.
Refresh your GitHub repository page to confirm the files have been uploaded.
Basic Vim Operations in Git Bash
If you accidental enter Vim's edit mode:
- To exit with out saving: Press
Esc, then type:q!and pressEnter. - To save and exit: Press
Esc, then type:wqand pressEnter. - Copy/Paste: Use right-click in the Git Bash window.
- Cencel a running command:
Ctrl + C. - Navigate command history: Use the up/down arrow keys.