How to learn Git slowly.
Samuel-Zacharie FAURE

Samuel-Zacharie FAURE @samuelfaure

About: I love : Web development - Writing - Music // I value: Radical respect - Radical honesty

Location:
Paris
Joined:
Oct 28, 2019

How to learn Git slowly.

Publish Date: Jul 18 '21
1360 22

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.

Related XKCD:
image

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?
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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?
Enter fullscreen mode Exit fullscreen mode

Commands to know perfectly:

git merge
git branch
git checkout <branch>
git checkout -b
git blame <file>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Commands to know perfectly:

git rebase
git push --force-with-lease (DANGEROUS)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Commands to know perfectly:

git reflog
git-filter-branch
git-filter-repo
git bisect
Enter fullscreen mode Exit fullscreen mode

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!

Comments 22 total

  • Waylon Walker
    Waylon WalkerJul 18, 2021

    👏 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.

  • GrahamTheDev
    GrahamTheDevJul 18, 2021

    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!

  • Vivek Ranjan
    Vivek RanjanJul 18, 2021

    Great breakdown of different categories of commands in git! It took me a few years to fully understand it after daily usage

  • Bobby
    BobbyJul 19, 2021

    I love this separation into different levels!

    Here is a free opensource eBook that might be helpful for absolute beginners:

    github.com/bobbyiliev/introduction...

  • ecyrbe
    ecyrbeJul 19, 2021

    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

  • Raí B. Toffoletto
    Raí B. ToffolettoJul 19, 2021

    Great Roadmap!! I'm seeing a few gaps on my git knowledge through this... time to study now. 😊😊

  • velociwabbit
    velociwabbitJul 21, 2021

    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.

    • Samuel-Zacharie FAURE
      Samuel-Zacharie FAUREJul 22, 2021

      My favorite is git merge -X theirs/ours with the meaning of theirs/ours reversed when you rebase.

  • Stuart
    StuartJul 22, 2021

    Well done, Samuel, and thanks for sharing.

  • EvelinCHamp
    EvelinCHampJul 22, 2021

    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...

  • JDeep
    JDeepJul 22, 2021

    alias git lg="git log --all --decorate --oneline --graph"
    is a lifesaver. Normal git log is very messy to understand anything

  • Lars Ejaas
    Lars EjaasJul 22, 2021

    This article is really awesome! Thanks for doing this!

  • Anita Graham
    Anita GrahamJul 23, 2021

    I normally say "I have a difficult relationship with Git"

  • Michael
    MichaelJul 23, 2021

    Nice! I remember working on CVS at first and thought that was simple and easy, especially when automating build scripts, git brought that to a whole other level.

  • HaroldV
    HaroldVJul 25, 2021

    Excellent post Samuel congratulation this post is very useful, @samuelfaure you allow me translate this post to spanish ?

  • Harold Villalobos
    Harold VillalobosJul 25, 2021

    Hi Samuel congratulation for this post is very useful, I want to ask if you allow me translate this post to spanish ?

  • Manuel Ricci
    Manuel RicciJul 26, 2021

    Great article!

  • React Hunter
    React HunterAug 11, 2021

    Hope it includes sample commands for some cases, or proper tutorial link.
    Thanks

Add comment