A brief Introduction to Git
What is Git?
In general, Git is distributed version-control system for tracking changes in source code. Git Saves all of our history development, it allows us to collaborate our code with others and enables feature branching, which we discuss about later on.
Installation
A link for the Installation of Git — http://rogerdudler.github.io/git-guide/
In order to check if the installation went well — open the command prompt and write “git –version” , if you receive the following message:
Then git was successfully installed.
How does it work?
The Git Repository is like a container of our project’s files. We save our project in the repository, and each time we make a change in our project, we add a new version of it to the repository. Each version of the project that is saved is named ‘commit’.
For each version, we can view the changes that was made such as: the time and date of those changes and by whom they were made.
When we collaborate our code with others — we have a private(local) partition for each one of the writers/developers and one public (shared) partition that is available for all of the group. The manager of the group can define different permissions for each developer, i.e. which sections he allowed to change in the project, and which not.
Basic Git commands:
1. Initializing the repository:
In order to start a new repository, first you should choose your primary folder of your project.
Open the ‘Git Bash’ app that was installed in the beginning of this article, and navigate to the desired folder. Then, we initiate our repository with the command: “git init” , which creates us a git repository inside our project’s folder.
2. Add and Commit:
In order to add the project’s files to the repository, we need to perform an action named Commit. In order to do that, we first need to perform ‘add’. We write the ommand ‘git add .’ to add all the files in the folder to the git project (the dot is important). If you want to add only a specific file you should write ‘git add filename ’.
Afterwards, we perform the command: git commit -m ‘your text here’. The text you enter after the -m is the comment of the commit, which should describe generally the commit.
If it is your first commit — you should identify yourself, which means:
You need to apply the following commands:
- git config –global user.email ‘your email’
- git config –global user.name ‘your name’
And then perform the commit.
3. Log:
In order to see all the commits that was done in the project, the command is: ‘git log’
4. Status:
In order to see if there are files that was changed, the command is: ‘git status’
Branching:
We use branches to create separations in our codebase, so we can isolate our codebase and divide it amongst our team of developers. Each one of the team can create a different branch and work only on that separate partition. The main branch of each project is called ‘master’, and we can define the names of each branch that we create. In this example ‘your_new_branch’ represent the name of the branch.
The commands:
· git branch your_new_branch
· git checkout your_new_branch
creates a new branch and switch to it.
· git checkout -b your_new_branch
the same as above, with only one command line.
Merging:
merge the branches back into a single branch. This is how we put it all back together. If we run a merge, git will stuff all of our changes from our branch into one large merge commit that contains ALL of the branch changes.
The command:
· git merge your_new_branch master
Rebase:
Instead of doing a git merge, there is another option which is the rebase. What rebase does is take all of the commits on your new branch and move them on top of the master commits. Behind the scenes, git is actually blowing away the feature branch commits and duplicating them as new commits on top of the master branch. What you get with this approach is a nice clean tree with all your commits laid out nicely in a row, like a timeline. Easy to trace.
The golden rule of git rebase is to never use it on public branches. If you do that, what will happen is that the rebase moves all of the commits in master onto the tip of feature. The problem is that this only happened in your repository. All of the other developers are still working with the original master. The only way to synchronize the two master branches is to merge them back together.
The command:
· git checkout your_new_branch git rebase master
Wrap it up:
So, to summarize — when we want to create a new git project, the procedure goes like this:
1. initialize the repository in the main folder by the init command
2. perform changes in your code
3. add the changes to the project by the add command
4. save the project’s changes as a new version by the commit command
If you would like to work with other people on the project, it is best to create a branch to each one of you and later on to combine your work through the merge or rebase commands.
Now, what we do if we want to upload our project to a specific server?
We take for example the GitHub platform. To upload your project to GitHub, what you simply need to do, is to create a new repository with your profile (after you have a user) and then write in your git bush:
· git remote add origin ‘the url of your repository’
· git push origin master
Those 2 commands will upload your master branch to your repository in the GitHub.