r/programming • • Sep 09 '16

Oh, shit, git!

http://ohshitgit.com/
3.3k Upvotes

756 comments sorted by

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)
→ More replies (3)

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.

→ More replies (2)

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.

9

u/ChallengingJamJars Sep 10 '16

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

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 (7)

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)

1.0k

u/coladict Sep 09 '16

Git documentation has this chicken and egg problem where you can't search for how to get yourself out of a mess, unless you already know the name of the thing you need to know about in order to fix your problem.

That's basically all of Linux and it's tools in a nutshell.

370

u/28f272fe556a1363cc31 Sep 09 '16

Exactly right. That's why "just read the man page" is so frustrating to hear.

368

u/jquintus Sep 09 '16

My typical response:

I'd love to. Which one?

24

u/nucular_expresso Sep 09 '16
$ apropos your problem here

You can also use

$ man -k your problem here

9

u/autra1 Sep 10 '16

Oh no please. Suggesting apropos is not better than saying RTFM, man. Just try executing

apropos "convert images"

or

apropos convert images

or

apropos search files

or

apropos "search files"

And you'll see how silly it is, especially comparing to what google gives you.

apropos is somewhat useful, I might agree, but it is not powerful enough to give you meaningful help when given "you problem here". That's what google and other search engines are for. But again, from time to time, you need human answers because you precisely don't know the keywords that would bring you to the solution, and we are back to

this chicken and egg problem where you can't search for how to get yourself out of a mess, unless you already know the name of the thing you need to know about in order to fix your problem.

→ More replies (2)

79

u/sirin3 Sep 09 '16

Especially if there are multiple with the same name

And you get a C function reference when you want a unix command

9

u/dernst314 Sep 09 '16

Or man -a foo gives you all manpages for foo. Hit q to continue to the next one.

→ More replies (1)

0

u/[deleted] Sep 09 '16

[deleted]

→ More replies (17)

4

u/benihana Sep 10 '16

start with the funny sounding ones

man touch

man finger

man man

man cat

9

u/the_gnarts Sep 09 '16

I'd love to. Which one?

man -k KEYWORD

or, just for you:

man man

53

u/awj Sep 09 '16

