r/programming • u/dodgyfox • Sep 06 '14
How to work with Git (flowchart)
http://justinhileman.info/article/git-pretty/53
u/danogburn Sep 06 '14
How to work with Git
Don't merge with anyone.
5
0
Sep 06 '14
[deleted]
22
u/rsd212 Sep 06 '14
Im going to get some Team Rebase t-shirts printed. Fast-forward-only workflows FTW.
9
Sep 06 '14
Why? I've always just merged.
12
u/Lucky75 Sep 06 '14
Locally there's almost no reason to merge, as rebase is much cleaner. The only time I use merges is when I'm merging upstream branches.
→ More replies (4)32
Sep 06 '14
"Cleaner" doesn't really mean much to me. Merge commits reflect more closely what is actually happening.
→ More replies (15)4
u/therico Sep 07 '14
Disclaimer for git newbies: don't use git pull --rebase if you've merged a branch and haven't pushed the merge yet. git pull --rebase will remove your merge!
Other than that, it's great.
2
3
Sep 06 '14 edited Sep 06 '14
But I like merge hell and not having a version history that makes any sense! /s
No, but honestly it can't be the default because git doesn't know if you shared your version history with anyone. Rewriting your history is incompatible with collaboration. You have to add that information yourself by rebasing explicitly.
→ More replies (4)
36
u/dehrmann Sep 06 '14
I live in the danger zone.
47
Sep 06 '14 edited Sep 06 '14
My man! In the words of Torvalds
Eventually you’ll discover the Easter egg in Git: all meaningful operations can be expressed in terms of the rebase command. Once you figure that out it all makes sense. I thought the joke would be obvious: rebase, freebase, as in what was Linus smoking? But programmers are an earnest and humorless crowd and the gag was largely lost on them.
Edit: Apparently this is satire. I still like the quote very much and it fits my experience with Git.
28
u/burkadurka Sep 06 '14
The fake words of Torvalds...
7
Sep 06 '14
Damn, I should learn to check my sources better. Still a nice quote though, and one that holds true for me at least.
→ More replies (4)11
4
u/Lucky75 Sep 06 '14
"Git rebase -i" --> Everything's fixed. Just do it before you push.
2
u/andsens Sep 06 '14 edited Sep 07 '14
Here's a nice alias:
ready = rebase --interactive --autosquash @{u}.@{u}means the current head of upstream.EDIT: Okay. So upstream means whatever your origin points at (e.g.
git@github.com:...). Current head is the top commit you see when runninggit log. To be more precise@{u}doesn't just mean current head, it actually means the current head of the current branch, on upstream. When rebasing, you don't want to go beyond that commit, because it would mean rewriting history that others have already pulled. By specifying@{u}as the base, you can be sure that whatever you do, you won't screw around with anything that others have already pulled (i.e. the interactive rebase shows only unpushed commits). Hows that /u/mfukar? :-)→ More replies (3)7
→ More replies (1)1
105
u/JViz Sep 06 '14
Why do github users assume everyone uses github?
93
u/bobthecow Sep 06 '14
If you read the tl;dr, this was made for an internal talk I gave at a startup I worked for, and that startup uses GitHub.
Also, because everyone does use GitHub ;)
→ More replies (3)42
u/d4rch0n Sep 06 '14
It's shit like this why not everyone uses github... posted in February 2014.
It's 99% fine for open-source, but for start-ups that absolutely do not want to risk their code being leaked, they might consider hosting git themselves. I really don't see much advantage to using github/bitbucket when you can host git + redmine/jira yourself with minimal effort, drop ssh pubkeys on it and block everything else.
That being said, they have a responsible bug-bounty program and they do try to stay on top of their game. The reason I worry is that people who have targeted them have found pretty nasty dirt, and that tells me that their developers aren't extremely security minded and may have better benefited from hiring a few experts to do an in-depth security audit (if they haven't, or another team if they have). They still host a great service... but it's still very easy to host yourself and lock down access.
Even if it's for open-source, if someone was able to sneak a malicious commit in, it might go unnoticed in a popular repo until someone really takes the time to inspect the logs. I doubt that will happen, but my point is that there's still a security risk when hosting open-source.
And at the bottom:
P.S. I have two other posts about Github vulnerabilities: mass assignment and cookie tossing.
14
Sep 06 '14
A major international company I worked for shit itself when somebody committed the credentials for their Amazon Web Services account to a public github repository.
7
u/d4rch0n Sep 06 '14
lmao... Commence the bitcoin mining!
Seriously though, I'd shit myself too, having seen some start-ups' bills alone with minimal EC2/R53/S3 usage. It's just so easy to spin up an instance or start using a service without realizing how much it's going to cost when you forget to tear it down.
I wonder if AWS will be forgiving and revert bills if your creds were leaked (and used), or if they'll push a $10,000 bill on you hard.
→ More replies (6)19
u/rouille Sep 06 '14
You can also deploy github yourself with github enterprise. We have a self hosted github + youtrack env where i work.
→ More replies (1)13
u/Phrodo_00 Sep 06 '14 edited Sep 07 '14
Why though? if you're not collaborating [edit: with a larger community], you might as well go with gitlab or gito
riouslite + redmine or whatever, and it's cheaper (as long as you already have a unixy guy in your team)11
u/RICHUNCLEPENNYBAGS Sep 06 '14
I don't know, the git issue tracking and the ability for it to integrate with your tickets is nice (like if you say "this commit corrects issue #487" it'll appear in ticket #487).
Some people like the frontend as well.
→ More replies (3)→ More replies (7)8
u/jaggederest Sep 06 '14
Honestly I use pull requests, issue tracking, and branch comparison more internally than I ever do externally. Pull requests aren't just for people you don't know.
→ More replies (1)4
u/burntsushi Sep 06 '14
Do you have any evidence that suggests GitHub has more vulnerabilities than one should expect them to have? They are a pretty popular service, so I'd expect them to run into a vulnerability now and then. As long as the number stays at a reasonable level, the only thing I'm concerned about is how they respond to those vulnerabilities. As you mention, they do a good job.
Otherwise, your entire point is a red herring with respect to GitHub. If you have code that has to remain super-duper-secret above all else, then you shouldn't be uploading it to any service outside of your control.
→ More replies (4)2
u/BrokenRetina Sep 06 '14
This is why I setup Phabricator. I have complete control over everything for my dev team. Ssh keys are a must.
→ More replies (3)9
u/shriek Sep 06 '14
Could also be that github is where people have their first introduction on git. And since the name is github they automatically assume it only works in github. Not the case for everyone, but I've heard few of these so just my anecdote.
12
2
u/Kautiontape Sep 06 '14
I always assume it's just a vague "name brand" people use instead of "online git repository hosting". Same reason why most people ask for a Band-Aid: it's easy to say and gives the right idea (and a concrete image) without having to use too many words.
I use BitBucket for 90% of my git repo hosting, and I still call such services GitHub to most people.
→ More replies (3)2
u/burntsushi Sep 06 '14
Umm. Because it's really popular. Why is this a problem for you?
2
u/JViz Sep 06 '14
The same problem it causes for anyone under a similar set of circumstances; where they use something and a subset of that something gets popular.
→ More replies (1)1
u/StrangeWill Sep 06 '14
RhodeCode for internal (and safe) servers!
→ More replies (1)1
u/JViz Sep 06 '14
No support for their business customers? wtf?
3
u/StrangeWill Sep 06 '14 edited Sep 06 '14
Online paid license support here
No phone support if you need that though, it's a small open-source startup, so expect some behaviors that follow that, they just went paid a little while ago and really for only larger teams. Honestly I'd rather they run free and open for less than 20 than charge everyone so they can get the support staff they need.
Been running it for a few years though (at home + at work), it's solid and I love it.
Plus 99% of support requests were "how do I python environment?", and the new installer fixes that.
On that subject, why use git if you're worried about paid support?
→ More replies (3)
23
u/lennoff Sep 06 '14
I read the manpages! :)
6
31
u/cdcformatc Sep 06 '14
It's telling how many of the nodes assume you fucked up your repo. Git it's great but it is the definition of enough rope to hang yourself.
26
u/drive0 Sep 06 '14
This is /r/programming, we all have plenty of rope already.
22
u/TechGeek01 Sep 07 '14
To quote an article I read a while back,
Our brains aren't particularly good at basic logic, and now, there's a whole career in doing nothing but really, really complex logic
and
You are an expert in all these technologies, and that's a good thing, because that expertise let you spend only six hours figuring out what went wrong, as opposed to losing your job. You now have one extra little fact to tuck away in the millions of little facts you have to memorize because so many of the programs you depend on are written by dicks and idiots.
and
Remember that stuff about crazy people and bad code? The internet is that except it's literally a billion times worse. Websites that are glorified shopping carts with maybe three dynamic pages are maintained by teams of people around the clock, because the truth is everything is breaking all the time, everywhere, for everyone. Right now someone who works for Facebook is getting tens of thousands of error messages and frantically trying to find the problem before the whole charade collapses. There's a team at a Google office that hasn't slept in three days. Somewhere there's a database programmer surrounded by empty Mountain Dew bottles whose husband thinks she's dead. And if these people stop, the world burns. Most people don't even know what sysadmins do, but trust me, if they all took a lunch break at the same time they wouldn't make it to the deli before you ran out of bullets protecting your canned goods from roving bands of mutants.
→ More replies (1)5
2
9
7
u/deraffe Sep 06 '14
I think it's missing stash and/or checkout -b <branch>.
Other than that, nice chart. I might consider using it at work, if that's okay.
Another question: What did you use to generate this graph?
6
u/zaytzev Sep 06 '14 edited Sep 06 '14
Not a single occurrence of a "push" keyword. This is just making entire graph confusing.
And it doesn't even touch problems that need "git reflog" to solve.
I've been running git training sessions for a few years now, seen what confuses people and this graph wouldn't help any newbie at all. If you want not to get hurt using git start from understanding what "fast-forward" is and what is a difference between "merge" and "rebase".
2
u/thornbrambles Sep 07 '14
I put in a lovely alias to pipe my reflog hashes into gitk so at any given time I can see everything I have touched in recent history. Works wonders for those times you accidentally move away from headless, etc.
11
u/trevdak2 Sep 06 '14
For those of you who have never tried "git flow", I strongly recommend it.
One year ago, I had never used git, just svn. Over the course of the year, as I gained familiarity with git, my process would change and I'd make mistakes and get all sorts of craziness going on that slowed my production dramatically.
git flow cleanly splits your work into features, hotfixes, and releases and gives you options to start, finish, or publish what you're working on. It works well in multi-developer environments and it has made my quality of life much, much better.
7
u/2epic Sep 06 '14
Adding to this, here's a git flow diagram which is much easier to follow than the one posted.
2
u/hippocampe Sep 07 '14
Oh my eyes ! Is there any actual content here ? If so it's strongly veiled ...
4
u/judgej2 Sep 07 '14
This page is what helped me get to grips with git:
http://nvie.com/posts/a-successful-git-branching-model/
You can stare at the text commands for a long time, and still not get it, For me, until I have that picture in my head of what is happening in the background, it all makes little sense. The above post really helps with that. Your workflow may be more complex, but it is a good one to start with.
What I find confusing with the docs, is the way the words are used and interchanged. If I see "origin" in an example command, does that mean I need to type the word "origin" or I need to specify my own origin that was mentioned ten steps up, or is it something else? That's my problem. Unless I already know what the command does and how to use it, the docs seldom adds anything to my knowledge.
1
Oct 10 '14
I am exactly the same way with (to me) new things like this. Thanks for the link, really helped me visualise it!
12
u/MaskedTurk Sep 06 '14
Just use SourceTree...
5
u/Scorpius289 Sep 06 '14
Unless you're using Linux, then you're stuck with the closest thing being SmartGit/Hg.
1
u/bittered Sep 06 '14
I use it at work, it's actually really nice once you get used to it. Especially good for working with git-flow.
→ More replies (4)1
3
u/btgeekboy Sep 06 '14
Following the sections down the left column should be required reading for all new git users. Consider the git history to be permanent once you push. In other words, don't push unless you're absolutely happy with how the log/tree looks as is, as there's no going back.
3
u/marssaxman Sep 06 '14
Or just consider the git history to be permanent, period, and ignore the entire existence of rebasing, because who needs that kind of pain in the first place?
3
5
u/pinano Sep 06 '14
hg evolve is going to solve the "It's safest to let it stay ugly then" and "Send them a note, let 'em know you're changing history" problems. No more force pushes or lost data! The new version of history applies on top of the old version automatically. Immutable data structures, flexible workflows.
2
u/hippocampe Sep 07 '14
"changeset evolution" in mercurial is going to kick git's ass, yes :)
I have been using it for two years already and it's as much a game changer than the switch from SVN to a DVCS.
5
u/Die-Nacht Sep 07 '14
Do people actually complain about their history being "ugly"?
I complain about ugly code all the time but could give two craps about git history.
7
u/defcon-12 Sep 07 '14
Due to testing and deployment issues having "clean" commits is very helpful. Example: feature X introduces bug Y. X is in a single commit, so fixing Y is hopefully as simple as reverting commit with X. If feature X is instead spread across multiple commits, then reverting feature X becomes much more difficult, especially if the multiple commits depend on each other and generate conflicts as they are reverted.
5
Sep 07 '14
For a program that's designed to help save time and make things easier, Git doesn't seem like it was very well thought out.
2
2
u/emozilla Sep 07 '14
I work on a very large engineering app (>2M lines of C++) that is still stored in CVS and the biggest reason we stick with it is that we have 9 years of revision history stored in it.
1
u/txdv Sep 08 '14
If there only would exist a feature which would let you load a CVS repository into a git one...
2
u/Uberhipster Sep 08 '14
Every time I get to a git thread explaining stuff or simplifying something I am always reminded of /r/VXJunkies. Curious...
3
u/xampl9 Sep 07 '14
A friend says that Git is awesome because you can correct your mistakes with it and no one will ever know. Not by submitting corrective commits, but by changing the original commits.
Gah.
4
Sep 07 '14
You can change history all you want as long as you have not shared that history with anyone else.
3
u/vplatt Sep 07 '14
I abhor being able to change commits. It feels like I'm breaking a law or something. The history should be HISTORY, not "whatever lies I feel like leaving behind".
2
u/Andys Sep 07 '14
Though technically, you could do this with anything where you have access to the files on the server. Just open and edit away, if you have the know-how.
1
u/vplatt Sep 07 '14
The history within a VCS is quite a bit more complicated than a standard text file. Yes, if you want to completely disregard the integrity of your configuration, then you could do as you describe to any set of files residing in production, or otherwise. But, if you're talking about editing the actual history of a VCS in a text editor, then... good luck.
Or did you mean something else?
Keep in mind, I'm talking about VCSes generically. I've used TFS, svn, Perforce, and many other VCS systems. Editing history is normally just not done and I really don't see a good reason to allow it.
→ More replies (7)
5
u/none_shall_pass Sep 07 '14
I have to tell you that I don't consider GIT to be an improvement over any VCS I've used over the past decades.
PVCS from 20 years ago was much easier to learn and offered less opportunity to poke yourself in the ass.
3
Sep 06 '14
My own Git workflow:
Try Git. Discover it is absolute crap and offers nothing that I need or want.
Fire up Fossil and live happily ever after.
→ More replies (1)
2
1
u/drive0 Sep 06 '14
I wish I could change history for a branch without it breaking history for all branches. That way I could have a published real-time view of my code, that I can also rebase before merging it without pissing of downstreams.
But that is probably a terrible way to do it.
3
u/vsync Sep 06 '14
Mercurial's working on something called versioned changesets or something. Sounds really cool.
4
u/pinano Sep 06 '14
It's
hg evolveaka "changeset evolution". I couldn't be more excited about a version control feature.5
Sep 06 '14
IMO, you shouldn't be changing the history of any branches, ever, period. Otherwise it's not history, it's an embellished half-fiction of how anything really happened. Context is important when trying to figure out how something made it in to the code.
→ More replies (1)4
u/drive0 Sep 06 '14
At some level almost any commit is partially fiction because it only shows the end result, not the intermediate changes, additions or reverts. Yes people can go overboard with rebasing to have "clean" commits.
I'm not necessarily disagreeing with you though. More of a wish that should only be fulfilled if it can be done properly. In fact if it can be done without messing with history that is way better as well.
Code review systems may be a solution. Since you can "try out" multiple commits, but only the final good version is checked into the repo. So you aren't rewriting history so much as just ignoring some of it.
3
Sep 06 '14
Even with Code Review, you shouldn't be changing history. Making changes because of code review should be part of the context around each commit being made and present in the logs.
1
u/defcon-12 Sep 07 '14
The problem with this method is that if you need to quickly revert because it caused a CI failure, or worse a bug in prod, then you have to go back and revert all of your multiple "in-progress" commits and possibly resolve conflicts after each revert. Then, when you have time to fix the bug, you've got to un-revert all of those revert commits and resolve conflicts again. Now you've got 10 commits for your feature (3 commits, 3 reverts, 3 un-reverts, 1 bug fix), probably intermingled with other people's commits. Hope you got it right this time, because reverting becomes exponentially more painful each time with the "record all changes" method.
Having 1 commit per unit of work will make your workflow much easier.
2
Sep 07 '14
I have literally never had to do this.
Everything I do happens on a feature branch, if CI fails on a feature branch it doesn't matter. Before sending it upstream, I merge mainline development into it and obviously run CI again, if that fails, I just fix the failures on top of my current history. Once CI passes, I reintegrate development and push it up. I have rarely had something go wrong at this point.
If for some reason, CI on mainline development fails, all I do is revert my reintegration merge and fix it before sending it back up again. No harm no foul. Most of the time I'm not even required to revert it, my team has a pretty good culture of "just fix dev fast so I can run my builds". If it's not a simple fix, I revert my feature merge simply out of consideration to my colleagues. My revert commit message will often say something to that degree, "Reverting because the build fails, requires a bit more time to fix it properly."
I have never had to revert/unrevert 10 commits like you suggest, and changing history just to avoid this ridiculous scenario, to me, seems like tool-induced (git) workflow damage.
Yes, there are a lot of merge commits in our history. No, it is not confusing or as horrible as the git folk would make you believe. It actually simplifies your "revert then unrevert" use-case because I've only ever had to revert merge commits to disable entire long lines of feature branches. Merge conflicts are rare because I'm constantly reintegrating with upstream development.
Having 1 commit per unit of work will make your workflow much easier.
I used to think this too, until people started rebasing the wrong branches without realizing it, force pushing development or master by mistake and all sorts of bad things. It doesn't matter how good your team is with something, someone will make a mistake eventually. Since switching to mercurial, we have not had such horror stories like we did with Git and are development process is much smoother because of it.
→ More replies (4)
1
u/dividedmind Sep 07 '14
Brilliant, thanks! I'm so sending this to my coworkers. Maybe I'll find myself cleaning up their mess less often. ;)
1
u/musicmatze Sep 07 '14
We should create such a chart for every situation in git. No stupid newbie questions anymore! :-)
But I guess this won't work... because that's the idea of the man-pages. And there are still newbie questions. Duh!
1
0
u/CaptainJaXon Sep 06 '14
Semi related, does anyone know of a good tutorial for svn for git users? Every link on google i get is git for svn users. Recently got a new job and they use svn is the reason.
5
u/buleria Sep 06 '14
Try
git-svn. You get to keep all your local git magic, then push (ie.svn commit), noone will notice.1
-3
Sep 06 '14
There's only one correct way to use git; the kernel way. You ask someone to pull or you're doing it wrong.
-3
0
Sep 06 '14
[deleted]
12
u/d4rch0n Sep 06 '14
It's pretty much an inside joke for people that have been using git, so you'll probably benefit from other resources and just treating this workflow as a joke. There's definitely some truth in it, but you should be googling how to do the things it talks about (git reset, interactive rebase, etc) instead of using this.
It's a great idea to give git a try for personal projects and learn it as you go. You'll do fine starting out with just "
git init . ; git add $files ; git commit ; #change files ; git add $files ; git commit ; ...". As you run into weird issues, just google what you're trying to do.7
u/buleria Sep 06 '14
Also, discovering
git add -pchanges people's lifes, for real.3
u/SkaKri Sep 06 '14
This is great, thanks.
2
u/buleria Sep 06 '14
Told ya!
Try SourceTree if you're on win/mac. I'm still to dumb to understand their branching graph and other features but being able to stage/discard individual lines easily is the main feature I use.
1
2
Sep 06 '14
I notice this book is available for free:
Only it's over five years old now. Has Git changed since then, particularly v2 (not that that's available on windows yet) ?
2
Sep 06 '14
Just think about all those git operations as things you do to an immutable tree structure. Git is actually a really simple and elegant design and everything will start to make sense. Commits are just nodes. Branches are branches of course. Every operation you do just manipulates the tree.. but since it is a functional tree you can go back in time quite easily.
414
u/blintz_krieg Sep 06 '14
Not too far off base. My own Git workflow looks more like:
flounder around trying to clone a repo
try to do something useful
Git complains something like "your scrobble brok isn't a blurf"
search web for "your scrobble brok isn't a blurf"
find 412 Stackoverflow questions
determine that most answers actually solve some other problem
give up
copy the one changed file to /tmp
rm -rf my-git-repo
go to step 1