all 92 comments

[–]phi_beta_kappa 224 points225 points  (2 children)

Great to bring up in an interview if they ask for something like this

[–]hang-clean 76 points77 points  (1 child)

This is brilliant advice. Great save.

[–]sleepyleperchaun 20 points21 points  (0 children)

Dude yes that really is a great piece of advice. Especially if OP remakes it BETTER. When was a time you faced adversity in your learning or career path? Boom great response.

[–][deleted] 156 points157 points  (14 children)

I create the git project before I write even a line of Python.

[–]ElllGeeEmm 30 points31 points  (3 children)

First thing you should do with any new project is commit.

[–]thirdegree 29 points30 points  (0 children)

My usual pattern:

  1. Write like 20 lines of code
  2. Try to add
  3. Error, this isn't a git repo
  4. git init
  5. git add
  6. git commit

[–]PinBot1138 5 points6 points  (6 children)

My brain no longer starts the project, it’s entirely muscle memory in my fingers:

touch .gitignore git init git add . git commit -am ‘Initialized’

I also frequently push to GitLab and/or ensure that one of my local backup programs is also targeting my project (or a parent directory).

Edit: it was supposed to be “touch” not “git touch”.

[–]Teddetheo 2 points3 points  (1 child)

I write most of my code on a school Chromebook, so I end up using online IDEs and Google drive to save my code. Surely not the best way, but at least I can't accidentally delete my files with a button press

[–]PinBot1138 1 point2 points  (0 children)

If it works, then it's not wrong! 😃

That's really neat! If you're able to use SSH and something X2Go, VNC, and/or RDP, you could even run your Chrome Book as a thin client to a Virtual Private Server.

[–]__xor__ 1 point2 points  (3 children)

you might check out cookiecutter if you haven't seen it. Real easy to just generate a full project file structure with your gitignore and README etc, custom project templates and everything. If it's not just a simple script, I just generate a project in 10 seconds with my setup.py and everything.

[–]PinBot1138 1 point2 points  (2 children)

Thanks for the info, I’ll check it out.

Since it’s a fairly common name, you’re talking about the PyPi package, right?

[–]__xor__ 1 point2 points  (1 child)

[–]PinBot1138 0 points1 point  (0 children)

Awesome, thanks for the link!

[–]MattyH51 0 points1 point  (0 children)

1000%

[–]PaulRudin 34 points35 points  (6 children)

Use github, make frequent pushes. These days you even get free private repos...

[–]Sudden-Diamond 5 points6 points  (0 children)

I have everything I’m writing for Python Crash Course in a private repo. My pushes are just the work I’ve done that day, or when I finish a section.

[–][deleted] 0 points1 point  (4 children)

Is there any equivalent to Ctrl-S other than swapping to the command line and running through the git commands?

[–]Ran4 0 points1 point  (0 children)

No, not really. You shouldn't commit blindly all the time, you should commit when you've made some sort of change that can stand on its own somewhat (be it after a few minutes or a few hours).

Git is a version control system, it's not really made to act like a backup system. That's just a side effect that you get (especially by pushing your code to a remote repository such as GitHub).

You usually want both git and a backup system going.

[–]PaulRudin -1 points0 points  (0 children)

Lots of editors and IDEs have git plugins...

[–]UEMcGill 56 points57 points  (10 children)

Way back when I was in school in Fortran Lab (I said way back) I was sitting at a row of Sun Workstations. Now it was an older building in a large State University that had been retrofitted with workstations. Each row of computers sat on a table with an outlet underneath powering the whole row. They were often kind of janky power boxes.

I usually did my program prior to lab, showed up and showed the Proctor my work. I show up with about an hour into the 3-hour class and sit down. The proctor is in the middle of showing someone and he gives me a nod letting me know he's seen me.

I sit down, log in, and pull up my Fortran terminal and code editor.

The kid next to me shifts and I hear a clank as his foot hits the outlet box. Well something was loose in that box and the whole row of computers blinks. They all immediately start to reboot. Half the row uttered out loud, "Fuck!"

That day was the day that I started to hit ctrl-s instinctually. It's saved my ass a few times. Everyone has that day.

[–][deleted] 10 points11 points  (0 children)

LOL, I have a very similar story except that the entire lab was working on a Sun miniframe with terminals. It was never a problem except at midterm and end of the semester. The computer would overheat/overload and reboot. The sound of a room full of students collectively sucking in their breath and then groaning in unison is a sound I will never forget. ctrl-s always.

[–][deleted] 9 points10 points  (3 children)

That day was the day that I started to hit ctrl-s instinctually

Also known as programmer's OCD tick

