Git & GitHub - Basics, Branching, Merging, PRs

Table of contents
- Learning points:
- โ Solutions
- Challenge 1: Fork and clone an open-source project (sample project link)
- โ Challenge 2: Create a new branch (feature-branch), switch, and commit changes.
- ๐ Explanation of the Commands:
- โ Challenge 3: Merge feature-branch into main and push the changes.
- ๐ Explanation of the Commands:
- โ Challenge 4: Use git reset or git revert to undo a commit.
- โ OutPut
- ๐ Explanation of the Commands:
- โ Challenge 5: Rebase feature-branch onto main and understand the difference.
- โ OutPut
- ๐ Difference Between git merge and git rebase
- ๐ Explanation of the Commands:
- โ Challenge 6: Create a PR on GitHub from a feature branch.
- โ OutPut
- ๐ Explanation of the Commands:
- โ Challenge 7: Create conflicting changes in different branches and resolve them.
- โ OutPut
- ๐ Explanation of the Commands:
- โ Challenge 8: git stash to save and restore uncommitted changes.
- โ OutPut
- โ OutPut
- ๐ Explanation of the Commands:
- โ Challenge 9: Add version tags to commits and push them.
- โ OutPut
- ๐ Explanation of the Commands
- โ Challenge 10: Use git commit --amend and git rebase -i to edit past commits.
- โ OutPut
- Option 2: Edit Older Commits (git rebase -i)
- โ OutPut
- โ OutPut
- Thanks for your time. I hope you enjoy my blog, please follow me on LinkedIn for more updates.

