Git is a distributed version control system. It was created by Linus Torvalds as a tool to support the development of the Linux kernel. Git is free software and has been published under the GNU GPL version 2 license.
The very popular github, gitlab or google source repository works on the git base. Each of these companies has a free and paid plan. But i will concentrate on free plan.
GITHUB — GITLAB –GOOGLE REPOSITORIES
Github – in his case, we can only run public repositories free of charge, if we want to use github privately (without providing sorce code publicly), we have to take into account fees. EDIT: GitHub Free now includes unlimited private repositorie.
Gitlab – we can have an unlimited number of private repositories. Gitlab is an open source project, thanks to which we also have the opportunity to create our own server.
Google Cloud Source Repositories – using the Always Free package we have the opportunity to create a private repository up to 50GB with access for 5 people completely free. In addition, we have the option to deploy a project, e.g. a web application, directly from the repository with a few clicks.
Why we need GIT?
If we work in a team, git can make work much easier, it allows you to preview what has been added without digging into hundreds of lines of code (git will tell us what has been done / changed in the code compared to e.g. the previous version). The basic command of git is commit which saves everything that we have changed in the code, we can check what has changed in a given commit at any time and even go back to it. Git has the ability to create so-called branch (s) on which we can add new functions and then test them without interfering with the so-called “master” code. For example, working in a group of 3 anyone can create a branch and test new functionality, after testing you do the so-called merg-which integrates the work of the entire team into a whole. Before merging the version can be tagged e.g. v1.2 and even if something goes wrong git will take us back to the previous version when we want it. For solo people, all the functionality of the guitar is also very useful.
The repository consists of three trees managed by git. Working directory | working directory (stores files) —— Stage | Index | (works as a waiting room) —– HEAD (last commit created)
To create a new repository, just create a folder with any name, e.g. mkdir ./test_repo, then enter cd ./test_repo and do git init, but if you want to work in the repository you created earlier, you must clone it using the git clone command.
git init //utworzenie nowego repozytorium git clone /path/to/repository //cloning of an existing repository
Git add and commit
After adding the file / code to the working directory, we add the file to index by:
git add <filename> //add a specific file git add * //adds all files in the folder except hidden files git add . //adds all files from the working folder together with hidden files that contain a dot at the beginning, e.g. ".test"
To commit the changes and add them to the repository (locally) we will use:
git commit -m "message" //In the message, enter, for example, added changes
Now the added files are in the head (local working copy), the repository has been created, the next step will be to send it to the remote repository we will do it using the command:
git push origin master //master indicates the main version of the repository, we can change the master to any branch in which we want to make changes
If you have not cloned the existing repository and want to connect to a remote server, you must use the command
git remote add origin <server> //<server> server address
In the case of Google Cloud Source Repositories, Github and Gitlab, we can create a repository directly on the website through the interface, clone the working folder to the computer and then make changes and send them to the server using the command git push origin master without the need to configure and enter the address of the remote server. In the cloned repository will find the hidden “.git” folder which stores the project settings.
The master branch is the default main branch when creating a repository.
We can create other branches to develop the project, test new functionalities, when we are finished we merge them back into the master branch.
git checkout -b test_branch //creates a new branch called "test_branch" and switch to it git checkout master //switches back to master git branch -d test_branch //removes the given branch in this case test_branch //the branch is not available to other users until it is sent to a remote repo git push origin <branch> // sent changes to a given branch
Update and merge repository
Żeby zaktualizować lokalne repozytorium do ostatniego commita, należy użyć komendy git pull w katalogu roboczym na swoim pc, komenda ta pobierze(fetch) i scali(merge) zdalne zmiany.
To update the local repository to the last commit, use the git pull command in your pc’s working directory, this command will download (fetch) and (merge) remote changes.
git pull //repo update from a remote server
To merge another branch with the active branch (e.g. master) we will use
git merge <branch> //<branch> branch name
After executing the commands, git tries to merge the changes automatically. This is not always possible and causes conflicts. You must then manually edit the files pointed to by git.
After change, mark them as merged with
git add <filename>
Before merging changes, you can view using:
git diff <source_branch> <target_branch>