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:
  git log --author="Your Name"
Enter fullscreen mode Exit fullscreen mode
  • Check only specific files:
  git log -- file.js
Enter fullscreen mode Exit fullscreen mode
  • Check changes within a time frame:
  git log --since="2 weeks ago"
Enter fullscreen mode Exit fullscreen mode
  • Combine it all for targeted queries:
  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.
  git merge feature-branch
Enter fullscreen mode Exit fullscreen mode

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

  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.
  git rebase main
Enter fullscreen mode Exit fullscreen mode

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

  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:
  git push origin feature-branch
Enter fullscreen mode Exit fullscreen mode
  • Push and set upstream (perfect for new branches):
  git push -u origin feature-branch
Enter fullscreen mode Exit fullscreen mode
  • Force pushing when you absolutely need to:
  git push --force
Enter fullscreen mode Exit fullscreen mode
  • Push specific tags:
  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:
  git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode
  • Mixed reset uncommits and unstages the changes:
  git reset HEAD~1
Enter fullscreen mode Exit fullscreen mode
  • Hard reset wipes out everythingโ€”gone, poof ๐Ÿ’ฅ:
  git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode

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

Image description


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.

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:
  git config --global alias.gwip "!git add -A && git commit -m 'WIP'"
Enter fullscreen mode Exit fullscreen mode
  • gunwip: Undo your last WIP commit and restore your working state:
  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