DEV Community

Cover image for 6 Git commands every beginner should memorize

6 Git commands every beginner should memorize

Jeremy Schuurmans on November 08, 2019

For people new to Git, it can be confusing and intimidating. If that's you, here are six Git commands for your toolbox. With these, you can become ...
Collapse
 
kevinhch profile image
Kevin

Don't forget about:

git push origin master --force
:)
Collapse
 
eromanowski profile image
Eric • Edited

and

git pull origin master
git reset --hard
git stash
git stash pop

merging into master should be done via pull request imho

Collapse
 
perpetualwar profile image
Srđan Međo

I appreciate the joke :)

Collapse
 
exadra37 profile image
Paulo Renato

You can appreciate the joke as an experienced developer in git, that knows the implications of using it, but we need to be careful when trying to make jokes for the ones that don't get the context of it, specially when the article is targeting beginners.

Thread Thread
 
jeremy profile image
Jeremy Schuurmans

I thought it was pretty funny myself, but you also make a good point. Thank you for bringing it up!

Thread Thread
 
perpetualwar profile image
Srđan Međo

I agree, hence my acknowledgement of the joke for what it is.

Collapse
 
exadra37 profile image
Paulo Renato

This is a very bad advice, and should be never used, not even in solo projects, because you get into a bad habit that can have pretty nasty consequences in a professional environment, like Jenkin developers accidentally do "git push --force" to over 150 repos on github.

I don't have the link anymore, but I read a story once of a company that took months to recover from a git push --force to their master branch.

To be on the safe side ALWAYS configure your Github, Gitlab or whatever you use to not accept push to master.

Collapse
 
mandaputtra profile image
Manda Putra
git push upstream master --force

Cause you are on forked repo

Collapse
 
auroraskye profile image
Aurora Skye

Trololololol...

Collapse
 
exadra37 profile image
Paulo Renato • Edited

You forgot the most important of all git checkout -:

╭─exadra37@approov ~/Developer/Approov2/blog/shipfast-on-docker  ‹approov2-new-rebased› 
╰─➤  git checkout -
Switched to branch 'approov2-new'
╭─exadra37@approov ~/Developer/Approov2/blog/shipfast-on-docker  ‹approov2-new› 
╰─➤  git checkout -
Switched to branch 'approov2-new-rebased'

It's just like our old Linux friend cd - ;)

Ok, the above one is more a tip, but a very important command to know about for beginners is git stash --help and git show --help

With git stash we can save our changes that are not commited yet, and if we include -u we will save also the files not tracked. It's useful to clear the workspace or to move code not committed yet to another branch or just to later reuse on the same branch, just by doing git stash apply.

With git show we can see the git diff for the last commit, and with git show <commit-hash> or git show HEAD~3 we see the git diff for that specific point in history.

When things go terrible wrong, like you delete accidentally a branch, you can use git reflog --help to see your history of changes and get back to a good state.

Collapse
 
axadream profile image
Alex Pintea

Very useful tips.Thanks a lot, especially for the idea to use git stash apply

Collapse
 
jeremy profile image
Jeremy Schuurmans

Agreed! These are awesome tips. Thank you!

Collapse
 
jackogwello profile image
Jack Ogwello

git 2.23 introduced git switch for switching branches.

More info is available here

Collapse
 
auscompgeek profile image
David Vo

You mention you could shorten git checkout to git co, but you never explain how to configure that alias.

Collapse
 
jeremy profile image
Jeremy Schuurmans

Good catch! Thank you -- it completely slipped my mind, which is unfortunate. I just edited the post with instructions on how to do it.

Collapse
 
codebeautify profile image
Code Beautify

Extend it to 10 and add git rebase, cherry-pick, push, fetch

Collapse
 
mattvb91 profile image
Matthias von Bargen

I feel like git clean would be a good one toio

Collapse
 
jeremy profile image
Jeremy Schuurmans

Thanks to everyone in this thread for the responses! These are some really great tips.