Here is the Jaime Barriga foolproof way for not screwing anything up when you want to push and pull from a github repository.
You may be here because you want to learn the ways of the master. You may be here because you royally messed something up.
I am here because I am a genius that's been working with git for years, and I programmed for a company that had thousands of engineers working on one repo and I didn't screw anything up.
I deserve a medal for this guide.
Note: From here on out, if you screw up at any point, that's ok and don't panic. There is a section at the bottom to get you fixed.
The steps for how to not ruin your pushes and pulls in git:
0. git status
- Are there any files listed here in green or red text? If yes, you are going to need to deal with those first. Consider proceeding immediately to the end of this document.
1. git branch
- This better have an asterisk next to master or you are already in a bad place. If it doesn't, type git checkout master
2. git pull
- This is going to make sure you have everyone's code as of right this second. This will prevent you from lot of pain. I am a genius.
3. git checkout -b whatever_you_want_to_call_this_new_branch_where_you_will_work_on_stuff
- A branch is a place for you to keep your work without screwing up anyone else, including yourself. The `checkout -b` command makes a new branch and immediately puts you there. Safely. Without worry.
5. git do all the work for me
- This isn't an actual git command but I laughed because I am a god of comedy.
Once the work is done:
6. git merge origin/master
- This helps you merge your conflicts with people who are the kind of people that cut you off in the supermarket checkout line. This guide is for pushing and pulling, not resolving your other problems so figure this out on your own time.
7. git add --all
- Adds all the files to a magical place in git that says "these are the files I care to send to the mothership". Or it doesn't. Who cares. Just type the command.
8. git commit -am "This is the commit message that I saw on jaimebarriga.com as a good commit message to use"
- Prepares these files for actual launch, and attaches that message in quotes to all the files you just added. Bonus: If you deploy to production with this message you'll get a promotion. Guaranteed.
9. git diff origin/master
- This is your last chance to make sure you didn't accidentally add a file or type the wrong things. Consider it a confessional, but it will tell you what your sins are. Page up/ Page down are your friends here, and the Q button quits in case this is too Byzantine of a labyrinth for your simple mind.
10a. git push
- Finally. This will send your files to your github repository, where you can be done with them forever. It will almost definitely block you here, and that's fine. It gives you a command to run at the bottom, so just copy/paste that command and run it.
10b. Go to the website that it tells you to go to.
- When you're there, you can click the green button to "Create pull request." A pull request is when the CSS team blocks your critical PHP code deployment for 2 weeks because they just changed their standards for how buttons in admin tools look, and at the end they admit that they were wrong and approve you.
10c. When your pull request gets approved
- Click the green dropdown and select "Squash and merge." This will make it so your commit messages don't spam everyone and you will be loved and celebrated by all.
11. git checkout master
- Go back to your master branch so you can work on something else.
12. How many of you noticed step 4 was missing?
- If you did, you're fired. Clean out your desk.
You are done.
All of this might be overkill, but I've been using this process for years, and I can now safely say that the only issues I've had are because I didn't follow my own advice and I screwed something up.
Addendum 1: What about git rebase?
This is how you thank me?
Addendum 2: Help I screwed something up!
We're going to solve this the way I solve all my problems: with the surgical precision of a suicide vest.
1. Find all the files you were working on and save them somewhere else on your computer
- A git diff origin/master or a git status might help you out here but it might not. Just try not to lose anything because we are going to nuke some stuff.
2. Nuke the folder (on your computer, not on github) containing the entire repo you are working on.
- Basically, type `git status`, then `cd ..` and hit enter, until you see a fatal error. The folder you want to delete is one lower than you currently are (where the git status error is happening)
3. Put the repo back on your computer with `git clone whatever_the_github_website_tells_you_the_git_clone_line_is_on_the_github_website`
- When you pull up the website for the repo you were working on, there is a green button in the top right to clone. Stick that after the words `git clone`
4. Put the files you saved back where they came from
- This will restore order and justice.
Go to my articles about pizza or game design.