Git Commands


Create a new repository on the command line

1. Creating repository

git commands Description
git init Initialize a git project
git add <filename> . or – means all new files
git rm —cached – Remove all files from cache
git commit Commit your changes to repository
git commit -a Combined the add and commit step
git status Check what the file changed
git diff See the changes of
git diff v2.5 HEAD Compare the current HEAD to v2.5

The commit you are currently on is known as the HEAD commit.

2. Version rollback

git commands Description
git log Show the logs(SHA)
git log –pretty=oneline Show the logs concisely
git reflog Get the version number(first 7 characters of the SHA)
git reset –hard HEAD^ Go back to the last version, –hard is used to reset the index and working directory
git reset –hard HEAD@{n} Go back to the nth versions before
git reset –hard version_number Reset to the specific version

3. Undo changes and delete files

git commands Description
git checkout HEAD <filename> Undo the file changes and recover the file to the HEAD


3.1 Delete commits

git commands Description
Git glog See commit ids
Git rebase -i commit_id Edit the commits from HEAD to commit_id (delete line to remove commits)
Git push -f origin branch Force push to origin branch

4. Remote repository

Step 1: Create SSH Key

ssh-keygen  -t rsa -C ‘your_email’ —> generate puclic/private rsa key pair

Step 2: Open github -> settings -> SSH and New GPG keys -> New SSH keys

Fill anything in the title and copy the to the key text box. Then click Add Key.

a. Add remote repository

git commands Description
git remote add origin Add remote repository
git remote add origin The same.
git push -u origin/dev dev <br>= git push -u origin dev<br>= git push origin dev Set upstream for git pull/status. dev branch will be created if not existed.
git checkout origin/dev –b dev Checkout to dev branch and set upstream to origin/dev. Contain the next command
git branch -u origin/dev dev <br>= git branch -u origin dev dev set up to track remote branch dev from origin.

After that, use git push/git push origin master

b. Clone the remote repository

git clone remote_location //clone_directory

Remote location can be local or online location, such as http/ssh. If there is no clone_directory, then the default directory is the root directory.

5. Create and merge branch

git commands Description
git checkout -b <name> Create and switch to the branch created. This command equals the following two commonds git branch dev and git checkout dev
git branch (-v) List all the branches
git branch <name> Create branch
git checkout <name> Switch branch and will automaticlly merge
git branch -d <name> Delete branch
git push origin -d <branch_name> Delete remote branch

On master branch

git commands Description
git merge dev Merge dev into the master
git merge –no-ff -m “…” dev Merge without fast forward mode
git merge origin/master Merge into the remote origin/master branch

6. Bug branch

git commands Description
git stash record the current state of the working directory and the index, and go back to the clean HEAD.
git stash list  show the working directory
git stash apply recover the working directory
git stash drop delete the WIP(working in process)
git stash pop equals apply + drop

7. Cooperation

git commands Description
git remote check the remote repository status
git remote -v check the detailed information
git remote remove <name> Remove the remote
git remote rename <old> <new> Rename the remote

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s