All Things Git You Can Absolutely Get Right the First Time
The thing about Git is even though you have no clue how it does what it does, you can still get everything right. And even if you know how everything works, you can still get every single thing totally wrong. I had to learn Git and use GitHub when I worked on a data science project, and just like everyone, I dreaded it from day one.
As soon as the course is completed, I uninstalled Git for Windows from my desktop and only used the GitHub repository to slowly make sense of GitHub Pages to host my own personal website, which is still a work in progress. (Trust me, it looks a lot better now.) I created this hub based on all the little things I didn’t get right when I first tried Git, but could’ve.
- How to learn git, or should I learn GitHub instead?
- No resets: setting up username and email
- Save a repo elsewhere
- Add a commit message, and then edit it!
- Find out where you are
In addition, if you need to display your code on a Word doc, you might also find this hub useful:
1. Git Makes More Sense When You Understand __________
Installing Git is the easy bit. Once you've got it in your machine comes the difficult part: how do you learn Git? For me, it was not a choice.
There is no wrong way to learn Git, but I am convinced that there is a right way. And it’s not learning by doing. That goes without saying. What I mean is to learn to think within a mindset that makes it easier to understand these new concepts. What I mean is to know what it is not.
How quickly can you learn Git?
Guides and how-tos are a good place to start learning about Git and GitHub. Git website has some comprehensive getting started guide; GitHub learning lab now has plenty of courses to learn to use the platform. But when you’re under a deadline and can’t tolerate technical drawings or jargons yet, it’s a good idea to find solace in the words of other coders and developers who have tried them before you. You can probably understand Git in just a few lunches.
Each code school will have its own tutorial, either in the form of a free course or a resources article. Some good ones are from Codecademy, Pluralsight (previously Code School), and Git Tower. Others are from the source code hosts: Bitbucket, GitLab, Digital Ocean. One long article is usually enough to get acquainted with Git and GitHub, except when you need more.
Other resources for first-timers:
- Try Git: All of the learning materials are now put in one page on Try Github.
- Learn Enough: A tutorial of tutorials with one part dedicated on how to learn enough Git to be dangerous.
- How to GitHub: A tutorial from Gun.io
- Think Like (a) Git: An entire website dedicated to helping you think like Git
- Git Immersion: A tour that walks you through the fundamentals of Git
- Articles: Getting Started with Git and GitHub by Codecademy1, An Intro to Git and GitHub for Beginners (Tutorial)2, A Beginner’s Tutorial to Git and GitHub3, etc.
Read about other things, too!
It seems that coders are realizing that once you understand the concept of X [insert another idea here], Git starts to make more sense. Years after learning Git, people are saying, for example, an understanding of the motivation behind Git’s design can help you learn it. Or, an understanding of why some commands worked, and some others don't.
Take a Quiz About Git: What It Is and Isn'tview quiz statistics
2. No Resets! Setting Up Global Username and Email
Before starting with any new projects, you’ll have to set up a global username and an email. It’s part of the getting started phase. Now this should be easy-peasy, but I actually had to try this several times to get right. The initial setup looks like this:
$ git config --global user.name "Your Name" $ git config --global user.email "email@example.com"
The first important thing is your user name is your name, not your GitHub username (although it could be). Get this setup right the first time, because this information will be used for every single repo you work on. Although I'm pretty sure there is a way to reset this, it's already too much for first-timers to be dealing with. Ditch the "--global" attribute to set up a username and an email for a single repository. Write down your name and email–along with the quotation marks–into the command line, like so:
$ git config user.name "Anonymous Blocks" $ git config user.email "firstname.lastname@example.org"
3. Clone That Repo Elsewhere
It’s frustrating to see new folders appearing randomly inside the C: directory. Sometimes when the cloning is done, I am left with the sad feeling of not organizing my project folders appropriately. It's what some might call feeling "disempowered". I didn’t know where the new cloned folder would pop up because I didn’t set it up to a specific path. I still don’t know how to do this, but I’ve learned that there is a way to organize your cloned repos: put them inside a separate folder!
The following code creates a folder named "Clones" inside the Downloads folder in the D: directory, and then clones a repo into that new folder. So the next time you clone a repo, you can save it into a new folder that you create manually beforehand. Maybe inside the Clones folder you want "Repo-1", "Repo-2", "Repo-3", and so on.
$ cd D:/Downloads $ mkdir Clones $ git clone https://www.github.com/username/repo-name.git D:/Downloads/Clones
Clone only a branch of the repo
In addition, if ever you need to clone only a certain branch of the repo, you can get that right on your first try, too. I've had one occasion when I didn't need the master branch and only needed to work on one of the branches. Although I didn't push that change, I learned that this code could make it happen (just specify a directory at the end if you need it inside a set folder):
$ git clone --single-branch –b branch-name https://www.github.com/username/repo-name.git
4. Add a Commit Message, and Make Edits
Saved changes on GitHub are called commits, and each of these changes should have an associated commit message that describes why the change was made. When you make a commit message via Git Bash, you’re actually only adding a title to your commit message. You’ll need to manually input the reasoning behind the change from the editor on GitHub.
After going through my old commits, I realized that I might have been too lazy to actually write a commit message. But actually no, it's not due to laziness. It's because I don't really know what a good commit message should look like, the format and the content. To edit your commit message, when you’re outside the editor mode, you can use the following code, and then follow the instructions to edit your message:
$ git commit --amend
5. Find Out Which Directory You’re Really In
This might seem like a super simple task, yet I didn’t know how to do this properly. Most of the times I’m just guessing that I’m in the C: directory inside a folder when I’m working on a cloned repo, but apparently there is a way to find that out for sure. Running ‘pwd’ on your bash will return the full path of your current working directory. Never get lost again.
$ pwd $ /d/folder/subfolder/current-folder
What Worked, What Didn't
It's been years since my first commit, but I still am under the same chaotic aura when I'm coding on GitHub. The last thing I want to do is make a mistake, but if we don't make mistakes we aren't really learning, are we?
These are just some of the very mundane things that I feel I could've done right the first time but didn't. There are a few other Git things that I could add to this list, but maybe for another day. Being the expert procrastinator that I am, I had to wait until the last day of October to start contributing to Hacktoberfest 2018. I've done 5 pull requests today, so hopefully they'll send me my goodie bags this year.
Every coder, new or experienced, must have an opinion about Git. What about you? What are some things you'd like to add to this list?
Be sure to bookmark some of the resources mentioned, for when you need to consult them in the middle of your coding session.
- (2018) Getting Started with Git and GitHub. Codecademy.
- (2016) Beginner’s Guide to Using Git and GitHub. Code Mentor.
- Meghan Nelson. (2015) An Intro to Git and GitHub for Beginners (Tutorial). HubSpot.
- Matthew Setter (2015) A Beginner’s Git and GitHub Tutorial. Udemy.
- (2018) An Introduction to Open Source. Digital Ocean.
If at first you don't succeed, call it version 1.0.
This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.
Questions & Answers
© 2018 Lovelli Fuad