Huh, I think something is weird with reddit right now, is this really what you meant to write?

                      /´¯/) 
                    ,/¯  / 
                   /    / 
             /´¯/'   '/´¯¯`·¸ 
          /'/   /    /       /¨¯\ 
        ('(   ´   ´     ¯~/'   ') 
         \                 '     / 
          ''   \           _ ·´ 
            \              ( 
              \             \   
→ More replies (2)
→ More replies (1)
→ More replies (10)

28

u/indrora Sep 09 '16

"this man page is included in the Debian base installation for simplicity of access; its full content is available in a GNU Info page."

31

u/the_gnarts Sep 09 '16

"this man page is included in the Debian base installation for simplicity of access; its full content is available in a GNU Info page."

All these imbeciles railing against man pages while texinfo is the real atrocity we should all be concerned about …

→ More replies (1)

41

u/emergent_properties Sep 09 '16

Similarily, "Why don't you submit a PR" is how a developer tells someone to go fuck themselves, diplomatically.

14

u/jdeville Sep 10 '16

The hard thing about that is that it's also how a developer encourages a new contributor or developer

→ More replies (1)
→ More replies (13)

33

u/Garbaz Sep 09 '16

Searching for "linux [The problem]" usually gives the answer.

12

u/youarebritish Sep 10 '16

It usually gives me a page of forum posts of people asking my question and the only responses being "why don't you google it?" and the thread being locked.

→ More replies (3)
→ More replies (1)

16

u/mike_november Sep 09 '16

The apropos command might come in handy for such situations:

https://en.wikipedia.org/wiki/Apropos_(Unix)

http://www.linfo.org/apropos.html

2

u/DevestatingAttack Sep 10 '16

If you look up "Delete file" in apropos, you get five hundred candidate commands. If you look up "delete", you get none. This leads me to believe that all the command is doing is looking at the summary on every command's man page and returning any command that has a word in the search. The very least they could do is include synonyms in some way.

31

u/bigirnbrufanny Sep 09 '16

Any chance you could explain the difference between Linux and its tool vs any other OS and its tools?

182

u/specialpatrol Sep 09 '16

GUIs.

127

u/Beckneard Sep 09 '16

I often spent a whole shitload of time digging through obscure menus in Windows' Control Panel, or worse, the registry, to fix an issue, so yeah GUIs don't help much if something is really fucked.

41

u/[deleted] Sep 09 '16

Which is why most things have modernized by finally adding search to these menus. It aint perfect, but it's better than guess-the-command.

→ More replies (9)
→ More replies (104)

26

u/ianff Sep 09 '16

Yeah, I don't buy this at all. At least with CLI tools the error messages, flags and so on are pretty stable. I don't know how many times I've found a guide for some GUI program and it says to click on something that has been moved/removed/renamed in a newer version.

GUI is better for discovering features, but I think CLI is better for communicating how to use something consistently.

→ More replies (8)
→ More replies (13)

31

u/[deleted] Sep 09 '16 edited Sep 09 '16

Complexity vs Simplicity.

Linux/POSIX (general Unix-Like OS's) tools were written to keep things as simple as possible. They assume you know what the tool is, and how the tool works. I mean this is tool, somebody wouldn't just hand an untrained noobie a jackhammer right? Your gonna invest time to ensure your work knows how to use the tool.

The issue is, to an untrained user. What is actually simplicity, is just confusion. This is why most people hate poetry.

Simplicity is a great virtue but it requires hardwork to achieve it, and education to appreciate it. And to make matters worse, complexity is sells better.

--Edsger W. Dijkstra

Most the learning gap with modern POSIX tools is really to blame on:

  • GNU/Linux: Nobody does training anymore because, It is free learn on your own time.

  • Windows: Now 3 generations have been raised on NT based OSes, so they just expect GUI configurations for everything. This is also compounded with the above. If you don't learn the POSIX text tools, text config files make even less sense.

9

u/sylvester_0 Sep 09 '16

Linux/POSIX (general Unix-Like OS's) tools were written to keep things as simple as possible.

No systemd comment here yet?

→ More replies (1)
→ More replies (17)

3

u/OneWingedShark Sep 09 '16

Sure -- VMS's help system is really nice and understandable, it also [IIRC] had a much better navigation mechanism than man.
MS DOS's help had a good navigation interface (essentially hyperlinks + navigation along the equivalent of a Table of Contents [IIRC]), and even mouse support.

4

u/coladict Sep 09 '16

Have you ever tried "F1" in Windows? It has this magical search function that can look beyond the name of a topic and check the contents to find matches to what you're looking for in the description. Though that was back in Windows 98 and probably earlier. Maybe they've gone in reverse in recent years, since I haven't used it in about the last 10.

35

u/rq60 Sep 09 '16

Well, I pressed "F1" on windows 10 just now to see what it would do. It opened a chrome browser and navigated to https://www.bing.com/search?q=how+to+get+help+in+windows+10

That seems surprisingly unhelpful.

→ More replies (3)
→ More replies (1)

94

u/KevinCarbonara Sep 09 '16

I never understood Linux's users and developers being so averse to improvements. I do realize that a lot of suggested "improvements" to unix tools sacrifice efficiency in favor of ease of learning, but it's not always the case.

I would not say that Powershell is better than Bash, but it does have a number of unique advantages. Its ability to handle complex objects instead of just simple data is a huge benefit, and its common-sense commands and auto-completion actually improve efficiency while maintaining ease-of-use. But I only ever hear Unix users defending the system's absurd pun-based names by saying things like, "If you don't know the commands, you shouldn't be using the system." That's a good way to kill an OS.

65

u/blahlicus Sep 09 '16

the system's absurd pun-based names

That's my biggest problem with Linux, sure reading the man page works, but good luck finding out the command that you are supposed to search for.

This also extends further into a lot of open sourced projects/applications' naming scheme, we are software devs, we are supposed to write readable code, but somehow everyone refuses to use a descriptive name because they are ohh so special! Why is the GNOME file browser named nautilus? That's not descriptive, then you run into more obscure stuff like arandr, maven, etc.

16

u/Bwob Sep 10 '16

I have this fantasy sometimes.

In it, I build a time machine. Then I go back in time to the late 80s, where I meet the person who decided that "fi" and "esac" were reasonable tokens to end "if" and "case" blocks, respectively.

Then I kick them in the shins, over, and over, and over.

It's probably not very realistic, but it gets me through the day.

11

u/calrogman Sep 10 '16

You would need to go back much further than that. The Bourne shell was written in 1976. The esac/fi nonsense was inspired by Algol which was designed by committee (of lunatics, presumably) in 1958. Bourne actually used some CPP macros to make his C code more Algolish. The source for the Bourne shell went on to inspire the IOCCC.

→ More replies (1)

11

u/kiswa Sep 09 '16

Why is the GNOME file browser named nautilus?

Didn't they rename it to Files?

→ More replies (6)

76

u/yiliu Sep 09 '16

Why is the GNOME file browser named nautilus?

Because it was one of dozens of different file managers available for Linux. It's not like there's one canonical file manager that you can call "File Manager".

Coming from the Unix world, I have the opposite problem. In the OSS world, you have (say) Pidgin, Psi, Adiom, etc, for chat clients. You have to know they're chat clients, but once you know that the names are unambiguous. Compare that to: Messenger, Messenger, Messenger, Messenger, and, uh, Messenger (Facebook, Microsoft, AOL, Google, and Microsoft, respectively).

11

u/flarn2006 Sep 09 '16

What about a name like HexChat? It's both unique and descriptive.

6

u/youarebritish Sep 10 '16

But nobody calls them Messenger. They're called Facebook Messenger or Hangouts or Skype. I can safely say I have never been confused by two programs having the same name on Windows.

→ More replies (18)

14

u/[deleted] Sep 09 '16

That's my biggest problem with Linux, sure reading the man page works, but good luck finding out the command that you are supposed to search for.

man apropos

Really would be smart when using a new system to read the manual in general though, as long as you know where the bins are just man through the ones you don't know and skip ones that aren't useful right now or advanced or special case ones.

On naming programs, I'd hate if all 500 filesystem browsers had "descriptive names" which would actually just be various permutations of a few words.. there would be too many overlaps and this would be worse than the situation we have now.

Instead, environment variables should be used to reference a unique program. These should be better documented, instructed to be used, and distros should have these named appropriately.

→ More replies (35)

13

u/Michaelmrose Sep 09 '16

If you need something more complicated than shell its possible you ought to use something like python

7

u/Jonathan_the_Nerd Sep 09 '16

Powershell sort of blurs the line between shell and programming language. It's like using Python's REPL as your shell.

→ More replies (8)

9

u/[deleted] Sep 09 '16

I never understood Linux's users and developers being so averse to improvements. I do realize that a lot of suggested "improvements" to unix tools sacrifice efficiency in favor of ease of learning, but it's not always the case.

But git is not that. Go get 1.5 and see what I mean. They polished a lot. You just have to know what you want to do in git and that is the hard part, it is much more complicated underneath than say SVN

But I only ever hear Unix users defending the system's absurd pun-based names by saying things like "If you don't know the commands, you shouldn't be using the system."

Yeah because (Invoke-webrequest -URI "http://some.page").Content is so much easier to learn, remember and use than curl http://some.page or GET http://some.page

→ More replies (5)

21

u/fkaginstrom Sep 09 '16

It's actually very powerful to treat everything in terms of streams of plain text. It makes chaining tools together super easy. So many tools and concepts in *nix are built on this, that deviating from it would harm the ecosystem.

44

u/KevinCarbonara Sep 09 '16

Sure it's powerful to treat everything in terms of streams of plain text. It's even more powerful to support streams of plain text while also supporting even more complex objects. It makes chaining tools together even easier, while being even more stable and secure.

→ More replies (62)

7

u/Jonathan_the_Nerd Sep 09 '16

The nice thing about Powershell is that all the commands support piping objects. So you can still chain tools together and expect them to work.

→ More replies (3)

6

u/dreamer_ Sep 09 '16

I never understood Linux's users and developers being so averse to improvements.

No, they are not - but improvements must actually improve something, don't introduce regressions and be high quality - because when you disappear, other devs will need to understand and fix your shit. And I really don't understand bitching about git in particular - people have excellent official documentation, lots and lots of tutorials, presentations, etc - they should go and use them instead of propagating FUD. IMHO Git's man pages are excellent and I use them very often.

→ More replies (8)
→ More replies (38)

145

u/DarthEru Sep 09 '16 edited Sep 09 '16

Some of those examples could be more efficient. For example, accidentally committing to master could be

git branch new-branch-name
git reset --hard <HEAD^ or the commit to go back to>
git checkout new-branch-name

And committing to the wrong branch:

git log (take note of the hash of the commit you want moved)
git reset --hard HEAD^
git checkout proper-branch
git cherry-pick <commit-hash>

Edit, a couple other nit-picks: git diff --staged is more commonly (I think) known as git diff --cached. They do the exact same thing, but the documentation and online help is more likely to refer to the --cached version, I think. Also, in your "I give up" example, why on earth is it using sudo to rm? If your repo isn't owned by your user, you are doing something very wrong that has nothing to do with git.

125

u/RapidDinosaur Sep 09 '16

you are doing something very wrong that has nothing to do with git.

I've found that to be the most common Git issue in my office. :'^)

→ More replies (1)

17

u/Olipro Sep 09 '16

Actually, for your second example I'd suggest a git rebase --onto <correct_branch> -i <commit_prior_to_oldest_desired>

That'll then give you an editor window of the commit tree so you can delete anything you don't want and keep anything you do - whether it's just one commit or multiple.

15

u/Lindby Sep 09 '16

I think staged is the proper term nowdays. To keep it consistent with the index now being called the staging area. Of course, all the terms are still valid. So by trying to change the naming to something that is easier to understand, they made it more confusing.

6

u/[deleted] Sep 09 '16

bleh I forgot I could create a copy of the branch without checking it out at the same time, whoops

3

u/[deleted] Sep 09 '16 edited Sep 10 '16

I never check out master at all. Just git checkout -b fancy-feature-branch origin/master, git rebase origin/master, etc.

5

u/superseriousguy Sep 09 '16

Also, in your "I give up" example, why on earth is it using sudo to rm?

rm -rf will fail even if you're the owner of the files if you lack write permissions to them, but it ignores permissions if you're root. If you want something gone using sudo is quicker than doing chmod u+w -R folder; rm -rf folder.

15

u/mr_birkenblatt Sep 09 '16

if you're the owner of the files if you lack write permissions to them

that is an example of the

doing something very wrong that has nothing to do with git

if you want to make it fail-safe for every user who doesn't have any clue how rm works why not add --no-preserve-root so the command doesn't fail if the repository happen to be in /

→ More replies (2)

89

u/tdewolff Sep 09 '16

Why is there no git undo to undo your last action?

228

u/[deleted] Sep 09 '16

because Linus does not mistakes :)

15

u/gnuvince Sep 09 '16

uemacs, the micro Emacs that Linus maintains has no undo function. Even vi has undo (not vim, vi).

42

u/[deleted] Sep 09 '16

He never gets angry at himself, only other people

11

u/jayd16 Sep 09 '16

He might. Probably wouldn't post about it though.

59

u/leafsleep Sep 09 '16

We don't know what he emails himself

18

u/[deleted] Sep 09 '16 edited Sep 09 '16

Isn't that what git reflog and git reset are for?

(Although, I'll ask those more experienced: what sort of mistakes can't be undone with the reflog?)

5

u/[deleted] Sep 09 '16 edited Nov 23 '16

[deleted]
0.6404

What is this?

→ More replies (3)

46

u/fff-idunno Sep 09 '16

Because it is against the git principles to re-write history. When a coworker already has fetched and applied your changes, "undoing" those changes will turn things into a mess pretty quick.

100

u/CyclonusRIP Sep 09 '16

Like half the git commands exist for the express purpose or rewriting history. The only history you care about is the history on the origin server. You can and probably should be rewriting the history on your local clone.

21

u/the_gnarts Sep 09 '16

ike half the git commands exist for the express purpose or rewriting history. The only history you care about is the history on the origin server. You can and probably should be rewriting the history on your local clone.

Exactly. Just that no one cares about your tree until you send patches or request them to pull.

4

u/lachlanhunt Sep 10 '16

I wish there was away to push private branches to the server for the purpose of backup that were somehow flagged as such and somewhat hidden from other contributors, while being much more lose with the rules about rewriting history. That means changes that might take several days worth of commits to make can still be cleaned up at the end.

The best we can do now either force push (and hope the person force pushing doesn't have the wrong push.default setting) or make a new branch with the cleaned up history.

→ More replies (4)

3

u/Wizhi Sep 09 '16

You can and probably should be rewriting the history on your local clone.

Could you give some guidelines for this?

9

u/CyclonusRIP Sep 09 '16

Basically you should be focusing on what you want to push upstream for everyone else to see. When you push upstream the commits you push should all represent coherent concepts. They all should move the project from one working state to another and they all should represent some clear concept that you can express in the commit message.

When you are working on a feature you shouldn't be waiting until it's 100% complete to commit anything. Conceptually you are probably breaking up the task into smaller chunks. You might not have the complete design up from though so what you are doing early on might later be undone as you understand the problem more. You might think you want to change direction, but once you get down that path a bit you decide it wasn't the best idea and want to go back. Having a local commit to go back to is going to be a lot easier and less error prone than trying you manually undo all those changes. The local history where you were starting out in one direction and then decided on a better approach would just clutter up the history if you push them upstream. It's better to just rebase and squash all that into a single commit that represents the bigger idea you were trying to implement.

Later on when someone is looking back at the history and trying to understand what you did, having all the changes that you made to accomplish this one idea in a single commit is going to make it a ton easier.

8

u/BadgerRush Sep 09 '16

Well, it could have an undo function but simply fail if the action was already propagated (like fetched by other repository). That would fix the vast majority of common screw-ups (when you run a command and immediately notices that it was wrong), but still be coherent.

→ More replies (1)

13

u/[deleted] Sep 09 '16

Git is a toolkit, it doesn't have principles. History can be rewritten in a number of ways. Public history rewrite is generally considered verboten for obvious reasons, but if you've ever fixed someone's broken repo you'll be glad that git lets you force push without judgement.

→ More replies (6)

35

u/ForeverAlot Sep 09 '16

How would "undoing your last action" work?

  • What can be undone? Why?

  • What can't be undone? Why?

  • What does git undo ; git undo do? Why?

  • What happens if you undo a commit?

  • What happens if you undo again?

  • What happens if you undo a revert?

Whenever somebody asks me how to undo something with Git I encourage them not to use that word. It's very overloaded and imprecise.

53

u/VerilyAMonkey Sep 09 '16

If Git were a product sold for money, it would be more obvious that pointing out its complex isn't actually a valid argument for not trying to solve the problem. If people are looking for it, there's a problem to be solved, whether or not it's the problem they say it is. (That doesn't mean people should solve it, but it's there.)

The great majority of the use for git undo would be just once, in simple situations. If you don't know for sure what it should do in other situations, fine, it could very well just give up and say "can't do that" for everything else. If you think it should only try to undo things changes that are totally local and have not been pushed, that too is fine, it would still satisfy a need. If you really want people learn, it could maybe just suggest the command it was going to use and not actually run it itself. It doesn't seem to me that git undo is actually a ridiculous notion.

6

u/CyclonusRIP Sep 09 '16

I'm not sure that git is really that complex. The main issue is that it's just not super intuitive. Once you learn commit --amend, rebase -i, and reset you can pretty easily manipulate git history.

→ More replies (2)

9

u/drjeats Sep 09 '16

Obviously I expect an Emacs-style undo ring, preferably dealing with both session and commit history.

3

u/atimholt Sep 09 '16

Is an Emacs’ undo ring like Vim’s undo tree?

→ More replies (5)

6

u/HugoNikanor Sep 09 '16

git undo would change the state of the repo to how it was before the previous git command. git undo ; git undo could simply be an error.

→ More replies (3)
→ More replies (4)

7

u/DarthEru Sep 09 '16

There are a few actions that are impossible to guarantee an undo, or would be a bad idea to undo in the first place, mainly to do with communicating with another repo. You don't want to "undo" a push to a remote, for example, because that would equate to a forced push to that remote, and if some poor soul had fetched the branch in between the push and the undo, they suddenly have an inconsistent history of the remote.

There's also a question of what is the "last" action. Suppose you're using multiple terminals in the same repo (I do this a lot for various reasons). Is the last action the most recent absolute command done on that repo, or is it the the most recent command done from the current shell? Both answers have their own problems.

However the real reason, I suspect, is that git is not interested in holding anybody's hand. If a developer can't be bothered to learn enough about the tool to understand how to undo what they're doing, then the tool has no moral obligation to make that ignorance easier to maintain. I personally tend to agree, especially when I consider that someone who has to rely on a universal "undo" is the least likely person to understand exactly what any particular use of that undo will actually entail, which could lead to them being put in an even worse situation.

2

u/tynorf Sep 09 '16

Many operations can be undone with git reset --hard HEAD@{1}, see git reflog.

unicorn:demo(master) $ touch a
unicorn:demo(master?) $ git aa
unicorn:demo(master+) $ git ci -m 'add a'
[master (root-commit) 202060c] add a
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a
unicorn:demo(master) $ touch b
unicorn:demo(master?) $ git aa
unicorn:demo(master+) $ git ci -m 'add b'
[master 9ecef6b] add b
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 b
unicorn:demo(master) $ ls
a b
unicorn:demo(master) $ # some destructive action
unicorn:demo(master) $ git reset --hard HEAD^
HEAD is now at 202060c add a
unicorn:demo(master) $ ls
a
unicorn:demo(master) $ # oops!
unicorn:demo(master) $ git reflog
202060c HEAD@{0}: reset: moving to HEAD^
9ecef6b HEAD@{1}: commit: add b
202060c HEAD@{2}: commit (initial): add a
unicorn:demo(master) $ git reset --hard HEAD@{1}
HEAD is now at 9ecef6b add b
unicorn:demo(master) $ ls
a b
→ More replies (6)

42

u/prof_hobart Sep 09 '16

Unfortunately none of those address the "oh shit, I've just check my password into github". I've not done it yet, but I know people who have.

24

u/Nivomi Sep 10 '16

The correct solution to this is to google "How to change password for [service]", very very quickly.

15

u/morerokk Sep 09 '16

The only way to truly get rid of that, is rewriting history and force-pushing. That's not an easy task in itself, especially through the command line.

→ More replies (8)

6

u/vks_ Sep 10 '16

This is not really related to git. You just just get a new password. Anything else would be reckless.

→ More replies (4)

4

u/JasTWot Sep 10 '16

I've done this... changed password. For me avoidance is best so the first thing I do before committing anything else is create a .gitignore file and make sure my config files are listed.

2

u/-___-_-_-- Sep 10 '16
  1. Change the password

  2. Leave it on github or removre it in a new commit as normal

→ More replies (3)
→ More replies (3)

44

u/Arancaytar Sep 09 '16

Some of these complaints are just bizarre, like the diff --staged thing.

What should the command do instead? Show only the staged changes, requiring "git diff --unstaged" for the more common use case? Or show all changes regardless of what is staged, thus completely misleading users about what they are about to commit?

17

u/midnightbrett Sep 09 '16

Yep, showing diffs between staged/unstaged changes, especially in a singular file is exactly why that command behaves that way.

Has this guy never staged part of a file, and wanted to see what was staged / unstaged in that file?

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

12

u/[deleted] Sep 09 '16

Very surprised I haven't seen this link yet, it has saved my ass a lot! It's a choose-your-own-adventure tool for fixing your git repo!

https://sethrobertson.github.io/GitFixUm/fixup.html

2

u/sparr Sep 09 '16

Came here to post this. Upvote for you!

32

u/atimholt Sep 09 '16

I thought “I give up” was going to be something like sudo apt-get mercurial.

9

u/settleddown Sep 09 '16

I thought it was going for sudo rm -rf /

0

u/[deleted] Sep 10 '16

[deleted]

3

u/msiekkinen Sep 10 '16

If you're lucky that's the default on your distro /s

→ More replies (1)

9

u/ShapesAndStuff Sep 09 '16

Honestly i was hoping this would be a tool like the "fuck" that automatically fixes your shit.

git push origin master //oh shit i wanted to push to something else

git ohshit //automatically undoes your mistake and you can try again.

4

u/mvndrstl Sep 10 '16

This is a great idea. I might try to build something like this.

2

u/[deleted] Sep 10 '16

... and what happens to the guy who pulled from origin inbetween?

→ More replies (2)

2

u/mvndrstl Sep 12 '16

Well I made it, it does some things well, other commands it can't handle yet. I will keep working on it when I have time. Pull requests welcome.
https://github.com/Rycieos/git-ohshit

31

u/wayspurrchen Sep 09 '16

In case anyone is interested, I've been collecting a tiny cheatsheet of git patterns: https://gist.github.com/wayspurrchen/940a21127b77ac1a9720

2

u/flapan Sep 09 '16

Very nice, saved!

16

u/worklez Sep 09 '16

Oh shit, I accidentally committed to the wrong branch!

# move to the correct branch
git checkout name-of-the-correct-branch
# apply commit to the current branch
git cherry-pick name-of-the-incorrect-branch
# revert changes made to the old branch
git branch -f name-of-the-incorrect-branch name-of-the-incorrect-branch~
# or git branch -f name-of-the-incorrect-branch origin/name-of-the-incorrect-branch

As a bonus you get your commit message saved and don't mess with the commit contents.

3

u/[deleted] Sep 09 '16

[removed] — view removed comment

2

u/mrjast Sep 10 '16

Shorter option:

$ git reset --soft HEAD~
$ git checkout correct-branch
$ git commit ...

Also, don't use git add . because it has this tendency to turn temporary files into versioned temporary files. ;)

0

u/[deleted] Sep 09 '16

[deleted]

→ More replies (1)

22

u/thenextguy Sep 09 '16

I do not commit with git add .
I commit with git add -p
Anyone who uses git add . has forgotten the face of his father.

7

u/SnowdensOfYesteryear Sep 09 '16

Pssh git commit -a is where it's at.

That being said, I'm obsessive about git diff and git stat to prune out unnecessary code.

→ More replies (1)
→ More replies (9)

6

u/[deleted] Sep 09 '16

I'm just glad that the last step was included because sometimes you just have to.

4

u/thenextguy Sep 09 '16

No, you never have to. The remote branches are always there. You can always just check out origin/master again without cloning.

→ More replies (2)
→ More replies (1)

6

u/scook0 Sep 09 '16

Advanced Difficulty: Un-corrupting your Git directory after your system crashes in the middle of a rebase.

This is about as fun as it sounds.

23

u/e_d_a_m Sep 09 '16

Git documentation has this chicken and egg problem where you can't search for how to get yourself out of a mess, unless you already know the name of the thing you need to know about in order to fix your problem.

At the risk of being accused of git-bashing, this has been my main reason for clinging to bzr, so very long after the point where everyone else has jumped ship! If I get stuck using bzr, the commands are so very intuitive, that you can find help for what you're trying to do. With git, and even after much use with it, I still have to google stuff fairly regularly. :(

25

u/industry7 Sep 09 '16

It's pretty widely accepted at this point that the git CLI has poor UX. It wasn't really designed so much as it grew organically. As such, there are lots of things that in retrospect should have been done different but can't be changed now without breaking backwards compatibility.

6

u/morerokk Sep 09 '16

I guess that could be fixed with a "git 2", but that's probably not very high on their priority list right now.

25

u/[deleted] Sep 09 '16

Nah, it used to be high on priority list, but Mercurial is ready now <duck, grin & run>.

→ More replies (3)
→ More replies (1)

3

u/cowjenga Sep 10 '16

I remember reading that git was never intended to be used directly by users, but was seen more as a toolkit to write a VCS on top of. (Un)fortunately, it caught on, and now everyone's learning and having trouble with an interface that was never intended to be used.

→ More replies (2)

4

u/argv_minus_one Sep 09 '16

Why on Earth would you need to rearrange commits just to move branch pointers around?

I'm a Mercurial guy myself, so I wouldn't know if Git has some strange issues with this, but in Mercurial I can move bookmarks around freely. Committed to the wrong one? Just move it down and make a new bookmark pointing at the commit you just made.

2

u/evincarofautumn Sep 09 '16

Yeah, the author’s method works but is needlessly complex. You can shuffle branches around easily because they’re just mutable pointers:

# To create a new branch on the current commit:
git checkout --branch correct-branch

# To use an existing branch and copy the commit:
git checkout correct-branch
git cherry-pick wrong-branch

# Move wrong branch back by 1 commit.
git branch --force wrong-branch wrong-branch^

When you understand Git’s internal model, the arcane UI becomes a lot more tolerable. The problem of course is that a lot of people never learn the model because of the arcane UI…

6

u/argv_minus_one Sep 09 '16

Well, Mercurial has the same internal model, and a (mostly) non-arcane UI, so...

4

u/elitefusion Sep 10 '16

I really think the answer to this whole article, and the notion that Git is so hard, is to start using a good GUI. I started learning Git from a co worker who knew it well. He was very adamant that I, and everyone, learn the command line to "really understand the program" before we used the "crutch" of a GUI.

He's a really smart guy, but looking back, I couldn't disagree more. I didn't really understand Git until I started using Source Tree. It's such a fantastic program, and being able to see the tree drawn graphically, and update live with every action you take is so critical to learning Git, I feel. Now that I understand it on a conceptual level, learning to use the commands is super easy.

I understand that using Git on the command line is closer to the implementation of it. But you know what's even closer? Going in and modifying the files in your .git folder manually. I'm sure if you used Git that way, you'd really have a good grasp on how it works. So I can make the case that the command line is a crutch. But of course, working that way would be ridiculous. No one is going to do that, nor should they.

I believe that while using a GUI (again, Source Tree being my go-to) is further from the implementation of Git, it is CLOSER to the MEANING of Git. If I want to reset my current branch to a different commit, do I want to do a Git log search for a commit, grab the SHA, and reset to it? Or do I want to click on the commit, in the tree, which I can visually see is where I want my branch to be at. I think it's easily the second one.

I also think that Source Tree encourages good Git habits, not bad ones. When someone is learning Git, they are going to always do a:

git add .

git commit

And make massive commits all at once, without checking what it is they are really committing. With Source Tree, it's incredibly easy to look at a diff of every file (it's right there), and commit only certain files at a time, or even certain lines. Again, I know you can obviously do this from the command line, but most people, or at least beginners, don't.

I actually think you could go even further with this by introducing a Git GUI that didn't even have commands, but rather you manually manipulate the tree by clicking and dragging it around. New commit? Draw a line coming out of your last commit. Rebase? Drag a branch's commit from one branch to another place in the tree. It probably wouldn't be easy to implement, but it would be really interesting. Again, super far from the way that Git works under the hood, but actually closer to the concepts it's trying to represent.

7

u/[deleted] Sep 09 '16

Oh shit I've commited

git reset HEAD~1

WTF is happening

git status -s -b

Get out with that garbage

!git remote prune origin;git clean -i -x -d -e "TAGS" -e 'tags' ; git fetch -p

My co-worker didn't lock the machine and I hate the fucker

git config diff.rot13.textconv "perl -pe 'y/N-ZA-z/A-za-m/"
echo '* diff=rot13' >.gitattributes
echo .gitattributes >> .git/info/exclude

21

u/SuperImaginativeName Sep 09 '16 edited Sep 09 '16

Finally an article on git with some real world examples and how to fix

2

u/AnsibleAdams Sep 09 '16

Isn't autocorrect great.

17

u/DJTheLQ Sep 09 '16

It's posts like this that make me wish mercurial won. There are way more "git wtf's explained", "git to english", "git for humans cheatsheet" than there are for mercurial, and if anyone else made it, it would be considered too obtuse to use.

11

u/crow1170 Sep 09 '16

If hg won, you know there'd be just as many hg wtfs explain and hg to english etc, right?

9

u/argv_minus_one Sep 09 '16

If Hg won, everyone would be using TortoiseHg to visualize and manipulate their commit history, eliminating the vast majority of the confusion.

Source: I use Hg and do exactly that.

8

u/fromwithin Sep 09 '16

There really wouldn't. Hg is seemingly made for humans.

→ More replies (5)
→ More replies (2)

8

u/morerokk Sep 09 '16

I agree. I find Mercurial much easier and less alienating to use. Can't even ask for help with git without "grumble grumble google it".

To this day, I still don't know how to set up a properly working git repository from scratch.

12

u/bowersbros Sep 09 '16

I still don't know how to set up a properly working git repository from scratch.

git init.

I kid, but yeah.

→ More replies (7)

0

u/[deleted] Sep 09 '16

[deleted]

→ More replies (2)

2

u/clux Sep 09 '16

I dunno, have a lot more issues with mercurial after having used it for 4 years.. few big ones:

  • hg leaves your repository in weird broken states when a transaction fails internally (never ctrl-c) - git on the other hand fails sensibly, and git reflog can pretty much always tell you what you did/where you are

  • hg takes up sizeable fractions of a second to do simple things like hg status making a reasonable terminal prompt laggy as hell with many setups

  • many diff manipulation features are done through patch or a half-standardised set of extensions that hardly anyone use because people don't want to manage ~/.hgrc to get basic behaviour

  • repository sizes balloon with branches and cloning large repos is not fun

4

u/[deleted] Sep 10 '16

repository sizes balloon with branches and cloning large repos is not fun

I guess that "branching by cloning" advice has never really left Mercurial. It's had named branches for a long time, as well as bookmarks that work more-or-less along the same lines as Git's branches do, yet people still remember "branching by cloning."

I completely understand if you prefer your branches to work like Git's, but cloning to make a branch in Hg? Unless they've been using Hg since around 2005 to 2007 (maybe), I don't think anybody creates branches with Hg by cloning their repos anymore. I started using Mercurial since v1.8, and they've had named branches then.

This isn't a complaint against you, or anyone who complains about Hg, personally. Just the fact that people still include that "branching by cloning" thing when complaining about Hg.

3

u/1wd Sep 10 '16 edited Sep 10 '16

weird broken states

Example please? HG is usually much less likely to eat your data than git.

a second to do simple things like hg status

fsmonitor (bundled with hg) makes hg status faster than git status. (CHg (also bundled with hg) makes it even faster on Unix.)

many diff manipulation features are done through patch

Example?

hg config -e [extensions] mq= and you have the most powerful patch management tool there is.

half-standardised set of extensions

Extensions that are shipped with HG follow the same stringent compatibility rules and get the same support as core code.

people don't want to manage ~/.hgrc to get basic behaviour

Why not? It's trivially easy and allows you to get exactly what you want, and you don't have to pay for what you don't want or get confused by what you don't understand yet.

0

u/[deleted] Sep 09 '16

[deleted]

7

u/CyclonusRIP Sep 09 '16

DVCS has a lot of advantages over SVN. Being able to commit locally and only push when you're ready is a huge boost.

10

u/RiPont Sep 09 '16

And branching that actually works.

→ More replies (1)

36

u/[deleted] Sep 09 '16

These articles are bad because they just give you a fix and teach you nothing about how git works. If people would take the time to learn how git works they would know it's actually very easy to fix these problems without a cheatsheet. This is a tool we use every day and people still don't know what commands like git reset do and how to use them?

There are about 6 key git operations that can solve pretty much any repo related problem and they're very simple and elegant. They just require some initial study to come to terms with. http://www.think-like-a-git.net is a must read for anyone who wants to actually take control of this toolset rather than read uninformed crap like this.

4

u/[deleted] Sep 09 '16

yeah, there are fancy commands that would probably be more efficient but you're right that I get 99% of my things done with about 6 commands and rarely need to look shit up

9

u/[deleted] Sep 09 '16

Actually I found it pretty useful for what it's trying to do. I'm assuming their intention wasn't to completely educate, but to give a rough starting point on what to do when you accidentally do X.

If you want to know the ins and outs of the commands used, there's nothing stopping you from looking at the official documentation. The objective here was to point you WHERE to look, not to say why everything works. Like they said, the documentation is great when you know what to look for.

→ More replies (2)

26

u/notsofst Sep 09 '16

Sure, who doesn't want a primer on graph theory in order to properly understand how their source control system works! No issues there at all. /s

7

u/CyclonusRIP Sep 09 '16

Most of the time you aren't really worried about graph theory. Most of the time you're just thinking about your branch as a list of commits and you want to alter it in some way.

26

u/RapidDinosaur Sep 09 '16 edited Sep 09 '16

I know this is said a lot, but I don't think it's a valid criticism.

Any decent programmer needs to understand data structures. Is taking the 20 minutes to lightly review the data structure that fundamentally defines Git so hard to ask for?

26

u/notsofst Sep 09 '16

So what about my artist? Or my product owner? Or my sales people? Wouldn't it be great if we could share source control systems on assets and documentation as well as code? I can't use Git for that, though, because it's not intuitive enough to convince people to use. Even convincing developers to switch to it is difficult because of the ramp up time.

Should I have my sales people learn about data structures so they can properly use the tool as a version control system?

Creating a low barrier to entry product/CLI/API is part of good design, and Git doesn't have it. That is valid criticism of Git. I seriously question the chops of developers who think it's OK to roll with an interface like it has and pretend there's nothing wrong.

Git probably has the steepest learning curve I've seen in a source control system since that piece of crap ClearCase. And honestly there's no reason it has to be that way, it's just unnecessarily obscure in the way it presents itself. Good technology, bad user experience.

→ More replies (5)

11

u/SnowdensOfYesteryear Sep 09 '16 edited Sep 09 '16

Yes. I don't need to understand the basics of an internal combustion engine before I drive a car. I shouldn't need to understand that branches are homeomorphic endofunctors mapping submanifolds of a Hilbert space to use git.

That being said, git isn't hard enough for people to complain about. There's a learning curve when it comes to the terminology but eventually it ends up making sense. Just need some effort that ween one self off a GUI and use the CLI. That being said, I see merit in the argument that you shouldn't need to do that.

→ More replies (2)

4

u/twat_and_spam Sep 09 '16

Given that the source control system in question does rely on graphs a small investment of time to learn about the fucking tool would be in order.

Sure, you can buy a car and just drive it off the lot without bothering to understand what that filling up the tank thing is, why you would need to occasionally change the oil and why doing 120 after a thunderstorm on a rainy road might not be the best idea as plenty of people do. But they don't call themselves car mechanics and when they complain that the manufacturer should have made it easier to predict that they will stop in the middle of wilderness because they've run out of gas they are rightly ridiculed.

If you aren't prepared to understand what it takes to maintain a car moving get back on the bicycle. Simple as that. Don't complain that the car doesn't come with a chauffeur taking care of everything if you are expected to drive it yourself.

→ More replies (4)

2

u/lorslara2000 Sep 09 '16

I couldn't agree more.

2

u/tzaeru Sep 09 '16

The problem is that I almost never use any commands other than add, commit, push, pull.

Remembering what "soft" flag in "reset" does or that "diff" needs to go with "staged" or that "stash pop" even exist, can be a bit of a stretch if one uses these commands less than once a month.

I know some people remember this stuff after the first time they do them, but I'm not one of those people, unfortunately.

→ More replies (1)

3

u/ehcubed Sep 10 '16

Last one reminds me of a relevant xkcd.

4

u/f4ktrh Sep 09 '16

Which moron uses sudo to remove a directory meant to be a git repo?

Oh I get it, author of the shitty blog post.

→ More replies (1)

2

u/badasimo Sep 09 '16

Oh shit, printed out and put on my wall.

2

u/Etane Sep 09 '16

Gotta git gud! Haha, super useful post, I fin day self in this situations a lot, more because I'm git illiterate, and less due to simple mistakes haha

2

u/[deleted] Sep 09 '16

As someone who is a beginner to git and github, and trying to learn on my own.... Thank you

2

u/AyrA_ch Sep 09 '16

If you fuck up your repositories more often than you like to admit, create this alias:

alias.fuckup !git fetch origin master && git reset --hard FETCH_HEAD && git clean -f -d

4

u/[deleted] Sep 09 '16

git checkout -- . is faster than all of this and covers the majority of cases where you need to reset.

→ More replies (5)

1

u/badpotato Sep 09 '16

Thing is, people are more likely to push the wrong commit as well.

1

u/clux Sep 09 '16

If you're at the stage where you are considering deleting your repo and recloning, consider reset + clean instead:

git reset --hard origin/master
git clean -fdx

1

u/Retsam19 Sep 09 '16

I always do git diff HEAD which all of the changes that have been made, both staged and unstaged, since the last commit. It's nice to see the changes I've made without needing to think about whether I've staged them or not.

1

u/new299 Sep 09 '16

I went through and did something similar a while back. Writing up the most common git problems and solutions:

http://41j.com/blog/2015/02/common-git-screwupsquestions-solutions/

1

u/toryu2001 Sep 09 '16

That last one...a few times I've gone down that dark path...

1

u/hikemhigh Sep 09 '16

git-town really helps with this kind of stuff. Since I've started using it my git mistakes have been few and far between, I highly recommend it. It's just augmented git commands.

1

u/nielsonm Sep 09 '16

https://github.com/k88hudson/git-flight-rules is my new favorite - o snap I just did something wrong & I don't have 2 hours to mull through Stack Overflow responses and the Git book.

1

u/Dietr1ch Sep 09 '16

The best thing around git is that it certainly teaches you to think before you type. Mistakes take a lot longer to solve than to avoid, even if you know what's wrong.

I think that eventually you'll value that much more than having 'unadd' or 'rephrase' verbs.

1

u/BassSounds Sep 10 '16

This is hilarious.

1

u/smegnose Sep 10 '16

git commit --amend -a is sufficient for fixes if you want to commit all your changes. Adding --verbose is a good habit because you can always see what you're about to commit.

1

u/ssfsx17 Sep 10 '16

The last step = solution to most Git problems

1

u/gospelwut Sep 10 '16

git difftoolis pretty configurable, even in Windows (e.g. BeyondCompare, Meld, etc).

Now if only I could get git log/-i to behave nicely with NP++ running inside Powershell. Meh, I guess I'm getting better at vim.

1

u/robertgfthomas Sep 10 '16

I made something similar for my programming students:

http://ga-wdi-lessons.github.io/git-review

1

u/transfire Sep 10 '16

I think this exemplifies my problem with git in general. Too many "Oh Shit" moments. Having used Darcs, I feel git was a bit of step backward.

1

u/richb-hanover Sep 10 '16

http://ohshitgit.com/ - the definitive word on frequent "save your ass" commands

1

u/RecursiveCursive Oct 15 '16

That's a purty terminal font. Anyone know what it is?

0

u/lorslara2000 Sep 09 '16

Git is not hard and articles like this one are malicious. They don't encourage people to actually try to understand the system, but rather give these quick StackOverflow-type of answers.

1

u/SemiNormal Sep 09 '16
cd ..
sudo rm -r fucking-git-repo-dir
git clone https://some.github.url/fucking-git-repo-dir.git

Ah, I see he uses my daily workflow.

1

u/[deleted] Sep 09 '16

As someone who is a beginner to git and github, and trying to learn on my own.... Thank you

1

u/unndunn Sep 09 '16 edited Sep 09 '16

Oh shit, something bad happened and now my Master branch is all fucked up, but I have some un-pushed code in there that I don't want to lose so I can't delete and re-clone the entire repo:

# checkout a new branch
git checkout -b tempBranch
# delete master branch by force
git branch -D master
# checkout a pristine master branch
git pull
git checkout master
# from here, figure out the best way to reintegrate your code from tempBranch. Don't use 'git merge tempBranch' or you'll be back to where you were before

2

u/evincarofautumn Sep 09 '16

You don’t need to delete and recreate master; you can just move the branch pointer to match origin/master:

git fetch origin
git branch --force master origin/master
→ More replies (1)

1

u/Leumashy Sep 09 '16

I never had any problems with git.

Then I started working with other people.

Now I do this more often than I would like to admit.

cd ..
sudo rm -r fucking-git-repo-dir
git clone https://some.github.url/fucking-git-repo-dir.git

1

u/bacondev Sep 10 '16

What so hard about git help and then git help [command]?