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.
Not ๐ฉ, here's how to write actually good commit messages (hint: It's not just adding commit-lint)
Jayant Bhawal for Middleware ใป Jun 6
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"
- Check only specific files:
git log -- file.js
- Check changes within a time frame:
git log --since="2 weeks ago"
- Combine it all for targeted queries:
git log --author="Your Name" --since="2 weeks ago" --oneline --graph
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
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
Use --no-ff
if you want to always create a merge commit, even when Git could fast-forward:
git merge --no-ff feature-branch
-
git rebase
: Moves your branch on top of another, making the history cleaner.
git rebase main
Want to polish your commits before merging? Try an interactive rebase:
git rebase -i HEAD~3
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
Use it with git diff
to see how your local branch compares:
git diff origin/main
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
- Push and set upstream (perfect for new branches):
git push -u origin feature-branch
- Force pushing when you absolutely need to:
git push --force
- Push specific tags:
git push origin v1.0.0
Need to delete a branch from the remote? Easy:
git push origin --delete feature-branch
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
- Mixed reset uncommits and unstages the changes:
git reset HEAD~1
- Hard reset wipes out everythingโgone, poof ๐ฅ:
git reset --hard HEAD~1
Pro tip: Run git reflog
if you accidentally reset something important.
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>
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'"
-
gunwip
: Undo your last WIP commit and restore your working state:
git config --global alias.gunwip "reset HEAD~1 --soft"
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"
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)
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.
Be the change you wish to see, @thegrumpyopsdude.
Share it with your juniors! ๐ง
Great post! Well done!
Here is also a free eBook in case that anyone wants to learn more:
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:
Dark mode
Light mode
ePub
๐ Chapters
๐ง Thanks for
gwip
andgunwip
Excellent. Thank you