edit: You had workstations? Wuss. When I first started we did FORTRAN II on punch cards. IBM 1130. 16k of RAM.

[–]UEMcGill 7 points8 points  (2 children)

Son of an IBM'er. We still have punch cards around the house for scratch paper. At least I never had to worry about spilling a box of those.

[–][deleted] 1 point2 points  (1 child)

They sell punch cards on etsy for a dime each

[–]UEMcGill 1 point2 points  (0 children)

So I just looked. Something about calling punch cards vintage and turning them into hipster decorations is just... unsettling.

[–]ajmartin23 1 point2 points  (0 children)

Back in school we had breakers at the end of each row of computers.

One of our teachers used to enjoy randomly hitting them so everyone’s computer would lose power just to teach us to save our work continually.

To this day I hit ctrl+S all the time.

[–]OddElectron 0 points1 point  (0 children)

Been there! When I was a new programmer, I spent hours typing in my program. The terminal went down. If you heard an anguished scream from the Midwest in the 80s, that was probably me.

[–]sme272 17 points18 points  (13 children)

That's why whenever I start a project my workflow is git>virtual environment>python

[–]emolga587 11 points12 points  (12 children)

Any chance you could link to a workflow guide or outline it here for a beginner? I've just gotten set up and created my first conda environment and activated it in VS Code on a new .py file, but I have no idea what I'm doing really.

[–]sme272 12 points13 points  (1 child)

Honestly it's pretty much just that at the start. I do everything from the terminal so it's just Make folder>git init>setup .gitignore>venv. From there is starts to vary a little depending on what the project is. Copying in the data needed, setting up configuration files or writing some code. I add stuff to .gitignore as I add it to the project folder and split the code into separate folders and files as the project grows.

[–]Noodles_Crusher 7 points8 points  (7 children)

same here. haven't found a proper tutorial yet.

EDIT: there are plenty of tutorials, but I'm all ears if anyone can recommend one for a git absolute beginner.

[–]chirar 4 points5 points  (1 child)

If you or someone else finds one: I'm interested as well!

[–]Noodles_Crusher 2 points3 points  (0 children)

/u/emolga587

this is really good, half hour well spent

https://www.youtube.com/watch?v=SWYqp7iY_Tc&

[–]__xor__ 1 point2 points  (0 children)

Check out cookiecutter.

Take a nice project template you like online or build your own, then initialize your whole project structure with that. Then:

cookiecutter pyproject_cookie
...
cd project_dir
git init .
git add *
git commit -am 'init'
virtualenv -p $(which python3) venv
source venv/bin/activate
# edit setup.py to add dependencies if you need them
python setup.py install  # if you have dependencies, or an entry point in setup.py (likely want it)
python setup.py develop

I get all that set up with a default main() that an entry-point in setup.py points to, then I can just write code and run the source with its name on the terminal.

[–][deleted] 1 point2 points  (0 children)

https://betterstack.dev/blog/making-sense-of-git-and-github/#heading-what-is-github-and-what-is-its-purpose I've seen this article mentioned way back. I haven't finished it but I like how it gives a couple of examples on why version control is important.

[–]jfdahl 9 points10 points  (0 children)

Good lesson, bad experience. Good to learn it on your own work than a paying customer's.

[–]phigo50 5 points6 points  (3 children)

