Let Us Start With GIT!
You are working on your “once-in-a-lifetime idea™”. You wrote a lot of code. Your concept changed slightly though and you decided to rewrite huge parts of it. Then it turned out bad idea. You want to revert to what you had before.
“Oh no, I have not saved the last working version!”, you thought, “I would like to have it saved, but copying my files all over again and again… It takes so much time and I want/need to ship ASAP!”
There is someone in this world, who eradicated all your excuses for not having control version system and making regular backups. Meet The Right Honourable Grand Master Of The Linux Kernel, Linus Torvalds!
Linus created GIT. Linus also created Linux. As Linux kernel grew over decades, due to contribution from multiple institutional and individual contributors, handling this input and deciding which code should be accepted and which not became very tiresome job for Linus. This is why Linus decided to shake the world once again with GIT. Even if huge project, such as Linux kernel, was an inspiration for GIT, this tool is useful for any size and kind of projects. Even your learning one!
You can download GIT from here, but most of the time it will be included in your Linux distribution. If you are using Windows, it is also available via “Bash on Ubuntu on Windows”, so you might not need to download separate GIT for Windows installer.
It is also good to know github.com. Create an account there and receive unlimited online storage for your code. Free, if you want to keep your code public. If you work on a project where you prefer not to share your code, consider paid account or just stick to local GIT repository, that is when following the guide below, do not go further than step 5.
Once you create your account, you need to create the project from your GitHub panel. After that, you are good to set up synchronisation with your local GIT setup.
Launch your terminal window and let me introduce you some commands by giving you this short quick start guide:
- Before you start commiting, let GIT know who you are! This way, you also share this information with other contributors of your project. It is quite useful for collaboration, when you need to figure out who wrote each line of code.
git config --global user.name "My Name"
git config --global user.email "firstname.lastname@example.org"
- Initialise GIT in your project folder, i.e. create new local git repository:
- Add your project files to the folder in which you initialised GIT. If you do not have any project files yet, wait with the next steps until you have some.
- Add all your project files in the current folder to your repository. Let GIT watch them, so you do not have to:
git add .
- Note there is a dot after “git add”!
- Commit your current version of project to GIT:
git commit -m 'put some note here'
- Add remote repository to your project (i.e. GitHub in this case):
git remote add origin https://github.com//.git
- The step above might require you signing in to GitHub. Follow the instructions displayed in your terminal window.
- Push your precious code to the master branch of your external repository (I will skip discussing branches for now):
git push -u origin master
Yay, you made it! Now you see, all you need to know about GIT can be wrapped up with this picture:
Now, remember these commands and remember to use them!
If you are curious about restoring previous versions and branching, see multiple additional resources about GIT, like this.
If you are seeking how to revert your commits, i.e. Reverting to the previous version of your code, this article explains it in much detail.
Now you can start enjoying your more beautiful coding world again!
Thank you, Linus!
Featured image from: https://twodifferentgirls.files.wordpress.com/2013/11/mlwo999.jpg
Originally published at At The Back Of The Browser.