2016-12-15 - By Robert Elder
What is git and why would you want to use it?
- If you accidentally delete your code, using git can help you get it back.
- If you accidentally change your code and break something, git can revert it.
- Git lets you share and exchange code with other developers easily.
- If you want to know what recent changes you made to you code, git will show you.
- Git lets you backup your code easily to a remote server.
- Many more things!
Git is a piece of software that allows you to perform version control. Large software projects require some piece of software to keep track of all the different changes made to a code base in order to track things like: Who edited a certain file; what they changed; and how to get back to the original code if necessary. Git does all of these things and more, so it's not surprising that most large software companies use git!
Git can be used on any operating system through a number of different programs. You can use git as a graphical application:
You can also use git directly from the Linux command-line. This article will focus on a few common Linux commands for git, and why they are useful. The advantage you get by learning Linux commands as opposed to learning a particular GUI, is that you'll understand how all other GUI applications work with git. The git commands for Linux are also the same in Windows command-line git clients.
Your most commonly typed git command will be 'git status'. This command will show you information about the current state of your files in a git repo, and highlight any differences from the last time you used 'git commit'.
You'll also use the 'git add' command frequently. You need to use this command before you commit any of the changes that appear with 'git status'.
git add <name of file>
git add code1.c
This image shows how the output of 'git status' changes after you issue the 'git add' command:
You can use 'git commit' after you've used 'git add' to stage files to be committed. After you use 'git commit', you'll now have a saved local backup copy of the file, but the backup won't have been saved to a remote server yet. For example, if you use Github, 'git commit' won't automatically save your changes to Github. You'll have to use the 'git push' command for that.
git commit -m "You can type a helpful commit message here."
Another extremely useful git command is 'git diff'. This will show you line-for-line differences in your files.
In the image below, green means lines that are added, and the red lines are removed that were removed. The difference is comparing what the state of the files at the current moment, to the state they were in last time you did 'git commit'.
The 'git push' command is used to 'push' any of your recent commits to another version of your repo. The repo you push to is usually on a server somewhere (for example if you use Github). It doesn't have to be on another computer though, and you can in fact create many local repos and 'push' to them. if you push your changes to a local repo though, they won't be safe if your computer crashes. The basic push command is:
To be more explicit, you will usually do:
git push origin master
Git clone is used to make a local copy of a remote repository. For example, you can do
git clone https://github.com/RobertElderSoftware/recc.git
to make a local copy of one of my repos that includes a C compiler.
Other Awesome Uses Of Git
If you're still a beginner at git or Linux, some of this might section be difficult to follow, but it's important to show you what you can eventually do with git.
Once you learn about ssh and how to set up ssh config files, you'll be pleased to know that you can do git pushes and pulls through an ssh tunnel! This lets you securely back up and distribute your code to others so that the public can't see it.
To do this, you'll can add a git remote that corresponds to whatever name you configured in your ~/.ssh/config file:
git remote add <ssh-remote-name-here>
Another thing you'll might want to do, is set up your own 'remote'. This would basically let you host your own equivalent to Github's servers. Just do
git init --bare
in an empty folder. You can then go into your local repo and do
git remote add new-origin /path/to/new/repo
where the '/path/to/new/repo' is the place where you ran the 'git init --bare' command. This will give your local repo a reference to the new remote. You can then do
git push new-origin master
to push files into the new 'remote' that you just created.
This article skips many details of using git to its fullest potential, but my goal here has been just to show you why it's worth learning. The most important thing about git is that it is a much better substitute for copying files around, or storing them on a flash drive. If you plan to pursue a career in developing software, you should certainly learn how to use git because you're sure to encounter it in the work place one of these days!
A Surprisingly Common Mistake Involving Wildcards & The Find Command
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
The Most Confusing Grep Mistakes I've Ever Made
Use The 'tail' Command To Monitor Everything
An Introduction To Data Science On The Linux Command Line
Using A Piece Of Paper As A Display Terminal - ed Vs. vim
An Overview of How to Do Everything with Raspberry Pi Cameras
Why Bother Subscribing?