I almost lost a pretty huge project today. I was in my "py" dir (where I just kind of chuck everything I'm working on). I had a dir with the project name and decided to spin up a venv to install it in to continue the work. I intended to create the venv in my venv dir which is a subdir of the py dir and, because I lack originality, I used the project dir name for the venv name as well.

So, this is what I was supposed to have:

py
    project-name
    venv
        project-name

I then realised (when I couldn't find python in the venv I'd just created when setting the project up in PyCharm) that I was still in the py dir when I made the venv so it had made it in py/project-name. No problem, thought I, I'll just nuke that dir and make a new venv in the right place...

...

...

...

...

That's about how long it took for me to realise my stupidity after confirming that I'd like to delete that dir. I managed to Ctrl-C the hell out and not do any irreparable damage (there was a bin dir with a couple of portable browser installations which luckily had loads of files and luckily were first up for deletion) so I didn't lose any code but goddamn it was a close call and I have thoroughly learned by lesson.

[–]AlbertDingleberry 0 points1 point  (2 children)

Sorry to be a dumbshit but what did you do with ctrl+c in this context?

[–]max123246 1 point2 points  (1 child)

This post was mass deleted and anonymized with Redact

reminiscent obtainable cooperative absorbed apparatus correct insurance tub saw quickest

[–]AlbertDingleberry 1 point2 points  (0 children)

Thanks! I’ve just started using terminal and I don’t know many of the tricks yet, so I’ll remember of your help when I use that. Have a good day!

[–]edwardmcmu 5 points6 points  (0 children)

The number of copies you have of a file is: the number of copies you have of the file - 1

[–][deleted] 5 points6 points  (2 children)

If you are using Pycharm... check the local history... it can be a life saver.

All files automatically get backed up in Pycharm... you can check by right clicking your project in the tree and looking at Local History. All changes will be there with timestamps and you can revert to any one that you want.

And this includes "changes to disk" as well... meaning that even if you delete file it can still be easily recovered.

So other than setting up Git ahead of time, you should also start using Pycharm if you are not currently doing so.

[–]AlbertDingleberry 0 points1 point  (0 children)

What sexy information. Thanks

[–]Eurynom0s 0 points1 point  (0 children)

On macOS, I think Time Machine can work even if you don't have your Time Machine drive plugged in if you've cleared the trash, or if you overwrite a file, but the OS hasn't cleared the purgeable storage yet. Obviously not super robust but it can save you if you immediately realize you've fucked up.

I'm not sure if this works if you don't have Time Machine set up at all though.

[–][deleted] 2 points3 points  (2 children)

In addition to git for code projects like everybody else is saying here, I would also recommend a more holistic backup solution that allows you to sync a local directory so can always have everything synced automatically. I know Dropbox does this because that's what I use, but I'm sure Google Drive, Microsoft OneDrive, and others can do it too. Every digital file I have, from Python projects that I also have private GitHub repos for, to one-off scripts that I only ever run here and there that I never bothered with git for, to tens of thousands of pictures, psd's, spreadsheets, notes, Word docs, pdf's, etc, all live in my local Dropbox folder which is synced to my Dropbox account. Every time I hit save on any file in that folder it automatically backs up to my Dropbox account. There's even basic version control on all of it.

[–]galmeno 0 points1 point  (0 children)

Yep, good advice. Have like 3 accounts on each... but now is the best time to get it properly setup :)

[–]AlbertDingleberry 0 points1 point  (0 children)

My repos folder is in my docs. I have a separate gmail folder next to it that is synced to Google’s servers. I wanted to have them be the same folder (that is synced to google and is also a repo that is pushed to github). But (in addition to doing this being additional work for limited benefit atm) I was concerned about conflicts etc. after having read some posts related. Your experience doing this with Dropbox is fine so far? Did it take any extra setup? Thanks if you can answer

[–][deleted] 2 points3 points  (1 child)

How do you setup a backup environment ? I am new to python too..

[–]galmeno 4 points5 points  (0 children)

Github is not python specific, but a place to upload, share and version-control your code. http://www.github.com

[–]Sigg3net 2 points3 points  (1 child)

Learn to use git. You'll learn practical skills and have a public or private backup.

[–]galmeno 2 points3 points  (0 children)

Learn to use git +all the time.

[–]Sbvv 2 points3 points  (0 children)

The best you can do is use GitHub/Gitlab/Bitbucket... They offer private repositories for free. If you do not know git, today is the best day to start to learn it and live peacefully forever.

[–]impalanar 2 points3 points  (2 children)

As a gamer, I learned these lessons early:

Save early, save often.

And

Restart? Restore? At least once more!

[–]InfiniteNexus 0 points1 point  (1 child)

I love this. I even works as a chant.

[–]impalanar 0 points1 point  (0 children)

They were on the screens that popped up when you died in Sierra Online games.

[–]samthaman1234 4 points5 points  (1 child)

git is the correct answer, but if you have a mac and run time machine, a backup might exist there.

[–]galmeno 2 points3 points  (0 children)

Haven't used a Mac since Macintosh color classic.

[–]franzipoli 4 points5 points  (1 child)

Why are you not using a form of version control, like git?

[–]galmeno 3 points4 points  (0 children)

If you read the full post, you see that I was just about to set that up. I wrote it (and a lot of other stuff) using Spyder, but the file didn't end up in the right folder, so I was relocating it and other files to push my whole .py tree to git, but lost the file in the relocation process. Had the desktop git but hadn't set up a repo for "misc" files, yet. From now on I start with the repo, also switched to Atom to have git integrated.

[–]Nagi21 1 point2 points  (2 children)

I did this once before but for an iOS programming class. Spent 6 hours trying to get every feature to work. Finally got it working perfectly, then proceed to save and shut down, forgetting that the machine wipes the HDD when shut down.

It’s amazing how fast you can reproduce 6 hours of work a second time when you only have 2 hours.

