git is a verson control that works offline. Every thing is is stored is a folder called .git is the same directory as the project and after you are done making all your changes you can send it to the sever.
The reason that git is getting more popular than any other version control is:
- fast
- offline working
- make a patch file for every change
but there is one big problem with it: learning.
you need to spend at least a full day to learn everthing but after that every thing will be smoot and easy.
The way that developers update the server is through ssh and the way that people can download the project is with http or git daemon. git daemon is simular to http but a bit faster and it will use the port 9418.
An other reason that we will use git daemon is for people unfamilar with apache, they won't make a hole in the sever by using http. git daemon is only for downloading project not sending to server. It can be done but It's not safe uless you want every one change the project as they please.
In the tutorial we will do 3 diffrent projects and basic commands.
- installation
- basic commands
- the verson control and project files are public
- every one that works on the project has the files but only there version control is on ther server and project files are not
- both version control and files are on the server but the files are for a website and version control is in a directory that people have no access to it
- alias
- git daemon
1) installation
for windows and mac you can download the executable and for linux you can do
apt-get install git
for the project where the version control is public you need to intall git web and git daemon:
apt-get install gitweb git-daemon
2) basic commands
if you are on any other command line other that git you need to run this command so every thing will get color full and easeir to read
git config --global color.ui true
as git trusts every one (because if you have ssh access to there server you are trust worthy) it doesn't know who is making changes so it's better spesify yourself so every one knows who is making changes
git config --global user.name "name" git config --global user.email "you@youremail.com"
3) the verson control and project files are public
is this example everthing on your project folder or any other developer and the server project flolder will be the same.
so now you are is the project folder where you can see all the files and folders related to only 1 project
to start git you need to:
git init
and it will make the .git directory. for any reason, if you didn't like git you can do:
rm -rf .git
to delete everything realted to git
git status
will give you any useful information at the time, if any files content are changed or you didn't commit yet
git add .
will tell git that it need to look for changes in every file and forlder, but you can only add spesific folders like so:
git add /folder1
now you maked all your changes and you want to submit it so:
git commit -m "initial commit"
-m will add a message to the the commit, it's like a title of and article, so if you added feature x to your project you can do
git commit -m "added the cool feature x"
after your first git add . you can do
git commit -am "hello"
to add new files and commit at the same time it is basiclly git add and git commit
if you want the log of your commits you can do:
git log
everthing until now was offline so for adding it to the server first you will go to the server and make the desired folder and then just git init is this example the directory will be /var/project1 Now back the your computer you need to tell git where is the sever:
git remote add origin ssh://mysite.com:/var/project1 #or git remote add origin ssh://192.168.1.14:/var/project1
in every git project you can have several branches, you start with one called master for the first time to send files to the sever you can do:
git push origin/master
but after that it better to do:
git fetch origin/master git fetch master git push origin/master
git fetch will download the latest changes on the server and git merge will add all your changes. like this we can avoid any posibilities of error> there is and other command git pull
git pull origin/master git push origin/master
git pull is a git fetch fallowed by a git merge, yes it's faster but the reason that you want to use git fetch and git merge instead of git pull is to see if you need to do any last minute changes or not.
now files are on the server and you want an other person to download every thing from the server, the command for the is:
git clone ssh://mysite.com:/var/project1
now if several people want to work on a new feature and don't want to bug other people the create a branch
git branch
will give branch status, if new project only master is in there,
if you want only branches on the server
git branch -r
and if you want list of all branches (local and server)
git branch -a
now for creating a branch called brch1 we can do:
git branch -b brch1
this will create the branch and switch to it
to delete a branch:
git branch -d brch1
now to go back to the master branch:
git checkout master
checkout the the command for switching between branches
git diff
will show you diffrent stuff that was added or removed
diffrence between branches:
git diff git diff master..brch1
you can also see the diffrence between comits:
git diff 0da94be 59ff30c
and you can create patch files with it
git diff 0da94be 59ff30c > my.patch
to apply a patch you can:
git apply my.patch
to view the log diffrence of 2 branches,only stuff that not not merged, what is is brch1 that is not in master
git log brch1 ^master to get a more graphical view of commit in the command line:
git log --oneline --graph --all --decorate
4) every one that works on the project has the files but only there version control is on ther server and project files are not
in this version all the commands are the same but when your are in the server,instead of creating a folder for 1 project and
git init
you create the folder for several projects and then
git init --bare project1.git
this will only upload the changes not any files (everything in .git folder)
6) alias
this can allow you to use an alias for a long command
git config --global alias.lol "log --oneline --graph --decorate"
become git lol
or
git config --global alias.serve '!git daemon --base-path=/var/www/gserver/ --export-all --reuseaddr --informative-errors --verbose'
will become git serve
7)git daemon
to start git daemon we just have to type git daemon but there are several thing that we have to spesify like what the the projects folder
the fallowing command will use all projects in the folder /var/www/gserver
--base-path=/var/www/gserver/ --export-all --reuseaddr --informative-errors --verbose
--base-path will direct to the folder so if you have the project /var/www/gserver/a.git to clone it we have to clone git://mysite.com/a.git --enable=receive-pack will allow every one to push to the server (not safe) --reuseaddr will not wait for time out (everything becomes faster) --informative-errors --verbose will show all the connections and errors --export-all we use all the projects found
if you don't want to the git daemon for all projects in the folder just don't use the --export-all and all an empty file called git-daemon-export-ok to the git folder
touch git-daemon-export-ok