Git & GitHub Tutorial (Beginner's Guide)
Git & GitHub Tutorial (Beginner's Guide)
What is Git? Git is a distributed version control system that tracks changes in code and lets multiple
developers work together seamlessly. Each developer has a complete copy of the project history on their
machine 1 2 . This means you can commit changes, create branches, and merge code offline, then sync
later. Git keeps a full history (like backups) on every computer, so it is fast and reliable 1 3 .
What is GitHub? GitHub is an online platform that hosts Git repositories. It provides cloud storage for
your code and tools for collaboration 2 . Using GitHub alongside Git lets teams share projects, review
code, and manage projects together. Features like pull requests, issue tracking, and project boards make
teamwork easier 4 5 . In practice, you work on your local Git repo on Windows (using Git Bash or CMD),
then push changes to GitHub so others can see and integrate them 3 2 .
• Key reasons to use Git/GitHub: track all changes, revert mistakes, work on branches, collaborate with
others, and keep backups of code in the cloud 1 2 .
git --version
You should see the Git version. If not, ensure the PATH was added during install.
4. Configure your identity: Set your name/email (only needed once). In Bash/CMD:
Tip: On Windows, use Git Bash (installed with Git) or CMD. Git commands work the same; many developers
prefer Git Bash for Unix-like commands.
• Working Directory: Your current files on disk (where you edit code).
• Staging Area (Index): A “staging” space where you prepare changes for commit.
1
• Local Repository: The .git folder storing the full commit history.
git init
This creates a new Git repository 8 . Now Git will track changes in this folder.
git status
This shows new, modified, or staged files. It helps you know what will be committed 9 .
• Make a change: Create a file (e.g., README.md ) or edit code. For example:
• Stage changes: Add files to the staging area with git add . For example:
This tells Git, “include this file in the next commit.” You can stage multiple files:
git add .
The git add command takes changes from the working directory and adds them to the
staging area 10 .
2
The -m flag gives a commit message. This creates a new commit containing the staged changes
11 . Think of a commit as a saved checkpoint of your project.
This lists commits (most recent first) 12 . Each line shows a commit ID and message. Good commit
messages (brief and descriptive) are best practice.
Example: If you later add more text to README.md , you would again git add README.md and git
commit -m "Update README" to save that change.
git diff
• Remove from staging: If you accidentally added a file but decide not to commit it yet, you can
unstage it:
This moves the change out of the staging area. (On newer Git versions, git restore --staged
filename also works.) This does not delete your work, it just “unstages” it.
• git revert : Safely creates a new commit that undoes changes from a previous commit.
• git reset : Can move the HEAD to an earlier commit (discarding later commits). For example:
This command resets the branch to one commit earlier and discards all changes in your working
directory and staging area 13 14 . Use --hard with caution, only when you really want to
eliminate changes 15 14 . (Without --hard , git reset defaults to --mixed , which keeps
changes in your files but unstages them.)
3
• Example: If you made a commit and immediately realize it’s wrong, git reset --hard HEAD~1
will erase that commit as if it never happened 13 15 . But remember this is destructive locally.
Stashing Changes
Sometimes you start editing but need to switch to another branch or task without committing unfinished
work. Git stash lets you temporarily shelve your uncommitted changes 16 :
git stash
This records your uncommitted (staged and unstaged) changes and then cleans your working
directory to match the last commit 17 . It’s like putting changes in a locker.
• To apply the latest stash and remove it from the stash list:
Use-case: You have edits in progress but must urgently switch to main to fix a bug. Run git stash ,
switch branches, fix/commit, then return and run git stash pop to continue where you left off 16 18 .
4
2. Add a remote: In your local Git repo, link the remote URL. The convention is to name the primary
remote origin :
(Use master if your default branch is named master.) This sends your main branch to the
origin remote. The -u sets upstream tracking so future git push or git pull will default
to this remote/branch.
4. Clone an existing repo: To work on someone else’s or your own repo, you can clone it:
Note: On Windows, git push and git pull behave the same in Git Bash or CMD. When pushing, you
may be asked for your GitHub username/password (or use SSH keys for authentication).
Branches
Branches in Git let you develop features or fixes in isolation 20 . The default branch is usually main (or
master ).
or (shortcut)
• Switch branches:
5
git checkout feature-x
• List branches:
git branch
• Merge branches: After finishing work on a branch, switch back to main and merge:
This integrates the commits from feature-x into main . Git automatically does a “merge
commit” if needed 21 .
Example: You start a bug fix on branch bugfix , commit changes there, then merge back into main when
done. If Git can’t auto-merge, you’ll get a merge conflict (see below).
• Upstream remote: To stay updated with the original repo (the “upstream”), add it as another
remote:
6
git remote add upstream https://github.com/originaluser/original-repo.git
This updates your local and then you can push to your fork. Keeping in sync avoids falling behind on
changes.
Why: If you contribute to an open-source project, fork it, clone your fork, then use upstream to
incorporate new changes from the main project 22 23 .
Pull Requests
A Pull Request (PR) is a GitHub feature to propose merging your changes into someone else’s repository. It
provides a discussion and review interface 5 .
• Create a PR: Push your feature branch to GitHub, then on GitHub click “Compare & pull request.”
Select the base (e.g., main ) and the head (your branch). Write a description and submit.
• Never commit on main : Best practice is to make all changes on branches (even your own repo’s
branches), then open a PR. This keeps main stable 5 .
• Review and merge: Team members review the PR. Once approved, you (or project maintainers) click
“Merge”. This merges the commits into the target branch (often main ).
• Amend or remove commits: If you find mistakes in a PR, you can fix them in your branch and force
push:
This updates the PR. Use force push carefully; it rewrites history that others may have.
Pull requests help with code review and discussion before changes go into the official project 5 .
7
Squashing Commits and Rebasing
Sometimes you want a clean history. Git offers rebase and squash:
• Rebase ( git rebase ): Reapplying commits onto a new base 25 . Common use: update a feature
branch with the latest main :
This moves your feature-x commits to appear on top of main , creating a linear history 26 .
Rebasing rewrites history (creates new commits) but keeps the changes. It is useful for integrating
updates and making a clean history before merging 26 25 .
This opens an editor listing the last 3 commits. Change pick to squash (s) for commits you
want to fold into the previous one. For example, combining 3 tiny commits into 1 bigger commit 27 .
After saving, Git combines them, making the history look like your work was done in a single, clean
commit 27 .
• Why squash? It hides minor fixup commits and presents the feature as one neat unit 27 . Others
see a simpler history.
• Note: Interactive rebase is powerful but use with care. It rewrites commit hashes. Never rebase
commits that have been pushed to a shared branch (unless you coordinate with others).
Resetting Commits
Git has three reset modes 13 :
• Soft reset ( --soft ): Moves HEAD to a previous commit but keeps changes staged.
• Mixed reset (default): Moves HEAD and unstages changes (keeps them in working directory).
• Hard reset ( --hard ): Discard all changes in the specified commits. E.g.,
This moves HEAD two commits back and erases all intervening changes from your files 13 15 .
8
Warning: git reset --hard can permanently delete work. Use it only when you want to throw away
uncommitted changes 15 14 . It’s useful to quickly revert to a previous state on a local branch 13 14 .
• Identifying conflicts: After a failed git merge or git pull , Git will list conflict markers in files.
git status will show “Unmerged paths.”
• Resolving conflicts: Manually edit the file to fix the conflict. The file will have sections like:
<<<<<<< HEAD
your change
=======
incoming change
>>>>>>> feature-branch
Decide which code to keep (or combine), then remove the markers. Save the file.
• Conflict tools: You can also use GUI tools or IDE merge tools to help. But the basic idea is the same:
reconcile differences and commit.
In short: Conflicts require human intervention. Git helps by pointing out conflicts; you must resolve them to
complete the merge 28 .
• Tracking Changes:
9
• git add <file> – stage changes for commit 10 .
• git commit -m "msg" – save staged changes into a new commit 11 .
• Undo Changes:
• git reset --hard <commit> – reset to a commit and discard all changes 13 .
• git remote add origin <url> – link local repo to a remote repo.
• git push – upload commits to remote 29 .
• git merge <branch> – merge another branch into the current one.
• Stashing:
• Syncing Forks/Upstream:
Each command is used in Git Bash or Windows CMD. Always double-check with git status to see what
state you’re in before and after each step 9 .
10
Best Practices
• Branch for features: Don’t work directly on main . Create descriptive branches (e.g. feature-
login ) 20 .
• Commit often: Make small, logical commits with clear messages.
• Pull before push: git pull (or git fetch then git merge ) often to avoid conflicts.
• Use .gitignore: Exclude files you never want in Git (e.g. binaries, IDE configs).
• Review changes: Use git diff and git status to review before committing or pushing.
• Backup: Pushing to GitHub acts as a backup. Always push important branches to avoid losing work.
By following these steps and commands, you can manage your code with Git on Windows (CMD or Git Bash)
and collaborate using GitHub. This workflow – initializing a repo, making commits, creating branches, and
using pull requests – covers most real-world needs for version control 1 5 .
Sources: Git concepts and commands are based on Git documentation and tutorials 8 16 25 13 28 23
5 , adapted for beginners. Each command above is demonstrated or explained with real examples.
11