DEV Community

loading...
Cover image for Confused by git?  Here's a git crash course to fix that 🎉

Confused by git? Here's a git crash course to fix that 🎉

Chris Achard on September 10, 2019

This was originally posted as a twitter thread: https://twitter.com/chrisachard/status/1171124289128554498 NOTE: if you are looking for a very bas...
pic
Editor guide
Collapse
tripol profile image
Ekanem

First time hearing about git log --oneline. A truly cleaner way to look at the history.

Thanks Chris

Collapse
nutriz profile image
Jérôme Gully

And there is much more... try
git log --all --decorate --oneline --graph :)

Collapse
chrisachard profile image
Chris Achard Author

Yes! there's a whole bunch :) Here's the full docs if you're interested: git-scm.com/docs/git-log (examples at the bottom)

Collapse
tripol profile image
Ekanem

Just did it! Thanks.

Now I feel like a 10x developer :)

Collapse
chrisachard profile image
Chris Achard Author

Glad it helped!

Collapse
jameesy profile image
Jamees Bedford

On fire with these recent posts Chris!! 🔥

Collapse
chrisachard profile image
Collapse
evolutionxbox profile image
Jonathan Cousins

The biggest misconception I find (which I couldn’t see listed here) is that a the history is a list of changes. Instead commits are snapshots of the entire repo, not just changes.

Collapse
chrisachard profile image
Chris Achard Author

That's a good point - git is unlike SVN in that way. SVN stores diffs (which is why it can take a long time to calculate the current state when your repo history gets really long), and git stores entire files. I always had thought that git stored diffs as well (since you "commit" just the change, right?) - but nope!

I didn't include it because I find that, in practice, it doesn't matter much whether you think of the commit log as being diffs or snapshots - but I could be wrong... have you found cases where it matters a lot which way you think of it? Thanks!

Collapse
evolutionxbox profile image
Jonathan Cousins

It does when people get comfortable with cherry picking.

Thread Thread
chrisachard profile image
Chris Achard Author

Ah, good point

Thread Thread
evolutionxbox profile image
Jonathan Cousins

You have a very helpful article. I would definitely not expect someone new to git to start cherry picking.

Thread Thread
chrisachard profile image
Chris Achard Author

Thanks! Yeah; maybe on a more advanced course sometime :)

Collapse
m_starca profile image
marko

What is command when changes should be added under pushed commit?

Collapse
chrisachard profile image
Chris Achard Author

If I understand correctly, what you want is to add the extra files to the staging area with:

$ git add FILE

and then you can add them to the most recent commit with

$ git commit --amend

Does that solve the issue?

Collapse
m_starca profile image
marko

Is this also working for remote last commit?
Thank you.

Thread Thread
chrisachard profile image
Chris Achard Author

If you want to change a remote commit, you'll have to do this, and then push with -f (which is a force push).

HOWEVER! Be careful with force push. If you accidentally force push to the wrong branch, then it can really mess you up, and if you have teammates who have already downloaded a public branch (like master), then force pushing to master isn't a good idea.

If you've already pushed to a public branch, the better choice is probably to just make a new commit.

Thread Thread
m_starca profile image
marko

I will keep that in mind. Thank you.

Collapse
papidev profile image
Papidev

Love it, so clear

Collapse
sebbdk profile image
Sebastian Vargr

Git is the expression used for an old angry man.

Either git good, or git’ out.

Either way someone is going to be frustrated.

Collapse
sebbdk profile image
Sebastian Vargr

Just realised i missed out on a final pun.

"Either way someone is going to git' frustrated" ...

Collapse
chrisachard profile image
Chris Achard Author

🤣

I love puns

Collapse
kj2whe profile image
Jason

I was really getting confused, till I realized I was seeing the word 'comment' instead of 'commit'. Now it makes much more sense! Great article.

Collapse
chrisachard profile image
Collapse
alpe89 profile image
Alberto Pertusi

Very good explanation, thanks :)

Collapse
arif98741 profile image
Ariful Islam

Pretty good and clear article no doubt. Those who always fail to memorize basic git commands, I make a datatable which name is gitcom. you can check from below link as well.
arif98741.github.io/gitcom/

Collapse
blair2004 profile image
Blair Jersyer

nice course...i'm however a bit confused about "stash".. first time i read this. What is that ?

Collapse
chrisachard profile image
Chris Achard Author

Thanks - and yeah, I'm realizing that I didn't explain stash... like at all in the post 🤦‍♂️ oops.

Stash is a temporary place you can put work in progress. Usually, the workflow goes something like this:

  • You are working on something in your working directory

  • a high priority bug comes in. To fix it, you have to switch branches and clear your working directory of the changes you already have made

  • instead of trying to save your work for later in a commit or a special branch, etc, you can put it in the "stash" with git stash

  • Then you go and fix the high priority bug

  • later, you can re-apply what was in the stash with git stash apply or git stash pop (pop will remove it from the stash; apply just brings it back over)

  • then you can continue to work on whatever you were working on from bullet 1.

Hope that helps a bit! I probably should have had a separate point for it in the guide 😀

Collapse
jasterix profile image
Jasterix

Great article! I also recommend the atlassian git docs, which helped me get comfortable with git

Collapse
hseritt profile image
Harlin Seritt

Good stuff, Chris. Thanks very much for posting this!

Collapse
alexfrompalmsprings profile image
AlexCodes

Thank you!! I am currently learning git

Collapse
chrisachard profile image
Chris Achard Author

Glad it helped!

Collapse
camblan profile image
Julien Camblan

The clearest git cheatseet, thank you! 🙏

Collapse
kaxi1993 profile image
Lasha Kakhidze

The major git features, explained so simply, thanks Chris!

Collapse
nutriz profile image
Jérôme Gully

Best git summary ! Clear, concise, simple illustrations. I already master all these commands, but I never seen a so summary, so thanks. I will spread my team with your post :)

Collapse
ogaston profile image
Omar Gaston Chalas

wow! git log --oneline is amazing

Collapse
khangprcvn profile image
khangprcvn

Wow, thank you.

Collapse
achargoy profile image
Chargoy

Thank you so much for this course, really amazing 👍.

Collapse
meierreoi profile image
Meierreoi

Just find something useful in this post, thanks.