Hi! Today I’m going to write an article about basic Git usage in practice. I’m not going to go into depth of Git repository setup, but rather on developer’s side – what’s important to understand to start using it?
First of all, here’s the one and only goal:
Allow developers to work with Git as they did before it.
I’ll be explaining the logic through console approach because of clean informations you get, and you’ll get yourself familiarized with Git project workflow.
First, take a look at this:
Ok, I really haven’t show any rocket science here. I just show you a graphic that represents any repository. But it’s important to understand that only when each developer pushes his commits to shared repository, it will be available to his colleagues developers to pull them.
And you must know the difference between local and remote (shared) repository.
And now, take a look at this one:
Now, this is the best graphic I found that would in brief and yet comprehensive way show the workflow between the developer and remote repository. Don’t worry if you think that it’s too complicated, or if you’re scared of commands. It’s pretty simple.
Commands logic and flow
Like (almost?) with any other repository, Git is cyclic. It means nothing but you pull the changes from others, do your changes, commit them, and finally push them to repository for others. Then, you just repeat the process.
On to the fun stuff:
Start point commands
Well, regarding the commands, I’ll go through them by example as follows, as simple as possible (working on master branch only):
//This will get me updated on current online repository status $git pull origin master
Then, I’ll create my index.php in the document root, and add content to it, and go to console to add my file to the local repository:
//This will add file to my local repository $git add index.php
Then, I’ll make a commit with new file:
//This will add file to my local repository //And leave amessage "index.php added" $git commit -m "index.php added"
Finally, I’ll push my index.php with its contents to remote repository as follows:
//Push to the repository origin, on branch master $git push origin master
And there you go. Your first file added to Git. And as I wrote before, you now just need to repeat the process.
A bit longer list of commands
git add – adds a file to local repository
git commit – it will “save” current state of files locally, and prepare them for git push
git push – it will collect all of your current commits that havent been commited and push them to remote repository
git pull – combination of git fetch and git merge
git fetch – it fetches the remote changes to your local repository, but it doesn’t change your current source contents
git merge – it’s used for merging 2 branches into one, if theres no need for 2 anymore
git branch <new-branch-name> – creates a branch with name “new-branch-name”
git checkout or git co – used for switching between branches
git diff – if you fetch the changes from remote, you can then see the difference between your code current version and last one on remote
git status – shows you the current status of your repository (like changed / added / deleted files)
git log -n – shows you the n last commit messages with some other data
I haven’t gone into depth of project setup, I just wanted to show everyone how to use basic console commands. And I hope someone will find something useful in here!
git help <command-name> – it will show you the official documentation of selected command