Prior to my start as a developer at the Nerdery it had been over a decade since I’d used any sort of source or version control software (don’t judge). When I started here I learned that source control was an integral part of our development process and needed to get back up to speed. After nine months of constant use I finally feel like I’m really “getting it” thanks to some patient mentoring and lots of trial and error. Along the way I’ve picked up a few things that I wish I had learned / understood / heard going in that have since made my daily use of source control – GIT in particular – a lot smoother.
1. Eliminate one step.
There are a lot of you who might say that this first tip has a limited value, but it saves me nine keystrokes every time I commit a change. Based on XKCD’s “Is it worth the time” chart it’s a great investment.
I get tired of typing my username and password every time I do a “git pull” and / or “git push”. Thankfully my colleague Aaron Canon showed me a quick way to make it 50% easier.
Instead of getting a repo this way:
git clone https://github.com/AWESOME.Repo .
I can add my username to the repo URL:
git clone https://firstname.lastname@example.org/AWESOME.Repo .
Now any time I do a Push or Pull request all I’m prompted for is my password. This works for github as well as corporate or custom git installations. Just remember not to do this anywhere someone else might need to work.
2. Stash and Pop
When I’m working on projects with a team of people there end up being times when someone comes over to collaborate. This usually requires I stop what I’m doing and get their code from the repo.
When I first started in git I would commit my changes in whatever state they were in and do a “git pull” to see their code; time consuming and often requiring me to deal with conflicts or errors from incomplete code. Then I learned…
This simply takes everything I haven’t committed yet and sets it aside. Then I do a “pull” to get the most current repo without my changes. I can collaborate at will. Once that’s done I type
git stash pop
This brings all my changes back to where they were; ready to commit and push at will. I’ve read that there are times when I will get a conflict that needs resolving when doing a stash but I have yet to see it. Overall this simple process has saved me tons of effort within development teams.
3. Add All The Things
After adding tons of content in WordPress, adding plugins, or building out a theme I sometimes end up with lots of files to add before committing. I’m embarrassed to say I did them one at a time before my colleague, Thomas McMahon, showed me this
git add .
Just like that, all untracked files are added and ready for a commit. By The Way: this isn’t always very smart. But there are times when it makes sense. Practice responsible “Add” and your git life will move much quicker.
Note: I won’t lie; My 3rd item was actually about Rebasing. I make sure that all my repos have it. But as I sat down to write I find that my explanations are a lot like the so may on the web – wordy and confusing. I’ve started reading Pro Git though so i hope to understand it soon.
One More Thing:
A great deal of “knowing” is simply knowing where to look. For source control questions I’m constantly referring to the following resources.
So Git Going (sorry). And I hope these three simple tips will save you – the new Git user – all the time I lost before I learned them.