Git & GitHub - Basics, Branching, Merging, PRs

GOUROB DASGOUROB DAS
14 min read

Table of contents

Learning points:

  1. What is Version Control? โ€“ Why Git is the most popular VCS.

  2. Git vs. Other VCS โ€“ Differences from SVN, Mercurial.

  3. Repositories, Commits, and Branches โ€“ How Git tracks changes.

  4. Git States โ€“ Working directory, staging area, and commit history.

  5. Basic Git Commands โ€“ init, add, commit, status, log, diff.

  6. Branching & Merging โ€“ Creating branches and merging changes.

  7. Pull Requests (PRs) โ€“ Understanding GitHub workflows.

  8. Remote Repositories โ€“ Connecting local Git with GitHub (git remote add origin).

  9. Collaboration โ€“ Cloning, fetching, pulling, and pushing changes.

  10. 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 into main and push the changes.

  • โš ๏ธ Challenge 4: Use git reset or git revert to undo a commit.

  • โš ๏ธ Challenge 5: Rebase feature-branch onto main 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 and git rebase -i to edit past commits.

โœ… Solutions

Challenge 1: Fork and clone an open-source project (sample project link)

Step 1: Fork the Repository

  1. Open the provided GitHub link: (Sample Project LInk )

  2. Click the Fork button in the top-right corner.

  3. Select your GitHub account as the destination for the fork.

Step 2: Clone the Forked Repository

  1. Open your terminal or VS Code.

  2. 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.

  1. 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.

  1. Switch to the main Branch

     git checkout main
    
  2. Merge feature-branch into main

git merge feature-branch
  1. Push the Changes to GitHub
git push origin main

โœ… OutPut

๐Ÿ›  Explanation of the Commands:

  • git checkout main โ†’ Switches to the main branch.

  • git merge feature-branch โ†’ Merges the changes from feature-branch into main.

  • git push origin main โ†’ Pushes the updated main 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)

  1. Check the commit history
git log --oneline
  1. Reset to the previous commit (Removes the last commit completely)
git reset --hard HEAD~1
  1. Force push if changes were already pushed to GitHub
git push --force

โš ๏ธ Warning: This removes the commit and its changes permanently!

  1. 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)

  1. Find the Commit ID
git log --oneline
  1. Revert the Commit
git revert <commit_id>
  1. 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

Featuregit mergegit rebase
HistoryCreates a new "merge commit"Rewrites commit history
SimplicityEasier (preserves commit history)Complex (requires conflict resolution)
Best Used ForKeeping history clean and tracking mergesMaking feature branches linear

๐Ÿ›  Explanation of the Commands:

  • git checkout feature-branch โ†’ Switches to feature-branch.

  • git rebase main โ†’ Moves feature-branch commits on top of main.

  • 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 the main branch.

  • git merge feature-branch โ†’ Merges feature-branch into main.

  • 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

  • Open GitHub and navigate to your repository.

  • Click on the feature-branch in the branch dropdown.

3. Create a Pull Request (PR)

  • Click the "Compare & pull request" button.

  • Ensure the base branch is main and the compare branch is feature-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 the feature-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 into main.


โœ… 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 into main 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

  1. Tag the latest commit (Lightweight Tag):
git tag v1.0
  1. Tag with a message (Annotated Tag - Recommended):
git tag -a v1.0 -m "Version 1.0 release"
  1. Tag an older commit (if needed):
git tag v0.9 <commit-hash>

Replace <commit-hash> with the actual commit ID from git 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

  1. Push a single tag:
git push origin v1.0
  1. 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 of main, 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.

0
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."