As a software developer, one of the first tools you MUST learn is Git. When I first started coding I would be so happy to get something working. When I moved onto the next step in my code I inevitably broke the entire app and spent hours just trying to get back to the point I was at before where it was working. If I had known about “Version control” my life would have been so much easier.
Version control is helpful because it saves the entire history of your project, allowing you to move back and forth through the life of your project and it makes working with a team of multiple developers much easier. In this post we’re going to cover setting up and installing Git, and the basic commands like staging, making a commit, and removing files from your staging area. In future tutorials we cover more advanced topics like stashing, branching, reverting, and more. Check out all our Git tutorials here.
Luckily installation is pretty self explanatory and well documented in the Git manual. I won’t be going through installation quite yet as every computer system is slightly different. Once you’ve gone through the manual and have the install complete, go ahead and open a terminal and follow along below.
Making a Commit
Typically each project will have its own Git repository. You can tell a git repository exists by the hidden .git/ directory in a project. Let’s make a demo project to mess around in, “initialize the Git repository”, and create a file to use.
There are 3 “states” of a commit all of which you can move forward or backward.
- Unstaged: Git is not keeping track of the changes to these files. “Discarding” the file takes that file back to how it was at the last commit.
- Staged: this is the preparation step to commit changes to the history of the project.
- Committed: Changes have been recorded in the history of the project. I typically commit when I have a completed feature or have fixed a bug. After committing the cycle starts over.
$ mkdir tutGit $ git init $ touch index.html #create a file however you want.
Cool. Now in order to save any changes we need to change our file, so add some text to your file and click save.
Now that a file has been changed, run $ git status in the terminal to see what happened. You should see “untracked files” with your index.html file.
This means a file has been changed but Git isn’t keeping track of it. You need to stage and commit by running the commands below. $ git add file_name or $ git add . using a period will add ALL the files to the staging area.
$ git add .
Now commit your changes to the official “history” of your project by running git commit:
$ git commit -m "made a commit"
Congrats! You’ve officially stored the current state of your project into git. Now if you ever need to fall back to the way the project is now you can. If you’re working on new stuff and break things you can easily revert back to this point. We’ll learn how to do that in a future Git tutorial.
Undoing changes when you haven’t committed.
Say you’ve made some changes to a file or added new files and saved these changes. If you want to UNDO the changes and go back to how the project was at your last commit. Easy! You just have to either “unstage” the files if you’ve already added the changes to git or “discard” the changes if you have not added the changes to git.
Luckily that’ super easy to do. Git even tells you right there in the command line how to do it. It’s usually just as easy in a GUI. Here’s an image of my text editor VSCode with buttons to discard, stage, add, and view the difference between the versions of the file.
In the command line it also tells you how to discard changes you’ve made. Below shows instructions to move a file from staged to unstaged, and from unstaged to discarded:
$ git restore --staged file_name #if the file is already staged $ git restore file_name #if file is NOT staged.
Getting your code into Github
Github is basically Git but on the web. If someone breaks into your house and steals your laptop and throws it into a lake then all your code will be at the bottom of a lake and you’ll never get it back. If you pushed your code to Github, GitLab or BitBucket you can just download it from the internet on a shiny new laptop.
We’ll use Github. Steps are super simple. I assume you already have an account.
Create a Github repository (Go to github.com and click the “new” button”
Create a name for your repository and then click “Create Repository”.
Once you click “Create Repository” You’ll get a set of instructions. Just type those commands into your command line and you’re good to go. If you already have a commit then run the bottom section. $ git remote add is what creates the connection between your local computer and Github. $ git push -u origin master pushes the code up from your computer to the internet! Note: The password they ask for is the same password you use to login to github.
Once you’ve pushed your code refresh the page and you’ll see all your code stored in the web.
That’s all for now! I really hope this tutorial was helpful. We’re going to go through some more advanced GIT material in the next posts. You can check those out here.