What? Why? How?
What: Git is an open-source, widely used Version Control System (VCS). Much like any VCS, Git has its two primary uses which are (a) to allow teams to work on the same codebase at the same time and (b) to save in permanent memory the codebase at different points in its development. Check out this link for more info on what Git is exactly.
Why: But why learn Git in particular? There are other options of VCS that you could use: like Mercurial or Sub-Version, but Git is by far the most popular. Some claim Mercurial is more accessible and extendable (say if you wanted to tweak a feature to better suit your style), but the main principals are the same as in Git. Nonetheless, because Git is the most popular that means there will be much more help available online and many open-source projects will be using it, so I heavily recommend starting with Git. Check this page out for a bit more info on how the existing VCS differ.
How: There are hundreds of tutorials for Git out on the internet, so I won’t reinvent the wheel or just copy-paste those here — especially when lots of people have already done a fantastic job. Instead, you can use this page as a quick directory for some good links to tutorials and as a guide for figuring out where to start and how to develop your skills a bit further.
Learn the Basics
What’s the difference between Git and GitHub? A quick thing I’d like to clear up is the subtle difference between Git and GitHub because I’ve noticed that there can be some confusion in how they differ. Git is the software that you install on your computer and that lets you work with other people who are using the same software on their own machines. Git is free and open-source and is what runs the pushing, pulling, branching, etc. GitHub is an online service that hosts projects that use Git. It is a popular host for open-source projects (since it lets you host these for free), and it even adds lots of helpful features for managing teams of developers and product development.
Ok, so how do I start? The first thing you’ll want to do is install Git on your computer:
- Windows: Install Git for Windows on your machine here
- MacS: Download and run the most recent installer from this list
- Linux: Use one of these commands in your terminal.
Then what? After you’ve installed Git, you can work your way through the course material of Software Carpentry’s Git introduction. This will introduce all the basic features of Git such as committing changes, pushing/pulling and resolving merge conflicts.
I recommend looking on Atlassian’s Git tutorial for an explanation of branches as well (Atlassian owns a competitor product to GitHub called Bitbucket).
This is so much to keep track of… It definitely is. If you forget how to do something, you can always simply check on the internet or keep a cheatsheet at hand for the most common commands. I like Atlassian’s which you can find here.
Am I done? Once you’ve gone through these you’ll be have seen the majority of what you need to be comfortable with Git! Then it’s simply a matter of practice!
There are a few ways you can get your head around using Git. The most straightforward is to simply jump in the deep-end and start a little, fun, no-stakes project where you use Git, and query the internet for solutions to the questions you encounter along the way. This lets you learn features as you need them and lets you practice the all-important skill of searching the internet for answers to your software-related questions.
Another great tool, which is a great way to experiment with some Git commands, is GitHub’s Learn Git Branching interactive tutorial. Or if you want a sandbox version where you can do what you want, try the Visualizing Git tool.
A few more commands
Git allows you to be very flexible in how you manage your repository and commit history. There are lots of little commands that could help you achieve whatever contrived objective you may have, but in the day-to-day you’ll need a limited set. I find the following ones useful now and then and they can be handy to keep in your toolbox:
git stash: Stashing is a bit like the copy-past of Git. See here for an explanation.
git revert: Resetting and reverting have similar uses in that they can undo commits, but each have specific contexts in which they should be applied. See here for a comparison between the two.
git rebase: Rebasing, similar to merging, is another (more advanced) way to integrate commits into a repository's history. See this blog post for a brief introduction, then read Git website's article on rebasing for a much more in-depth discussion. The
rebasecommand also has an interactive mode which is very helpful in cleaning up your commit history: see here for an explanation.
One of the beauties of Git of course is how it allows many developers to work on a single project. The two elements you’ll need to do this are Pull Requests and Forking.
- Pull Requests are a key tool and process in team software development. They are a formal notification that a feature or bug-fix is ready for integration into the master branch. Atlassian gives a nice explanation.
- To Fork a repository is to create your own copy of someone else’s repository. This lets you make changes to a project where you might not already have editing rights. Later on, if you’d like, these changes can be turned into a pull-request for the original project. See GitHub’s introduction on Forking to get started.
There is no one way to use Git, but there are a few things that you should always do. These are explained here. You’ll notice that all the suggestions focus on how to write clear and explicit commit messages. This is important so that when other developers look over your code, they can understand not only how your code changed, but why. Watch this lecture for a good explanation of why it is important to have a clear Git history.
Beyond having a clear development process there are no set rules for how you should use Git. That said, one important part is to have a consistent workflow. A workflow is an agreed-upon manner of using Git across a team. Atlassian gives a good explanation and compares a few types.
So there you go!
Hopefully you’ll find this collection links useful! Git is an amazing tool that can take some time to get used to, but I assure you it is worthwhile. My last piece of advice is to take everything step-by-step and do not hesitate to ask the internet. Good luck!
Thanks to https://markdowntomedium.com/ for help with importing this article.