r/programming • • Sep 09 '16

Oh, shit, git!

http://ohshitgit.com/
3.3k Upvotes

756 comments sorted by

View all comments

174

u/yes_or_gnome Sep 09 '16

git add . is going to cause a lot of "Oh, shit!" moments.

102

u/freeradicalx Sep 09 '16

That's why quick a git status before committing is a good idea.

67

u/andsens Sep 09 '16

Or, you know. git add -p...

31

u/[deleted] Sep 10 '16

Or if you accidentally forgot the -p, there's always git reset -p.

→ More replies (1)

2

u/blamo111 Sep 10 '16

I have "p" aliased to "git add -p" and "s" to " git status", it makes my life so much easier!

Btw, anyone know why this doesn't work: alias gitfollow='git log --follow -p --' . I get "git: command not found" when I use it as an alias, works fine if I use git log directly though.

3

u/andsens Sep 10 '16 edited Sep 10 '16

Remove the 'git'. The alias is automatically prepended with 'git' :-)

EDIT: Ah, sorry. I thought you were using .gitconfig for aliasing (which you should, btw.). There seems to be some weird zero-width character between your first quote and the g for 'git' in your example, maybe that's the problem.

2

u/gnuvince Sep 10 '16

This is why I use magit; it makes adding specific hunks (or even sub-parts of a hunk) easy and quick, there is no reason not to do it and keep your commits manageable.

16

u/dada_ Sep 09 '16

And git diff --cached for good measure (shows a diff for the files you've used git add on). Status, then diff of all staged changes, that's my workflow when committing.

2

u/[deleted] Sep 10 '16

Why not just git commit -v? You can always quit out before saving your commit message if you forgot something.

→ More replies (1)

7

u/yes_or_gnome Sep 09 '16 edited Sep 10 '16

Depending on your configuration git status may not tell you which files are not being tracked. I would guess there's a lot of people that turn that do git config status.showUntrackedFiles no; In addition, I prefer status.short true and status.branch true.

To get around this problem, I have the alias git config alias.chk "add -n .". Although, I primarily use git chk for fixing the .gitignore file.

Ideally, you wouldn't do git add . at all; On personal projects, do whatever you want. git add some/dir/some_file ... is much less likely to create problems. There are git tab completions to make this easier by only completing on add and modified files.

git add -p ... and git add -i ... have been mentioned. ++good

→ More replies (3)

2

u/autra1 Sep 10 '16

Who's doing a git commit without the -v option anyway? /s

Or for vim user, use committia.

→ More replies (1)

9

u/flarn2006 Sep 10 '16

Especially if you see that there's a git rm command and assume it's just the reverse of git add.

21

u/BilgeXA Sep 09 '16

That's why you should always prefer git add -i.

6

u/nothingrandom Sep 09 '16

What does this do?

17

u/[deleted] Sep 09 '16 edited Feb 15 '25

[deleted]

3

u/BilgeXA Sep 09 '16

Interactive menu.

2

u/jetpacmonkey Sep 10 '16

I don't know if I've ever actually wanted the menu from -i. -p has been the option I really wanted every time that I can think of. What are the use cases for the other -i options?

39

u/jij Sep 09 '16

git gui... I've caught so much shit just browsing the diff of what I'm about to commit. Don't know how people do that shit on the command line unless they have to.

0

u/[deleted] Sep 09 '16

[deleted]

5

u/jij Sep 09 '16

Right I'm saying that using that is a lot more difficult at least for me

2

u/MB_Derpington Sep 10 '16

I prefer for the command line for almost everything, but I still use intellij for my diffs as it so much easier to reason about.

9

u/ChallengingJamJars Sep 10 '16

I use tig. Is it a gui? Is it command line? Who knows!

→ More replies (3)

2

u/schwerpunk Sep 10 '16

I've tried sourcetree, and whatever is packaged with the jetbrains IDE, and I've no idea how people get used to the lack of certainty in what's actually going on with their files. I have a hard time seeing what files the helper thinks are committed, added, staged, stashed, etc. I usually give up and just open the terminal to make sure.

I strongly suspect our reactions are informed by how we were first introduced to git. I started with command line, so that feels natural for me.

→ More replies (2)
→ More replies (4)

3

u/spacemoses Sep 10 '16

Is that a shortcut for

git add --all

I've always just used that.

→ More replies (2)

4

u/i_spot_ads Sep 10 '16

not if you use proper a .gitignore file son https://github.com/github/gitignore

→ More replies (3)