DEV Community

Cover image for Git Gud: Learning Git to survive your first month 🚀
Jayant Bhawal
Jayant Bhawal

Posted on • Originally published at middlewarehq.com

Git Gud: Learning Git to survive your first month 🚀

Getting started with Git?

It's pretty easy, tbh.
It's literally as simple as git clone, git commit, git push...
Wait, I need to pull before I push?
Google says I should rebase or something.
Wait, where did my changes go?
Oh god I was supposed to ship this today!
OH SH*T WHAT IS HAPPENING?!?!?

I got your back.

This post covers the basics, and some 🍑 saving measires.


Basic Workflow: init, clone, status, add, commit, push, pull

Let’s get these out of the way fast. These are the bread and butter of Git. You’ll use them in almost every project:

  • git init: Start version control in a new project.
  • git clone: Copy an existing project to your machine.
  • git status: See what’s changed in your working directory.
  • git add: Stage changes to be committed.
  • git commit: Save those changes with a helpful message 📝.
  • git push: Send your changes to the remote repo (like GitHub) 🚀.
  • git pull: Fetch and merge updates from the remote repo into your local one.

This is your life for the next few weeks. Add a meme here of a person looking at their terminal, overwhelmed by all the commands 😅. Now, let’s kick things up a notch.

Image description


Going Deeper: log, reflog, merge, rebase, fetch, reset, cherry-pick, aliases

1. git log: Looking Back in Time

git log is more than just a history view—it's a window into the soul of your code 👀. Want to narrow things down?

  • Find by author: ```bash

git log --author="Your Name"


- **Check only specific files**: 
  ```bash


  git log -- file.js


Enter fullscreen mode Exit fullscreen mode
  • Check changes within a time frame: ```bash

git log --since="2 weeks ago"


- **Combine it all** for targeted queries:
  ```bash


  git log --author="Your Name" --since="2 weeks ago" --oneline --graph


Enter fullscreen mode Exit fullscreen mode

For complex histories, the --graph option is the best thing since sliced bread 🍞—it shows how branches and merges evolved. Insert a meme of a messy Git graph here for a laugh!

2. git reflog: Recover from the Unrecoverable

If Git were a video game, git reflog would be your “undo” cheat code 🎮. It tracks every change you make, even when git log can't help. Accidentally wiped out a commit with reset --hard? No worries.



git reflog
git reset --hard HEAD@{2}  # Go back to the state of the repo before the mistake


Enter fullscreen mode Exit fullscreen mode

Image description


3. git merge & git rebase: Navigating the Big Leagues

These are the strategies for bringing together different branches. Both are super powerful, but they serve different purposes:

  • git merge: Combines two branches and creates a merge commit. If you want to keep the full history intact and visible, merge is your friend. ```bash

git merge feature-branch


  Use `--no-ff` if you want to always create a merge commit, even when Git could fast-forward:
  ```bash


  git merge --no-ff feature-branch


Enter fullscreen mode Exit fullscreen mode
  • git rebase: Moves your branch on top of another, making the history cleaner. ```bash

git rebase main

  Want to polish your commits before merging? Try an **interactive** rebase:
  ```bash


  git rebase -i HEAD~3


Enter fullscreen mode Exit fullscreen mode

Image description

For the unaware, the internet feels pretty... strongly about merges and rebases. In case you didn't know, you will, soon. :D

Do share what's your preference in the comments. 👇


4. git fetch: See Without Touching

git fetch is like peeking into the fridge to see what's inside before deciding what to eat 🍕. It updates your local copy with changes from the remote, but doesn’t mess with your working branch yet.



git fetch origin


Enter fullscreen mode Exit fullscreen mode

Use it with git diff to see how your local branch compares:



git diff origin/main


Enter fullscreen mode Exit fullscreen mode

5. git push: Beyond the Basics

While git push origin main is the standard, here’s how you can take it further:

  • Push specific branches: ```bash

git push origin feature-branch


- **Push and set upstream** (perfect for new branches):
  ```bash


  git push -u origin feature-branch


Enter fullscreen mode Exit fullscreen mode
  • Force pushing when you absolutely need to: ```bash

git push --force


- **Push specific tags**:
  ```bash


  git push origin v1.0.0


Enter fullscreen mode Exit fullscreen mode

Need to delete a branch from the remote? Easy:



git push origin --delete feature-branch


Enter fullscreen mode Exit fullscreen mode

6. git reset: Correcting Mistakes

Time to fix those “oops” moments! git reset lets you move backward in time—without a time machine. Here are the different flavors:

  • Soft reset keeps your changes but removes the commit: ```bash

git reset --soft HEAD~1


- **Mixed reset** uncommits and unstages the changes:
  ```bash


  git reset HEAD~1


Enter fullscreen mode Exit fullscreen mode
  • Hard reset wipes out everything—gone, poof 💥: ```bash

git reset --hard HEAD~1


Pro tip: Run `git reflog` if you accidentally reset something important.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bfwepz8mm29pz79es7wt.png)


---

#### 7. `git cherry-pick`: Selective Commit Application

Need just *one* commit from another branch? `git cherry-pick` is your secret weapon 🍒. Grab the exact commit you need without merging the whole branch.

```bash


git cherry-pick <commit-hash>


Enter fullscreen mode Exit fullscreen mode

This is especially handy when you need a quick bug fix from another branch without bringing in all the extra work.


8. Git Aliases: Work Smarter, Not Harder

Why type out long commands when you can use aliases? Here's how to cut down your keystrokes:

  • gwip: Stash all changes with a "Work in Progress" commit: ```bash

git config --global alias.gwip "!git add -A && git commit -m 'WIP'"


- **`gunwip`**: Undo your last WIP commit and restore your working state:
  ```bash


  git config --global alias.gunwip "reset HEAD~1 --soft"


Enter fullscreen mode Exit fullscreen mode

And the classics:



git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit
git config --global alias.lg "log --oneline --graph --all"


Enter fullscreen mode Exit fullscreen mode

Image description

You, when you realized you didn't need to type those long-damn commands a thousand times repeatedly.


Final Thoughts

Git isn’t scary once you get the hang of it, and these advanced commands will help you control your projects like a boss 💪. Whether you’re rewinding time with reflog, surgically applying commits with cherry-pick, or streamlining your workflow with aliases, you’ve got the tools to not just survive, but master Git.

Happy coding! 😎


Hope this helps you Git Gud faster—and don’t forget to share your favorite Git memes along the way!

Top comments (5)

Collapse
 
thegrumpyopsdude profile image
JB • Edited

But commit-lint makes my life so much easier /s

Good article, I wish someone had presented this for me like this when I began.

Collapse
 
jayantbh profile image
Jayant Bhawal

Be the change you wish to see, @thegrumpyopsdude.
Share it with your juniors! 🧠

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great post! Well done!

Here is also a free eBook in case that anyone wants to learn more:

GitHub logo bobbyiliev / introduction-to-git-and-github-ebook

Free Introduction to Git and GitHub eBook

💡 Introduction to Git and GitHub

This is an open-source introduction to Git and GitHub guide that will help you learn the basics of version control and start using Git for your SysOps, DevOps, and Dev projects. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Git to track your code changes and collaborate with other members of your team or open source maintainers.

The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Git, GitHub and version control in general.

🚀 Download

To download a copy of the eBook use one of the following links:

📘 Chapters

Collapse
 
array_dot_reduce profile image
Bhaskar Ghale

🧠 Thanks for gwip and gunwip

Collapse
 
marcelo_lobatoharo_ea988 profile image
Marcelo Lobato Haro

Excellent. Thank you