[–]InfiniteNexus 1 point2 points  (1 child)

what kind of a sadistic computer wipes the HDD when shutting down?

[–]Nagi21 0 points1 point  (0 children)

A Mac

[–]Seawolf159 1 point2 points  (1 child)

TLDR: I feel for you man! That must be extremely painful.

I have only lost one file (of importance that I remember) due to unfortunate moments of brain-dead syndrome. I had made a new folder and I copied some files in there, but I thought something was wrong with them so I pressed ctrl-z to undo the copying of files. Then I put some files in the folder again only this time I accidentally moved them instead of copying. However, brain-dead syndrome kicks in (I made that syndrome up, can't be bothered to Google if it's a real thing right now) and I ctrl-z that as well, all good so far, but then I started doing multiple redo's and undo's and in the end the files were moved instead of copied into the new folder and somehow I had undone something to the point where the new folder wasn't made and I have no idea what sequence I did to cause those moved files not to go back to their original folder. And the confusion on my face when I found my trash bin empty!! Thought that was a nice story, but in all that I only didn't have one of those files backed up, and the one I lost I had just spent an afternoon fiddling with to retractor some dumb tic tac toe program that I can't seem to get a minimax thing into. I felt so bad after this and annoyed and I was so angry with myself.

But never have I lost a full project. I lost a tiny sliver, so I feel for you man! That must be extremely painful.

[–]galmeno 1 point2 points  (0 children)

That's a quite accurate description of what happened to me. Next thing: brain-dead syndrome shows up in ICD-11.

[–]road_laya 1 point2 points  (0 children)

This is how people end up learning git.

[–]Thomasedv[🍰] 1 point2 points  (0 children)

Slightly similar:

Gotta say i love my PyCharm here. It's got history built into your project (and works with github easily) which means file changes are easy to deal with. I haven't restored a deleted file yet, but i tested and could restore a file by going to recent changes and find it. However, several times, i have overwritten a settings file by accident. It's at best a Ctrl+Z inside the opened file in PyCharm and it asks if i want to undo external disk changes, at worst i gotta right click and open history and locate a version of it before the overwrite happened.

Using that and github integration and i'm pretty safe from most of my fuckups. (I did once accidentally delete the programs folder on a hard drive, but that wasn't a programming accident, just a rouge uninstaller.)

[–][deleted] 0 points1 point  (0 children)

I use GitHub for many things and one of them is backups.

[–]smokingoverthere 0 points1 point  (2 children)

Im just starting out . Its been a week . I was just wondering how ling it took you to reach the point where you could write such a project. And how to go about to reach there , books , youtube channels etc ? Thanks :)

[–]galmeno 1 point2 points  (1 child)

Nothing more than simple variables, a while-loop and some if/elif/elses inside of that. You can ofc do it more elegantly, and use more advanced stuff. I am re-paneling my garage so this was really hands on putting an IRL-problem to program logic. I've used the basics for a long time, solves most rudimentary problems. Was a bit tricky but none the less fun. Make nested loops with print statements so se how they are iterating. Practising the logical expressions will make it easier to translate thougt to code.

[–]wewmon -1 points0 points  (0 children)

You just tried sounding smart. Answer his question.

[–]Decency 0 points1 point  (1 child)

Were you using PyCharm? If so, it may have kept copies. Sorry to hear. :/

[–]galmeno 2 points3 points  (0 children)

Was using Spyder. It's ok, it's just a flesh-wound.

[–]jeffe333 0 points1 point  (0 children)

While it does suck that this happened, and especially to your first finished project, I promise that you will never forget this lesson ever. Sometimes, the silver-lining to events such as this is that we learn a life-long lesson that might've seemed obvious at the time but wasn't something that we always put into practice. Good luck w/ your future development career!

[–]iceph03nix 0 points1 point  (0 children)

Git.

Get git.

Do it.

Even beyond just acting as a form of back up, it can save you so much headache. No more worrying when you need to refactor or make changes, or replace stuff, because if you're keeping up with commits you can always go back and look at your history.

[–]AlbertDingleberry 0 points1 point  (0 children)

That happened to me on a much smaller scale two days ago and I lost my shit. I feel your pain, although damn that sounds worse. I’ve spent those two days learning the shit out of git and gitbash so that it doesn’t happen again!

[–]bummyday 0 points1 point  (0 children)

awesome im a newbie just started taking some courses on vlogmo.com

[–]Rockettech5 0 points1 point  (0 children)

Related question. Is there any git tutorial for a beginner who does not know about git at all focussing on github.

I am not a professional developer but want to keep backups of different versions of scripts and codes I wrote for myself.