Also available on my blog.
This post is dedicated to my wife who, despite being one of the smartest person I know, still sucks at Git
I've been mentoring webdev students for a while now. So I'm in prime position to see what mistakes are common amongst beginners.
I wrote a previous article about how to start learning CSS - a great read for any CSS beginner.
Now it's time to master Git. Git is... not easy to master.
Which is why beginners can get confused easily. And a mistake can cost a lot: no one wants to be that guy who deleted their peer's work.
The best way to learn Git is gradually. It can take quite some time before you're a real Git master.
This guide intend to make your journey easier by organizing your learning path in simple, digestible clear-cut steps. Be sure to master each level very well before switching to the next!
I - Basic solo use
These tools will allow you to use Git for your own usage. Don't bother with branches for now, just do everything on Main.
Concepts to understand perfectly:
- What is the difference between Git and Github?
- What is a commit?
- What is the staging phase?
- What is a branch?
- What's the remote repository VS local repository?
- How to set one or more upstream repository?
- How to commit?
- How to push / pull to an upstream repository?
Commands to know perfectly:
git init
git clone <repository>
git status
git add <file>
git add --all
git commit
git remote add
git remote set-url
git remote -v
git push <repository> <branch>
git pull <repository> <branch>
II - Basic tools
These tools will allow you to be more at ease with Git as a working tool. We will also need to learn a little bit of configuration.
Concepts to understand perfectly:
- The .gitignore file
- The .gitconfig file
- Seeing the commit log
- File manipulation with Reset, Clean, Checkout <file>, Rm
- Repo manipulation with the Stash
Commands to know perfectly:
git log (with and without --stat)
git checkout <file>
git reset <file> (DANGEROUS!)
git reset --hard (DANGEROUS!)
git clean -f (DANGEROUS!)
git rm <file> (DANGEROUS!)
git config --global user.name
git config --global user.email
git stash
git stash apply
git stash clear (Kinda dangerous)
III - Basic collaboration
This will allow you to start collaborating with other people. You need to master this level BEFORE any attempt at collaboration.
Concepts to understand perfectly:
- Branchs: What they are, why they exist, how to use them.
- Merging
- Conventions for branch naming
- How to write good commit messages
- What are forked repositories?
Commands to know perfectly:
git merge
git branch
git checkout <branch>
git checkout -b
git blame <file>
IV - Basic collaboration: Rebase & Pull requests
I put the whole "rebasing to the collaborative branch" apart, because it adds the first command that can damage your remote repository: git push --force-with-lease
.
Indeed, if you're rebasing your local branch, you will need to push with this option to your distant repository. So it's dangerous, but you still need to master this part if you collaborate in a team.
I also add pull requests here, because they're an important concept but they are more of a Github/Gitlab concept than really a Git concept.
Concepts to understand perfectly:
- Simple rebase (and how it differs from merging)
- What are pull requests?
- How to make a PR from branch to branch
- How to make a PR from a fork to the original repository
Commands to know perfectly:
git rebase
git push --force-with-lease (DANGEROUS)
V - Competent level
This level allows you to better organize your work history, organize your branches, and navigate in your history with ease.
Concepts to understand perfectly:
- How to rewrite your local history
- How to rebase interactively
- Branch management (prune, fetch)
- Use of HEAD notation or commit hashes
- Using Diff to compare commits
- How to revert a commit
Commands to know perfectly:
git commit --amend
git rebase -i
git prune
git fetch
git remote prune
git checkout HEAD/HEAD~1/<commit hash>
git diff <commit hash 1> <commit hash 2>
git revert <commit hash>
VI - Advanced level
At this level, you can fix when you or someone else fuck something up.
Concepts to understand perfectly:
- What is the reflog?
- How to clean sensitive data from the repository
- How to effectively hunt for bad commits
Commands to know perfectly:
git reflog
git-filter-branch
git-filter-repo
git bisect
As a great alternative to the git-filter commands, I advise BFG.
Conclusion
I hope this guide helped you navigate the treacherous route that is learning Git for the first time.
If I forgot an important command you're using regularly, be sure to tell me!
If you liked this, you can buy me a coffee! ☕
Happy coding!
Top comments (22)
I love this separation into different levels!
Here is a free opensource eBook that might be helpful for absolute beginners:
github.com/bobbyiliev/introduction...
👏 git is a weird mindshift that takes time to absorb, I love how you present different levels to master before moving to the next. There are so many learn git in 5 minute posts, this one is more realistic.
git has the worst function naming of any app i have ever encountered.
I have been coding and using git for many years and still make huge unforced errors.
Upstream vs downstream pull vs push they are all bass ackwards
there is a great video on smartereveryday that shows how hard it is to ride a bicycle when the steering is reversed (right turns left, left turns right)
It seems to me that Git functions are the software equivalent of a reversed steering bicycle.
My favorite is
git merge -X theirs/ours
with the meaning of theirs/ours reversed when you rebase.What you can’t master git in 7 minutes like all the articles tell us? 😜🤣
Shame this will get 20 likes as the advice here is great! Have a ❤️ and a 🦄 to try and get this in front the people who need to see it!
Hello,
Thank you for sharing.
For those who will try to find squash command. It's not standard, you can create an alias to mimick the behaviour.
If you just need to squash when merging you can do :
git merge --squash
Thank you so much ! I'll correct this.
Great Roadmap!! I'm seeing a few gaps on my git knowledge through this... time to study now. 😊😊
Great breakdown of different categories of commands in git! It took me a few years to fully understand it after daily usage
Hey, thanks, man. I’ve been in a slump about using it lately. I’ve been working on some sort of database but with links to various websites. I want to follow something simple like Coinbetz but not related to gambling or bitcoin. Something niche. coinbetz.com/blog/crypto-credit-ca...
alias git lg="git log --all --decorate --oneline --graph"
is a lifesaver. Normal
git log
is very messy to understand anythingThank you very much!
This article is really awesome! Thanks for doing this!