DEV Community

Cover image for Git cheatsheet for beginners

Git cheatsheet for beginners

Duomly on January 08, 2020

This article was originally published at Git cheatsheet Git is a very useful skill to have and almost necessary in many companies. Browsing job...
Collapse
 
kriska profile image
Kristina Gocheva

I would like to disagree that writing git commands in the command line is hard. On the contrary, using commands is as easy as ABC especially when you have the "cheatsheet". If you want to understand git, use the command line and start to visualize the process in your head, not with UI.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

While GUI might be easier, it does have learning curve as well.

I really recommend simply -- VSCode, PyCharm, IntelliJ, Android Studio, or WebStorm... That is, your GUI-based Text Editor / IDE.

Collapse
 
duomly profile image
Duomly

I do command line mostly as well, but for beginners it can be a bit more difficult than just use GUI.

Collapse
 
jeansmaug profile image
jean-smaug

"head is the most recent commit of the repository you work with."

Even if it's for beginner and you need simplification. It's far from what HEAD really is.

Maybe you can forget what it is for beginners or you can just say "It's where you are currently". ;)

Collapse
 
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan • Edited

It's where you are currently

Yup!

And on that note, in my personal experience, commits and branching become much easier to understand and visualize once you think of your commit history as a linked list, and HEAD as a pointer that you can position at will.

In fact, this is hinted at in the syntax of git log: HEAD -> master, where HEAD is quite literally pointing to master (assuming that's where we are atm).

Also, I agree: We shouldn't make things simpler for beginners, even if it means they may initially be confused. It's part of the learning process!

Collapse
 
jeansmaug profile image
jean-smaug • Edited

IMHO the best way to demystify what HEAD is, is to go inside the .git folder.
After that you go to the refs folder and you can explain the branches ;)

head-ref

Personally I understood many things by watching inside this .git folder.

I didn't mention that, but your article is nice. I will send it to my students :)

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

I wrote this one about 1 years ago now -- github.com/patarapolw/SimpleGit

Actually, I have tried GitHub Desktop before, but I uninstalled.

Now I using only VSCode (Visual Studio Code). I can help with some GUI. People could probably do the same in PyCharm or WebStorm or IntelliJ-based.

Collapse
 
thefluxapex profile image
Ian Pride • Edited

Nice, useful article.
Cheat sheets are not just for beginners even if that is their focus. It's always a good idea to have quick references. Especially, with my failing memory as I get older & all the different languages I know with various syntax it's hard for me to keep together. What I find for me is I don't try to remember every little detail right away; I keep quick referencing until I know it so well I no longer need to unless I haven't done it in a few years.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

I have one important problem on Git CLI on macOS...

How do I change the default text editor? I am too bad at vi. Wanna change it to Nano or Code or something.

Otherwise, I will need vi cheatsheet as well, even when productivity doesn't matter.

Collapse
 
duomly profile image
Duomly

You can setup default editor in the .bash_profile or .zshrc file in your users profile :)

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

Thanks, found it. A better way, actually.

git config --global core.editor "nano"

And, git config --global --edit, of course.

Collapse
 
m1well profile image
Michael Wellner • Edited

Great one - i would recommend to add the word „locally“ at the description for the branch delete command with the small „-d“.

Collapse
 
slavius profile image
Slavius

While this may be "very basic" introduction, it is confusing. Nothing is explained in detail on how things work and relate to each other.
After reading this article you might be able to manage a single repository of configuration files on your own computer, that's it. For a software developer part of a team this is simply not even a start.

In a company with a team of SW developers you:

  • use git flow to work on features and bugs with many branches like: features, develop, release, hotfixes in addition to master
  • amend commits to update it
  • work with remotes to fetch/push/pull your code
  • work with tags for versioning
  • use .gitignore to exclude files/folders from index
  • merge or rebase changes onto different branches while resolving conflicts in the process
  • as a maintainer merge or cherry-pick commits from pull requests
  • use submodules for dependencies and checkout recursively, committing to superproject as well
  • use pre- and post- hooks to integrate with CICD pipeline
  • stash/shelve push/pop to save uncommitted changes while switching branches
  • using bisect to find bugs
  • using blame to find changes/bugs
  • using git reset/clean to revert mistakes
  • etc., etc. Each of these operations changes in some way your local or remote repository or some of the refs or files in working tree or your index. Make a mistake and you either lose your important changes or overwrite someone else's code with its history making it disappear forever. (unless you're lucky and can extract commits using git's reflog).

I just named about 30 new terms you need to know how to use, know all of their command line parameters and be aware what changes they do to your refs, files, index, branch or repository.

Confused? No wonder. Git has so many commands, terms and parameters that you can generate random legit looking git man pages without noticing that they are nonsense jokes. Just have a look at this "Git random man page generator" git-man-page-generator.lokaltog.net/

Collapse
 
yuribenjamin profile image
Ibrahim Ragab

Thanks for doing this! I was about start writing something similar. I want add to GUI list something useful I used to work with every day Git Graph extension

Collapse
 
evanhalley profile image
Evan

I've been using Git for about 10 years and started using GUIs early on. I started going command line only as of 6 months ago and love it because you really learn the underpinnings of Git which is abstracted away by most Git GUIs.

Great article.

Collapse
 
akshayvijapur profile image
Akshay Vijapur

Very use full. Thanks for writing.

Collapse
 
rnrnshn profile image
Olimpio

Thanks for the cheatsheet but what theme is in the header cover?

Collapse
 
duomly profile image
Duomly

Some modified monokai or flat material.

Collapse
 
mayronceccon profile image
Mayron Ceccon

I have been using gitk!

Collapse
 
bersbersbers profile image
bersbersbers

I stopped reading here:
"repository keeps all your project's files, including commits and branches.

branch is a copy of the repository ..."

So the repository holds copies of itself?

Collapse
 
duomly profile image
Duomly

I would say yes, without going deep into "how git works inside", when you create a new branch, you create a new version, something like copy of code from the branch which is the parent for your new branch.

Collapse
 
bersbersbers profile image
bersbersbers

A copy of a branch, yes. But not a copy of the whole repository (which would be impossible, since copying the whole repository into itself would be subject to recursion).

en.wikipedia.org/wiki/Russell%27s_...

Collapse
 
joehobot profile image
Joe Hobot

I actually like using VSC with Git Lense etc - vs like sourcetree etc..

Collapse
 
brok3ndev profile image
Darion Suggs

This is great

Collapse
 
vaibhavkhulbe profile image
Vaibhav Khulbe

Great one. This will certainly help beginners!

Collapse
 
endormi profile image
endormi • Edited

If someone is interested, I automated some of the git commands on my automation repository and this is great!