Git & GitHub Tutorial

Rahul's Profile

GitHub Workflow

After Cloining the Repo

1. Navigate into the Cloned Repository

After cloning, move into the repository directory using:

cd repo-name

Replace repo-name with your actual project folder name.

2. Check the Remote Repository

Use the following command to view the linked GitHub repository:

git remote -v

This shows the fetch and push URLs for the remote named origin.

3. Create a New Branch (Optional)

It’s a good practice to create a new branch before making changes:

git checkout -b feature-branch-name

This helps in keeping the main branch stable.

4. Start Editing or Building

You can now begin working on your code. Any changes you make can be tracked using:

git status

This shows modified files, new files, and untracked files.

5. Stage and Commit Changes

After making changes, stage and commit them:

git add .
git commit -m "Your meaningful commit message"

Use descriptive messages to explain what was done.

6. Push Changes to GitHub

Finally, push your branch to GitHub:

git push origin feature-branch-name

This uploads your code to GitHub for collaboration or backup.

Pull, Push & Fetch

1. Git Pull

git pull is used to fetch and merge changes from the remote repository into your current branch.

git pull origin main

This command pulls updates from the main branch of the remote named origin.

2. Git Push

After committing your changes locally, use git push to upload them to the GitHub repository.

git push origin main

Replace main with your branch name if you're working on a different one.

3. Git Fetch

git fetch downloads the latest changes from the remote repository but does not merge them into your current branch.

git fetch origin

Use it when you want to inspect changes before merging. Combine it with git log or git diff to review changes.

4. View Remote Branches

After fetching, you can see remote branches using:

git branch -r

This is useful to check what branches exist on the remote.

5. Merge Fetched Changes

If you used git fetch, you can merge the changes manually with:

git merge origin/main

This merges the main branch from the remote into your local branch.

Pull Request and Merge Conflict

1. What is a Pull Request?

A Pull Request (PR) is a way to propose changes in a project. You push your changes to a branch and then request them to be reviewed and merged into the main branch via GitHub’s interface.

It’s widely used in collaboration workflows.

2. Create a Pull Request

After pushing your branch to GitHub, go to the repository on GitHub and click:

  • Compare & pull request (appears after push)
  • Fill in the title and description
  • Click Create pull request

3. Merge a Pull Request

Once reviewed, you or a reviewer can merge the pull request using:

  • Merge pull request
  • Select Create a merge commit or Squash and merge

This brings your changes into the main branch.

4. What is a Merge Conflict?

A merge conflict occurs when Git cannot automatically resolve differences between two branches. It typically happens when two people change the same line in a file or one deletes a file the other modifies.

5. Resolving Merge Conflicts

Open the file with conflict. Git marks conflicting areas like this:


<<<<<<< HEAD
Your changes here
=======
Incoming changes here
>>>>>>> branch-name
  

Edit the file to keep the desired content and remove the conflict markers.

Then use:

git add conflicted-file.txt
git commit