So you have started a new project, congratulations!! It’s a very exciting time when you get to break into some new ideas and some new code, but let’s not forget to be smart about it. We have to make sure to save all of our changes so that we can keep track of what we have done. While just regularly saving your files is a good start, we want a little more than that.
That is where git comes in!! Git is a version control system. It will basically let us take snap shots of our project, so that if we ever make some changes we want to revert, we can just go back to one of these old snap shots. It is also great when you have multiple people working on the same code base, so that everyone will not be stepping on each other’s toes.
It is a great idea to use git even when working on a project yourself, you never know when you might need to go back and revert some changes. So how do we do it? First step is to initialize a git repository in your project. This is simple enough, just type the following line of code into the console while in your project directory.
This initializes a git repository so you can start using version control!! It makes a subdirectory in your project called .git which will handle all of the version control.
Now that we have our local repository how do we take snap shots of our project? To use the proper git terminology we just make a ‘commit’ of the project. When we commit, the changes we have made are saved to the local repository, but we have to tell it what we want to save! The simplest way to do this is to add all of the changed files by using the following line of code in the command line.
git add .
Once we have added or ‘staged’ our changes we can commit them with a message about what we have done with the following line of code.
git commit -m "put your message here!"
It is important to leave comments so that your teammates and even yourself can keep track of the changes. If you have made multiple changes but you don’t want to commit all of them, like we did with ‘git add .’ you can add files individually.
git add this-file.rb
git add the-other-file.html.erb
git commit -m 'just committing these two files'
There are also a lot of great tools to make commits easier. Plug-ins for VScode like GitLens can let you see all of the commits and the history of your repo. You can also stage commits and make your comments outside of the command line in VScode! just click on the git icon on the side and stage and comment the changes you need to!
These are the basic building blocks of building and tracking your git history so that you have an accurate history of the versions of your project. There are a few guidelines that you should try and remember to keep good practice.
Make sure to commit often and commit small changes. Committing large changes to the code can make it difficult to see all of the changes that have been made. For example, say I had a website and I built the entire user’s home page. And I also fixed a small bug that made text the wrong color on the home page. If I put these both into one commit, it might be hard for me to find the bug fix while looking through the git history. It is always best to have each git commit focus on one change in the project.
Also, it is very important to have a good comment on the commit. If your comment for a commit is ‘fixed bug’, what bug? where? what was wrong? why did you fix it? The more detail you can squeeze in while still being concise, the better. If someone is reviewing your changes, you want them to know exactly what you did and what changes they should be looking for.
So you have your repository, you have been committing regularly to it, and all the commits have good comments. All done right? Wrong! There are some great resources for storing, sharing, and managing our repositories! Probably the most common would be GitHub. On GitHub we can store all of our repositories remotely and we can access countless other repositories for open source projects!
To put your repository onto GitHub in the terminal run the following line of code
This will prompt you for your GitHub username and password, then it will post or ‘push’ your repository up to GitHub! This creates a remote repository that will track all the updates and versions that are happening on your computer. The remote repository will typically be the repository on GitHub, and you will have a local repository on your own machine.
Now that you have your project on GitHub, you can start playing with other projects you find on GitHub! When you find a project that you want to contribute to or play with we can do that too! First navigate to the repository of the project you want to work on, the you will see a green button at the top right corner that says code.
When you click on the green code button, you can then click on the clipboard (blue arrow in the above picture) which will copy the link to the repository. Then in your terminal type the following.
git clone [paste link here]
This will make a copy of the repo in your current directory. Then you will be able to play around with the project and learn from it as much as you want. Since a local repository was made when you cloned the project from GitHub, it will not change the remote repository on the GitHub page you got it from.
This should give you the stepping stones to start making your projects while integrating quick and easy version control! Happy Coding!!