How to use Git on our Linux server

1 QUICK START (Must Read)

ABOUT GIT

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

 

The following sections are a brief introduction to Git's basic features.

CREATING A REPOSITORY

To create a Git repository, all you have to do is log in to your Hosting account using SSH or Terminal, and then type the following command:

git init REPOSITORY

 

Replace REPOSITORY with the name of the directory where you want to create the repository (if the directory does not already exist, the git init command creates it).

To take a snapshot of all of the files currently in the directory, type the following command:

git add .

The git add command puts the files in a temporary staging area that Git calls the “index”. To commit the files to the repository permanently, type the following command:

git commit

A text editor opens, and you can type a commit message that describes the changes. Save the message, and then Git adds the changes to the repository.

You may receive the following message when you try to commit changes:

Please tell me who you are.

If you receive this message, type the following commands, using your own e-mail address and name:

git config --global user.email "[email protected]"
git config --global user.name "Your Name"

MAKING CHANGES TO A REPOSITORY

Git makes it easy to update a repository. After you edit a file or files, you update Git's file “snapshot” by using the git add command. To make the changes to the repository permanent, use the git commit command.

You can combine the git add and git commit commands by typing the following command instead:

git commit -a

You can even include the commit message on the command line by using the -m option. Replace message with your commit message:

git commit -a -m "message"

To view changes you have made to files in the repository since the last commit, type the following command:

git status

Similarly, to view a list of changes that have already been committed to the repository, type the following command:

git log

WORKING WITH BRANCHES

A single Git repository can maintain multiple development branches. For example, you could create a “testing” branch and a “production” branch to track changes for different versions.

To create a new branch, type the following command, replacing NAME with the name of the new branch:

git branch NAME

To see a list of all branches in a repository, type the following command:

git branch

The asterisk (*) next to a branch name indicates the current working branch. If you want to work on another branch, type the following command, replacing NAME with the name of the branch:

git checkout NAME

You can then make changes in the branch and commit them with the git commitcommand. If you switch to another branch, you will not see those changes in the branch.

Merging changes from another branch

To merge changes from one branch into another branch, type the following command. Replace NAME with the name of branch that has the changes you want to merge:

git merge NAME

The changes in source branch NAME are then merged into the destination branch (the current working directory).

CLONING A REPOSITORY

You can “clone” repositories. When you clone a repository, git makes an exact copy of an existing repository. For example, say you have a repository named project. To clone the repository into a directory named projectclone, you would type the following command, replacing username with your own account username:

git clone /home/username/project /home/username/projectclone

Now there is an exact copy of the project repository in the projectclone directory.

Suppose you (or another developer) make changes in the projectclone directory that you want to pull back into the original project directory. To do this, you would type the following commands, replacing username with your own account username:

cd /home/username/project
git pull /home/username/projectclone master

The git pull command fulfills two functions. First, it fetches changes from a remote repository (in this case, the projectclone repository). Second, it merges those changes into the current working repository (in this case, the project repository).

MORE INFORMATION

To view the official Git documentation, including references guides, tutorials, cheat sheets, and more, please visit http://git-scm.com/documentation.