EBAVS/ Try to work hard with the latest technologies. We work with git for several years and we learned a lot.
This is a simple reference guide with commands that we use day to day from the beginning of a project.
Tabla de contenidos
Initialising Repo with Git
First of all, we create a project (actually we download skeletons from a composer, but this is another history or post):
1 2 | $ mkdir /var/www/example $ cd /var/www/example |
Then we initialise the git repository. This is important because git makes their local repository for local working:
1 2 3 | $ git init Initialized empty Git repository in /var/www/example/.git/ |
Working with Repository
When you initialized we can begin work, then create a sample file for testing:
1 | $ nano index.php |
then add simple code:
1 | <?php phpinfo(); |
now add this file to a repository:
1 | $ git add . |
or
1 | $ git add index.php |
Now we have a repository with some of the code. It’s time to move this code, we have code added but isn’t in the repo yet. Write:
1 2 3 4 | $ git commit -m "initial code submit" [master (root-commit) 6f30a7f] initial commit 1 file changed, 1 insertion(+) create mode 100644 index.php |
The files are committed to a repo, are stored in this and assigned a short guide identifier 6f30a7f for us.
Go to change index.php and see what happens in the next commit:
1 | $ nano index.php |
And change to:
1 2 3 | <?php echo "php file modified"; |
1 2 3 | $ git commit -a -m "modifications" [master 67e5f24] modifications 1 file changed, 3 insertions(+), 1 deletion(-) |
Look what we add -a parameter. This means that we change a file. If we only add files don’t need -a parameter.
We have our application ready to store on the remote server. We could make a remote in GitHub or in Bitbucket. I Have a preference for bitbucket because I can make private repos for free. But GitHub is good too.
When we make a repository, in GitHub for example, they make a new URL for the repository, the URL construction is:
https://github.com/user/repo.git
A user is your username and Repo are the repository name. For this example a real URL could be:
Then is time to add this remote repository to our local repository:
1 | $ git remote add origin https://github.com/ebavs/example.git |
We have connected now our local repo with our remote repo, time to move code to remote:
1 2 3 4 5 6 7 8 9 10 | $ git push -u origin master Username for 'https://github.com': ebavs Password for 'https://fromcouch@github.com': Counting objects: 6, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (6/6), 452 bytes | 0 bytes/s, done. Total 6 (delta 0), reused 0 (delta 0) To https://github.com/ebavs/example.git * [new branch] master -> master Branch master set up to track remote branch master from origin. |
And we have our repo working.
Summary
commands that we used:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # initialize repository git init # add all files in repository git add . # submit code, make commit git commit -m "initial code submit" #submit code, make commit git commit -a -m "initial code submit" # Add remote repository git remote add origin https://github.com/ebavs/example.git # submit code to remote repository git push -u origin master |
Update:
If you like it you can visit our next post talking about Git: