For those of you not familier to Git, Git is a modern version control system. This means you are able to revert bak to an earlier versions if you experience a bug and so forth. The advantages of Git is staggering, and that is why the majority of developers use Git as a version control system.
Presuming you have already setup your Git account at either GitHub, Bitbucket, GitLab or any other Git repository manager. This tutorial will tell you how to proceed from where you currently are in the process.
For a more thorough read, check out Atlassians guide for git.
In this tutorial we will run through:
- Create your first Git repository
- Making a haunting choice: GUI or Terminal
- Setting up you Git environment locally
- Pushing your first changes to your git repository
Create your first Git repository
There are 2 ways to create a Git repository. Either through terminal commands or through the git repository manager site. The setup process will very from site to site, but it commonly asks for a repository name, description and if you want a public or private repository.
All the settings can be altered after creating the repository so its really isnt all that important what you input, but its good to get an idea of what to include.
We will learn to alter README and .gitignore later in the tutorial so you will get a deeper understanding of what they do later on.
I personally always create my repositories from the git site and not through the terminal because of two main reasons:
- I get better control of the information from the git site compared to the terminal. I can make a longer command with options to control the information, but its still a bigger hassel.
- I currently have 4 or 5 different git accounts at different sites, and my config file looks like a mess. Therefor if i enter the the command without the correct user information the repository will be created a repository at the most recently used git account.
All you have to do to create a repository through terminal is enter this command:
# This is a comment # git init <project directory> git init servebolt
Making a haunting choice: GUI or Terminal
After making your first repository you have two choices: GUI(graphical user interface) or Terminal commands. If you created your repository using Terminal commands you are probable comfortable enough to continue using that. For terminal git check out our glossary here!
Most of our tutorials focuses on terminal use, but if you want to use a GUI here is a well functioning one: GitKranken.
There are a few disadvantages to GUI, because you are not give the full range of attributes you are with terminal commands. However GUI is a good way to get a feel for what Git is, if you are not familier to the world of Git.
Setting up your Git environment locally
Setting up your environment locally using terminal command is easy. All you have to do is navigate to you git repository within the manager site of you choosing. Here you will often find a button or some text indicating you can find a ssh url to clone you repository locally. After you have located this clone url, you can go to your terminal and navigate to the directory you want to locate you repository. In the terminal enter the command underneath with your information.
git clone firstname.lastname@example.org:username/path/to/my-project.git cd my-project # Start working on the project
The gitignore file indexes files that are not supposed to be pushed to your repository. This can be a large variation of files and is often unique from project to project. Although there are common filles that you should be indexed in gitignore for every project.
There are 3 types of files you should banish with the gitignore:
- Files that should remain unique from developer to developer (E.g: database connection)
- Files that take opp a lot of space, that aren't necessary to you repository such as image uploads(Not static images like logos).
- When using any platform for developing a site, you dont want your upload images during testing in the repository unless you plan on using the same database and files locally and remotely (server). This is to decrease the amount of space wasted on the server for unused images that where implemented during testing.
- Files related to you computer or developing application
- I personally use a MacBook for developing, combined with the JetBrain applications. Both of this create files to keep track of directory location, files data and a lot more. You dont want this in your repository because its unique to your computer and not relevant for the server or another developer. If you are using npm or other package manager, you also dont want all the modules on the server, because it is only used while developing the site and not while the site i live.
Remember, gitignore is your friend - It will help you keep you repository clean and functioning.
I always start my project by planing and adding my gitignore. You can easily erase files after they have been commited if you dont want them on the repository anymore by entering the command below followed by adding them to your gitignore.
git rm --cached -r somedir or somefile.txt
Pushing your first changes to your git repository
Pushing you changes to a remote repository is easily done by executing 3 commands.
git add . git commit -m "your comment to the changes" git push --all
That's it! You will occasionally experience problems when working in teams about merge conflict and being behind the remote branch, to insure that it doesnt occur too often, make sure you allways pull from the remote repository before making any changes. This will insure that you are up to speed before commiting any changes.
git pull --all #Or you can be more spesific about branch git pull origin master