This a documentation on how to create a new Git repository, add files, and push your code to a remote repository on GitHub.
Before you begin, ensure that:
- Git is installed on your machine.
- You have a GitHub account.
- You have access to a terminal or command line interface.
-
Navigate to your project directory
Open your terminal and navigate to the directory where you want to initialize your Git repository.cd /path/to/your/project
-
Initialize the Git repository
Initialize a new Git repository in your project directory.git init
This command creates a new
.git
subdirectory in your project folder, which will track all your version-controlled files.
-
Check the status of your repository
See which files are untracked or have changes that need to be committed.git status
-
Add files to the staging area
Add all files in your project directory to the staging area to prepare them for commit.git add .
Alternatively, you can add specific files:
git add filename1 filename2
-
Commit the staged files
Commit your changes with a descriptive message to record the snapshot of your project at this point.git commit -m "Initial commit"
-
Create a new repository on GitHub
Go to GitHub, create a new repository, and copy the repository URL. -
Add the remote repository
Link your local repository to the remote repository on GitHub.git remote add origin https://github.com/your-username/your-repo-name.git
-
Push your code to the remote repository
Push the code in your local repository to the remote repository on GitHub.git push -u origin master
The
-u
flag sets theorigin
repository as the default remote for future pushes.
This part covers essential Git operations, including cloning a repository, making changes, pulling the latest code, forking a repository, and creating a pull request.
-
Find the repository URL
Navigate to the repository on GitHub you want to clone. Click the "Code" button and copy the repository URL (HTTPS, SSH, or GitHub CLI). -
Clone the repository
Open your terminal and use thegit clone
command to clone the repository to your local machine.git clone https://github.com/username/repository-name.git
-
Navigate into the repository
Change into the newly created directory.cd repository-name
-
Create a new branch
It's a good practice to create a new branch before making changes. This keeps your changes isolated from themain
branch.git checkout -b feature-branch-name
Replace
feature-branch-name
with a meaningful name describing your feature or change. -
Make your changes
Modify the files as needed using your favorite text editor or IDE. -
Add changes to the staging area
Add the modified files to the staging area.git add .
-
Commit your changes
Commit the staged changes with a descriptive commit message.git commit -m "Description of changes made"
-
Switch to the main branch
Before pulling the latest code, switch to the main branch (usuallymain
).git checkout main
-
Pull the latest code
Pull the latest changes from the remote repository to ensure your local repository is up-to-date.git pull origin main
-
Merge your changes
Merge your feature branch into the main branch.git merge feature-branch-name
-
Fork the repository
Navigate to the repository on GitHub and click the "Fork" button. This creates a copy of the repository under your GitHub account. -
Clone your fork
Clone your forked repository to your local machine.git clone https://github.com/your-username/forked-repo-name.git
Replace
https://github.com/your-username/forked-repo-name.git
with the URL of your forked repository. -
Make your changes
Follow the same steps as mentioned earlier to create a new branch, make changes, add, commit, and push the changes. -
Push your branch to GitHub
Push your branch to your forked repository on GitHub.git push origin feature-branch-name
-
Create a pull request
- Go to the original repository (not your fork).
- You will see a prompt to create a pull request from your recently pushed branch.
- Click "Compare & pull request."
- Add a title and description for your pull request.
- Submit the pull request.