Git Basics
Git Basics
Guide
1. Git Basics
1.1 Initialize a Git Repository
The first step is to set up a Git repository in your local project directory.
Example:
After making changes to files in your project, stage the files for commit.
● Command: git add <file> or git add . (to stage all changes)
● Explanation: This command adds the specified files to the staging area, preparing them for
the next commit.
Example:
Shubham Ahire
1.3 Commit Changes with git commit
Once your changes are staged, the next step is to save them as a commit.
Example:
Example:
Shubham Ahire
1.5 Check Status with git status
At any point, you can check the current state of your working directory:
Example:
● Explanation: HEAD represents the current snapshot of your branch. It’s important for
tracking where you are in your project’s history.
● HEAD is the latest commit made to a branch, there are multiple HEAD for every branch we
have.
Shubham Ahire
2. Git Branching
Example:
Example:
Shubham Ahire
2.3 Create and Switch in One Step with git checkout -b
Example:
Example:
Example:
bash
Copy code
git switch main
Shubham Ahire
3. Connecting and Syncing with GitHub
3.1 Connect to GitHub using a Personal Access Token (PAT)
● Step 1: Create a new PAT from GitHub by navigating to Settings > Developer Settings >
Personal Access Tokens > create token classic.
● Step 2: Clone the remote repository first & use PAT when pushing code for the first time.
● Step 3: Checkgit remote -v, now paste the http code like
https://<token>@github.com/Shubham-Ahire/practice.git now git push
Example:
Shubham Ahire
3.2 Connect via SSH
ssh-keygen: This will create a private & public key for you.
● Step 2: Now copy the public key (.pub) and then paste it as described below.
● Step 3: Add the SSH key to your GitHub account by copying the public key (id_rsa.pub)
and adding it to GitHub > Settings > SSH and GPG keys.
● Step 4: Now set the remote -v, by set-url as told in case of PAT tokens.
Example:
Shubham Ahire
3.3 Push Code to GitHub
Example:
Example:
Shubham Ahire
4. Handling Pull Requests
4.1 Create a Pull Request (PR)
Once changes are pushed to GitHub, you can create a PR to propose these changes to be merged
into a base branch (e.g., main).
Steps:
● Tool: Coderabbit
○ Review the changes proposed in the PR.
○ Coderabbit can assist with suggesting improvements and ensuring best practices.
Once the PR is approved, you can merge the branch into the main branch.
Shubham Ahire