Git for Intermediate– Branching, Remotes, Undoing Changes & More

Once you're familiar with basic Git commands, it's time to explore more practical and real-world usage. Git isn't just about adding and committing files, it's a powerful tool that helps teams collaborate, manage versions, and control the development flow with ease. In this guide, we'll walk you through intermediate Git concepts like branching*,* working with remotes*,* handling merge conflicts*,* undoing mistakes*,* tagging versions*, and even* contributing to open-source projects*.*

Each topic is explained in simple with real examples*, so you can confidently apply these skills in day-to-day development. Whether you're working solo or in a team, mastering these intermediate Git commands will level up your workflow, reduce errors, and make your version control cleaner and more professional.*

1. Branching in Git – What and Why

What is branching?

Branching lets you create a separate line of development so you can work on new features or fixes without disturbing the main project. It helps teams work on multiple features at once. Each branch is like a safe space to try out ideas. Once done, you can merge it back into the main code.

Think of it like creating a copy of your code to experiment with.

2. Creating and Switching Branches

You can create a branch using git branch branch-name and switch to it using git switch or git checkout. This is helpful when you want to work on something new without changing your main files. Git lets you jump between branches easily. This way, each task can live in its own workspace.

Example:

# Create a new branch
git branch feature-login

# Switch to that branch
git checkout feature-login

# Or use the modern switch command
git switch feature-login

3. Merging Branches

After finishing work in a feature branch, you can merge it into the main branch. Use git merge branch-name to combine changes. If there are no conflicts, Git does this automatically. Merging keeps your main code updated with new features.

# First, switch to main
git switch main

# Then merge your branch
git merge feature-login

4. Merge Conflicts – How to Resolve Them

Merge conflicts happen when Git can’t decide which changes to keep from two branches. You’ll see conflict markers in the file that you need to edit manually. After fixing the content, save the file and commit the changes. This tells Git that the conflict is resolved.

# You’ll see conflict markers like this in the file
<<<<<<< HEAD
code from main
=======
code from feature
>>>>>>> feature-login

How to fix:

  • Manually edit and keep the correct code.

  • Remove the conflict markers.

  • Save and commit.

git add .
git commit -m "Resolved merge conflict"

5. Remote Repositories – Add, Fetch, Push, Pull

Remote repositories are stored online (like on GitHub or GitLab). You can add a remote using git remote add origin <url>, then push or pull code as needed. git push uploads your changes, and git pull brings in updates from others. This is how you collaborate with a team.

Example:

# Add a remote
git remote add origin https://github.com/yourusername/repo.git

# Push your branch
git push origin main

# Pull changes from remote
git pull origin main

# Fetch (download but don’t merge)
git fetch origin

6. Tracking vs Non-Tracking Branches

  • Tracking branch: A tracking branch knows which remote branch it’s linked to and fetches updates automatically

  • Non-tracking branch: Non-tracking branches don’t have this link, so you need to push/pull manually.

You can set a tracking branch using the below command. This makes remote work easier.

Example:

# Set upstream (tracking)
git push -u origin feature-login

7. Undoing Changes

Git allows you to undo changes at different stages. If you changed a file but didn’t stage it, use git checkout -- filename to revert. To unstage a file, use git reset filename. You can also undo commits with git reset or update the last commit using git commit --amend.

Undo Unstaged Changes

git checkout -- filename.txt

Undo Staged Changes (but keep file)

git reset filename.txt

Undo Last Commit

# Keep changes in working directory
git reset --soft HEAD~1

# Remove changes too
git reset --hard HEAD~1

# Or amend the last commit
git commit --amend -m "New commit message"

8. Tagging Commits

Tags are like labels used to mark important commits, such as a version release. Use git tag v1.0 to tag a commit. This makes it easy to go back to a specific version later. Tags are useful in deployments and version tracking.

Example:

# Create a tag
git tag v1.0

# Push tag to remote
git push origin v1.0

9. Stashing Work

Stashing temporarily saves your uncommitted work. This is helpful if you need to switch branches quickly but aren’t ready to commit. Use git stash to save, and git stash apply to bring it back later. It’s like putting your work in a drawer for a moment.

# Save your work
git stash

# stash with message
git stash push -m "this is new file"
#OR
git stash save "this is new file"

# See stashes
git stash list

# Apply the last stash
git stash apply
#OR
git stash apply stash@{n}

# OR
git stash pop

#OR
git stash pop stash@{n}

10. Comparing Changes

To see what you’ve changed, use git diff. It shows the difference between files or commits. git log --oneline gives a quick summary of past commits. These tools help you understand your project's history and code changes.

Example:

git diff

# comparison of two commits
git diff commit_one commit_two

#OR
git log --oneline

11. Working with .gitattributes

The .gitattributes file controls how Git handles different file types. You can use it to manage line endings or define merge rules. It helps avoid issues when working across different operating systems. It’s especially useful in team environments.

Example .gitattributes:

*.txt text
*.jpg binary
*.php diff=php

12. Contributing to Remote Projects

To contribute to open-source projects, you usually fork the repo, then clone it to your computer. Make changes in a new branch, push them, and create a pull request. This process is called "fork → clone → pull request." It’s the standard way to suggest improvements to someone else’s project.

git clone https://github.com/yourusername/project.git

Read more: If you're just getting started, check out my Git for Beginners – A Complete Getting Started Guide to learn the basics before diving into these intermediate-level concepts.

Final Thoughts

By now, you’ve covered all the major intermediate Git concepts that developers use every day. Keep practicing with your own or open-source projects. These skills will help you collaborate efficiently and maintain clean project histories.

Want more Laravel tips?

Visit LaravelDailyTips for practical guides, interview questions, and tricks.

Subscribe now and get battle-tested Laravel insights delivered to your inbox before anyone else!

0
Subscribe to my newsletter

Read articles from Laravel Daily tips directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Laravel Daily tips
Laravel Daily tips

As a FULL-Stack, TALL Stack developer, and owner of laraveldailytips.com, I am from Pakistan. My passion is to write short and useful tips and tricks that can assist other people who are trying to learn something new and helpful. Since the beginning, I have loved PHP, Laravel, VueJS, JavaScript, jQuery, and Bootstrap. I believe in hard work combined with consistency.