0% found this document useful (0 votes)
11 views8 pages

Git Tutorial

This document is a comprehensive guide to Git and GitHub, covering everything from installation and basic commands to advanced features like branching, merging, and automation with GitHub Actions. It includes practical examples and best practices for developers, as well as a full workflow for managing projects. Additionally, it provides a quick reference cheat sheet for common Git commands.

Uploaded by

Roshan kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Git Tutorial

This document is a comprehensive guide to Git and GitHub, covering everything from installation and basic commands to advanced features like branching, merging, and automation with GitHub Actions. It includes practical examples and best practices for developers, as well as a full workflow for managing projects. Additionally, it provides a quick reference cheat sheet for common Git commands.

Uploaded by

Roshan kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Git & GitHub: Complete Guide (Beginner to

Advanced)

1. Introduction to Git & GitHub

What is Git?

Git is a distributed version control system used to track changes in code and collaborate with others.

What is GitHub?

GitHub is a cloud platform for hosting Git repositories with tools for collaboration like Issues, Pull
Requests, Projects, Actions, etc.

2. Installing Git

Windows / Mac / Linux

• Download from: [Link]


• Verify installation:

git --version

3. Git Configuration (First-Time Setup)

git config --global [Link] "Your Name"


git config --global [Link] "your@[Link]"
git config --global [Link] main

4. Git Basics

Initialize a repository

git init

1
Check repository status

git status

Add files to staging

git add filename

Add all files:

git add .

Commit changes

git commit -m "Your commit message"

View commit history

git log

5. Connecting Local Repo to GitHub

Add remote origin

git remote add origin [Link]

Push code to GitHub

git push -u origin main

6. Branching in Git (Beginner to Advanced)

Create a new branch

git branch feature-branch

Switch to a branch

2
git checkout feature-branch

Or create & switch at same time:

git checkout -b feature-branch

View branches

git branch

Delete a branch

Local:

git branch -d branch-name

Remote:

git push origin --delete branch-name

7. Merging in Git

Merge branch into main

Switch to main:

git checkout main

Merge:

git merge feature-branch

Fast-forward merge (if no conflict)

Git automatically moves the pointer.

Three-way merge (when branches diverge)

Git creates a new commit.

3
8. Handling Merge Conflicts
When Git detects conflicting code: 1. Open the file with conflict markers

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

2. Manually edit 3. Stage resolved files:

git add file

4. Commit:

git commit

9. Pulling & Fetching

Pull latest changes

git pull

Fetch without merging

git fetch

Pull with rebase (cleaner history)

git pull --rebase origin main

10. Forking (For Open Source Contributions)

Steps:

1. Click Fork on GitHub


2. Clone fork locally:

git clone [Link]

4
3. Add upstream remote:

git remote add upstream [Link]

4. Sync fork:

git fetch upstream

11. GitHub Issues (Creating & Solving)

Creating an Issue

1. Go to Issues tab
2. Click New Issue
3. Add title, description, labels, assignees
4. Submit

Solving Issues

1. Read issue details


2. Create a branch related to the issue:

git checkout -b fix-issue-#12

3. Commit changes with reference:

git commit -m "Fixes #12: issue description"

4. Push and raise PR

GitHub will auto-close the issue when PR is merged.

12. Pull Requests (PRs)

Create Pull Request

1. Push branch
2. Visit GitHub → Compare & Pull Request
3. Add details, link issue, assign reviewers
4. Submit

Reviewing a PR

• Add comments
• Request changes

5
• Approve

Merging PR

Options: - Merge commit - Squash merge (clean history) - Rebase merge

13. Advanced Git Commands

Stash changes (temporary save)

git stash

Apply stash:

git stash apply

List stash:

git stash list

Rebase (rewrite history)

git rebase main

Interactive rebase (edit commits)

git rebase -i HEAD~5

Cherry-pick commit

git cherry-pick <commit-hash>

Reset (undo commits)

Soft reset:

git reset --soft HEAD~1

Hard reset:

git reset --hard HEAD~1

6
14. GitHub Actions (Automation Intro)
Example workflow file ( .github/workflows/[Link] ):

name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Node setup
uses: actions/setup-node@v3
with:
node-version: '16'
- run: npm install
- run: npm test

15. Best Practices for Developers (Advanced)


• Commit small, frequent changes
• Use meaningful commit messages
• Always create feature branches
• Keep main / master stable
• Use PR reviews for quality
• Sync your branch with main frequently

git fetch origin


git rebase origin/main

16. Full Git Workflow (Professional)


1. Pick an issue
2. Create a new branch
3. Develop & commit code
4. Push branch
5. Open PR → add reviewer
6. Fix comments
7. Merge PR
8. Delete branch
9. Work on next issue

7
17. Git Cheat Sheet (Quick Reference)

Action Command

Create branch git checkout -b new-branch

Switch branch git checkout branch

Commit changes git commit -m "msg"

Push branch git push origin branch

Merge branch git merge branch

Pull latest git pull

Stash changes git stash

View logs git log --oneline

If you'd like, I can also create: - A PDF version of this document - Flowcharts for Git workflow -
Diagrams for branching & merging - An advanced cheat sheet

Just tell me!

You might also like