Learning points:
What is Version Control? โ Why Git is the most popular VCS.
Git vs. Other VCS โ Differences from SVN, Mercurial.
Repositories, Commits, and Branches โ How Git tracks changes.
Git States โ Working directory, staging area, and commit history.
Basic Git Commands โ
init
,add
,commit
,status
,log
,diff
.Branching & Merging โ Creating branches and merging changes.
Pull Requests (PRs) โ Understanding GitHub workflows.
Remote Repositories โ Connecting local Git with GitHub (
git remote add origin
).Collaboration โ Cloning, fetching, pulling, and pushing changes.
Resolving Merge Conflicts โ How to handle and fix conflicts
Initial Tasks:
โ Install Git on your system: Git Installation
โ Configure your Git username and email:
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
โ Create a new folder and initialize a Git repository:
mkdir day_3_git-project && cd day_3_git-project
git init
โ
Create two files (index.html
, style.css
), add content, and commit:
touch index.html style.css
git add .
git commit -m "Initial commit"
โ
Modify index.html
, check changes, and make another commit:
git status
git diff
git add index.html
git commit -m "Updated index.html"
โ Check commit history using:
git log --oneline
โ Create a GitHub Repository, add a remote, and push:
git remote set-url origin https://your token@github.com/gourobdas0103/60DaysDevOps-Challenges.git
git branch -M main
git push -u origin main
โ output
github link ( click here)
๐ฅ Challenges :
โ ๏ธ Challenge 1: Fork and clone an open-source project (Sample Project Link)
โ ๏ธ Challenge 2: Create a new branch (
feature-branch
), switch, and commit changes.โ ๏ธ Challenge 3: Merge
feature-branch
intomain
and push the changes.โ ๏ธ Challenge 4: Use
git reset
orgit revert
to undo a commit.โ ๏ธ Challenge 5: Rebase
feature-branch
ontomain
and understand the difference.โ ๏ธ Challenge 6: Create a PR on GitHub from a feature branch.
โ ๏ธ Challenge 7: Create conflicting changes in different branches and resolve them.
โ ๏ธ Challenge 8:
git stash
to save and restore uncommitted changes.โ ๏ธ Challenge 9: Add version tags to commits and push them.
โ ๏ธ Challenge 10: Use
git commit --amend
andgit rebase -i
to edit past commits.
โ Solutions
Challenge 1: Fork and clone an open-source project (sample project link)
Step 1: Fork the Repository
Open the provided GitHub link: (Sample Project LInk )
Click the Fork button in the top-right corner.
Select your GitHub account as the destination for the fork.
Step 2: Clone the Forked Repository
Open your terminal or VS Code.
Clone the forked repository to your local machine:
git clone https://github.com/your-username/eks-auto-mode-workshop.git
Replace your-username
with your GitHub username.
Navigate into the cloned repository:
cd eks-auto-mode-workshop
โ OutPut
๐ Explanation of the Commands:
git clone <repo_url>
โ Downloads the repository to your local system.cd <directory_name>
โ Changes directory to the cloned repository.git remote add upstream <original_repo_url>
โ Links the forked repo to the original for updates.git remote -v
โ Displays the list of remote repositories.
โ
Challenge 2: Create a new branch (feature-branch
), switch, and commit changes.
Step 1: Create a New Branch
Step 1: Create a New Branch
This creates a new branch named feature-branch
.
Step 2: Switch to the New Branch
git checkout feature-branch
Switches the working directory to feature-branch
, allowing you to make changes separately from the main branch.
Step 3: Make Some Changes
Modify or create a file in the repository. For example:
echo "New feature added" > feature.txt
This creates a new file named feature.txt
and writes "New feature added" into it.
Step 4: Stage and Commit the Changes
git add feature.txt
git commit -m "Added a new feature file"
Stages the file and commits it with a message describing the changes.
โ OutPut
๐ Explanation of the Commands:
๐น git branch feature-branch
โ Creates a new branch named feature-branch
.
๐น git checkout feature-branch
(or git switch feature-branch
in newer versions)
โ Switches to the newly created branch so you can work on it separately.
๐น echo "New feature added" > feature.txt
โ Creates a new file called feature.txt
and writes "New feature added" inside it.
๐น git add feature.txt
โ Stages the newly created file, preparing it for the next commit.
๐น git commit -m "Added a new feature file"
โ Commits the staged file with a message describing the changes.
โ
Challenge 3: Merge feature-branch
into main
and push the changes.
Switch to the
main
Branchgit checkout main
Merge
feature-branch
intomain
git merge feature-branch
- Push the Changes to GitHub
git push origin main
โ OutPut
๐ Explanation of the Commands:
git checkout main
โ Switches to themain
branch.git merge feature-branch
โ Merges the changes fromfeature-branch
intomain
.git push origin main
โ Pushes the updatedmain
branch to the remote repository.
โ
Challenge 4: Use git reset
or git revert
to undo a commit.
Option 1: Undo the Last Commit Using git reset
(Hard Reset - Deletes Changes)
- Check the commit history
git log --oneline
- Reset to the previous commit (Removes the last commit completely)
git reset --hard HEAD~1
- Force push if changes were already pushed to GitHub
git push --force
โ ๏ธ Warning: This removes the commit and its changes permanently!
- Push the Changes to GitHub (Forcefully, if needed)
git push origin main --force
Option 2: Undo a Commit Using git revert
(Keeps Changes in History)
- Find the Commit ID
git log --oneline
- Revert the Commit
git revert <commit_id>
- Push the Reverted Commit
git push origin main
โ OutPut
๐ Explanation of the Commands:
git log --oneline
โ Displays commit history in a short format.git reset --soft HEAD~1
โ Moves back one commit but keeps changes staged.git reset --hard HEAD~1
โ Deletes the last commit and its changes permanently.git push origin main --force
โ Forces the push after a reset.git revert <commit_id>
โ Creates a new commit that undoes changes from the specified commit.
โ
Challenge 5: Rebase feature-branch
onto main
and understand the difference.
1. Sitch to the feature-branch
git checkout feature-branch
2. Rbase feature-branch
onto main
git rebase main
3. Rsolve Any Merge Conflicts (if needed)
If a conflict appears, Git will pause the rebase.
Open the conflicted file(s) and manually fix them.
Stage the resolved files:
git add <resolved_file>
- Continue the rebase:
git rebase --continue
- If you want to abort the rebase and go back to the original state:
git rebase --abort
4. Switch to main
and Merge (After Rebase is Complete)
git checkout main
git merge feature-branch
5. Push the Changes to GitHub
git push origin main
โ OutPut
๐ Difference Between git merge
and git rebase
Feature | git merge | git rebase |
History | Creates a new "merge commit" | Rewrites commit history |
Simplicity | Easier (preserves commit history) | Complex (requires conflict resolution) |
Best Used For | Keeping history clean and tracking merges | Making feature branches linear |
๐ Explanation of the Commands:
git checkout feature-branch
โ Switches tofeature-branch
.git rebase main
โ Movesfeature-branch
commits on top ofmain
.git add <resolved_file>
โ Stages resolved conflicts.git rebase --continue
โ Continues rebase after resolving conflicts.git rebase --abort
โ Cancels the rebase if things go wrong.git checkout main
โ Switches to themain
branch.git merge feature-branch
โ Mergesfeature-branch
intomain
.git push origin main
โ Pushes changes to GitHub.
โ Challenge 6: Create a PR on GitHub from a feature branch.
1. Push feature-branch
to GitHub
git push origin feature-branch
2. Go to GitHub Repository
3. Create a Pull Request (PR)
Click the "Compare & pull request" button.
Ensure the base branch is
main
and the compare branch isfeature-branch
.Add a meaningful title and description explaining the changes.
Click "Create Pull Request".
4. Wait for Review & Merge the PR
If required, reviewers will check your PR and request changes.
Once approved, click "Merge Pull Request".
โ OutPut
๐ Explanation of the Commands:
git push origin feature-branch
โ Pushes thefeature-branch
to GitHub.Pull Request (PR) โ A request to merge changes from one branch to another.
"Compare & pull request" โ GitHub's UI option to create a PR.
Merge PR โ Combines changes from
feature-branch
intomain
.
โ Challenge 7: Create conflicting changes in different branches and resolve them.
Step 1: Create Two Branches (feature-branch
and bugfix-branch
)
git checkout -b feature-branch
echo "This is a feature update" > conflict.txt
git add conflict.txt
git commit -m "Added feature update in conflict.txt"
git push origin feature-branch
git checkout main
git checkout -b bugfix-branch
echo "This is a bug fix update" > conflict.txt
git add conflict.txt
git commit -m "Added bug fix update in conflict.txt"
git push origin bugfix-branch
Step 2: Merge bugfix-branch
into main
git checkout main
git merge bugfix-branch
git push origin main
๐ At this point,
bugfix-branch
changes are merged intomain
successfully.
Step 3: Try Merging feature-branch
into main
(Triggers Conflict)
git merge feature-branch
๐จ Git will show a merge conflict in
conflict.txt
.
Step 4: Resolve the Conflict Manually
- Open
conflict.txt
and you will see something like this:
<<<<<<< HEAD
This is a bug fix update
=======
This is a feature update
>>>>>>> feature-branch
- Edit the file to keep the correct changes, like:
This is a feature update and bug fix update combined.
- Stage the resolved file:
git add conflict.txt
Step 5: Complete the Merge
git commit -m "Resolved merge conflict in conflict.txt"
git push origin main
โ OutPut
๐ Explanation of the Commands:
git status
โ Shows the status of files, including conflicts.nano conflict.txt
โ Opens the file in Nano editor for editing.Conflict Markers (
<<<<<<<
,=======
,>>>>>>>
) โ Show the conflicting parts from different branches.Manually Editing the File โ You decide how to merge the changes.
git add <file>
โ Stages the resolved file.git commit -m "Resolved merge conflict"
โ Saves the resolution in the Git history.git push origin main
โ Pushes the resolved branch to GitHub.
Now your conflict is resolved, and feature-branch
is successfully merged into main
!
โ
Challenge 8: git stash
to save and restore uncommitted changes.
Step 1: Make Some Uncommitted Changes
Modify a file (e.g., example.txt
):
echo "This is an uncommitted change" >> example.txt
Check the status:
git status
You'll see the file is modified but not committed.
Step 2: Stash the Uncommitted Changes
Since git stash
only works on tracked files, we need to stage the file:
git add example.txt
Check the status again:
git status
Now the file should be in the "Changes to be committed" section.
Step 3: Stash the Uncommitted Changes
git stash
This will store the changes temporarily and clean your working directory.
Verify the stash:
git stash list
โ OutPut
Step 4: Restore the Stashed Changes
- Apply the last stash without deleting it:
git stash apply
- Apply and remove the stash at the same time:
git stash pop
Step 5: Commit and Push the Changes
- Stage the restored file:
git add example.txt
- Commit the changes:
git commit -m "Added example.txt after stashing"
- Push the changes to GitHub:
git push origin main
Step 6: Remove Stashes (If No Longer Needed)
- Remove a specific stash:
git stash drop stash@{0}
- Remove all stashes:
git stash clear
โ OutPut
๐ Explanation of the Commands:
git add <file>
โ Stages the file to be tracked.git stash
โ Saves uncommitted changes temporarily.git stash list
โ Shows all stashed changes.git stash apply
โ Reapplies the last stashed changes without removing them.git stash pop
โ Restores and removes the last stashed changes.git commit -m "message"
โ Saves the restored file with a commit message.git push origin main
โ Pushes changes to GitHub.git stash drop stash@{0}
โ Deletes a specific stash entry.git stash clear
โ Deletes all stashed changes.
๐ Now you've successfully used git stash
to save, restore, and commit uncommitted changes!
โ Challenge 9: Add version tags to commits and push them.
Step 1: Check the Commit History
- Before tagging, view recent commits:
git log --oneline --graph --decorate --all
This shows commit hashes and messages.
Step 2: Add a Tag to a Specific Commit
- Tag the latest commit (Lightweight Tag):
git tag v1.0
- Tag with a message (Annotated Tag - Recommended):
git tag -a v1.0 -m "Version 1.0 release"
- Tag an older commit (if needed):
git tag v0.9 <commit-hash>
Replace
<commit-hash>
with the actual commit ID fromgit log
.
Step 3: Verify the Tag
git tag
This lists all existing tags.
- For detailed tag info:
git show v1.0
Step 4: Push the Tag to GitHub
- Push a single tag:
git push origin v1.0
- Push all tags:
git push origin --tags
โ OutPut
๐ Explanation of the Commands
git tag v1.0
โ Creates a lightweight tag.git tag -a v1.0 -m "Version 1.0 release"
โ Creates an annotated tag.git tag -d v1.0
โ Deletes the local tag.git push origin --delete v1.0
โ Deletes the remote tag.git push origin v1.0
โ Pushes the new tag to GitHub.git push origin --tags
โ Ensures all tags are pushed.
๐ Now your v1.0
tag is properly set up and pushed!
โ
Challenge 10: Use git commit --amend
and git rebase -i
to edit past commits.
Option 1: Modify the Last Commit (git commit --amend
)
Use this method if you need to modify only the latest commit.
Step 1: Modify a Tracked File
echo "New change" >> file.txt
git add file.txt
This stages the modified file.
Step 2: Amend the Last Commit
git commit --amend -m "Updated commit with new changes"
โ This modifies the last commit instead of creating a new one.
Step 3: Push the Amended Commit (If Already Pushed)
If the commit was already pushed, a force push is needed:
git push origin main --force
๐จ Use with caution! This rewrites history and can affect collaborators.
โ OutPut
Option 2: Edit Older Commits (git rebase -i
)
Use this when you need to modify a commit that is NOT the most recent.
Step 1: List Recent Commits
git log --oneline -n 5
โ OutPut
Step 2: Start an Interactive Rebase
To modify the last 3 commits:
git rebase -i HEAD~3
Step 3: Modify Commits
Git opens an interactive editor showing:
pick a1b2c3d Added new feature
pick e4f5g6h Fixed API issue
pick i7j8k9l Updated documentation
- Change
pick
to:
reword a1b2c3d Added new feature
edit e4f5g6h Fixed API issue
pick i7j8k9l Updated documentation
- Save and close the editor
Step 4: Edit the Selected Commits
For
reword
, Git will prompt you to change the commit message.For
edit
, Git will pause the rebase.
Modify the commit (if needed):
echo "Extra fix" >> file.txt
git add file.txt
git commit --amend
Then, continue the rebase:
git rebase --continue
Step 5: Push the Edited Commits
If the commits were already pushed, force push is required
git push origin feature-branch --force
๐จ Again, be careful when force-pushing as it rewrites commit history.
โ OutPut
๐ Explanation of the Commands
git checkout feature-branch
โ Switches to the branch where you're making changes.git fetch origin
โ Updates your local repository with the latest changes from the remote.git rebase main
โ Moves your changes on top ofmain
, applying them sequentially.git add <conflicted-file>
โ Marks resolved files as ready for the next step.git rebase --continue
โ Continues the rebase process after conflicts are resolved.git rebase --skip
โ Skips an unnecessary commit.git rebase --abort
โ Cancels the rebase and restores the original state.git push origin feature-branch --force
โ Pushes the rebased branch to the remote repository, rewriting history.
โ
Final Outcome: Your feature-branch
is successfully rebased onto main
, with all conflicts resolved. ๐
Thanks for your time. I hope you enjoy my blog, please follow me on LinkedIn for more updates.
Subscribe to my newsletter
Read articles from GOUROB DAS directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

GOUROB DAS
GOUROB DAS
About "Hello! My name is Gourob Das, I completed my GSSOC as an open source Contributor, I completed a 4-month internship at Dpay Consultancy Services as an application developer, and completed my MCA Degree at Pondicherry University. I am highly passionate about DevOps dedicated to enhancing experiences through the power of artificial intelligence. Let's connect and collaborate on DevOps, cloud Computing development projects."Enjoy exploring the fascinating world of DevOps, constantly seeking to apply cutting-edge technology to solve real-world problems."