In the previous guide, I have shown how to install Git. In this article, we will create a repository and run some basic commands with it.
Create a repository and some basic commands need to remember
1) git init
The git init command creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new empty repository. Executing
git init will creates a
.git sub-directory in the project root, which contains all of the necessary meta data for the repo.
git init # or git init [directory]
2) git clone
If you have an exist repository, you will only clone it. The git clone command copies an existing Git repository. This is sort of like svn checkout, except the “working copy” is a full-fledged Git repository, it has its own history, manages its own files, and is a completely isolated environment from the original repository. As a convenience, cloning automatically creates a remote connection called origin pointing back to the original repository. This makes it very easy to interact with a central repository.
git clone <repo> # or git clone <repo> <directory>
Clone the repository located at
<repo> onto the local machine. The original repository can be located on the local file system or on a remote machine accessible via HTTP or SSH. The second command will clone the repository located at
<repo> into the folder called
<directory> on the local machine. Example:
git clone ssh://[email protected]/path/to/my-project.git cd my-project # Start working on the project
3) git status
This command to view status of working directory and the staging area. If you have any changing in project then they will be shown. Example:
[email protected]:~/working/my-project$ git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: .gitignore protected/config/main.php no changes added to commit (use "git add" and/or "git commit -a")
4) git add and git commit
This command is used to add all changes in
<directory> in working directory for the next commit and commit them into your local repository.
git add <file> <file> # or git add -A # (add all of changing files) git commit -m <message>
[email protected]:~/working/my-project$ git add protected/config/main.php # or [email protected]:~/working/my-project$ git add -A [email protected]:~/working/my-project$ git commit -m "add main.php file"
5) git pull and git push
If you have a remote repository then
git pull origin <branch> command will help you update all of changing in branch of remote repository to local repository. I will talk about “branch” in the next article. With
git push origin <branch>, it will push all of changing in commits to remote
git pull origin master git push origin master
These are popular commands of git that you will alway use to work with Git.