git命令最佳实践

Create a repository

From scratch – create a new local repository

1
$ git init [project_name]

Download form an existing repository

1
$ git clone my_url

Observe your repository

List new or modified files not yet committed

1
$ git status

Show the changes to files not yet staged

1
$ git diff

Show the changes to staged files

1
$ git diff --cached

Show all staged and unstaged file changes

1
$ git diff HEAD

Show the changes between two commit ids

1
$ git diff commit1 commit2

List the change dates and authors for a file

1
$ git blame [file]

Show the file changes for a commit id and/or file

1
$ git show [commit_id]:[file]

Show full change history

1
$ git log

show change history for file/directory including diffs

1
$ git log -p [file/directory]

Working with branches

List all local branches

1
$ git branch

List all branches, local and remote

1
$ git branch -av

Switch to a branch, and update working directory

1
$ git checkout my_branch

Create a new branch called new_branch

1
$ git branch new_branch

Delete the branch called my_branch

1
$ git branch -d my_branch

Merge branch_a into branch_b

1
2
$ git checkout branch_b
$ git merge branch_a

Tag the current commit

1
$ git tag my_tag

Make a change

Stage the file, ready for commit

1
$ git add [file]

Stage all changed files, ready for commit

1
$ git add .

Commit all staged files to versioned history

1
$ git commit -m "commit message"

Commit all your tracked files to versioned history

1
$ git commit -am "commit message"

Unstages file, keeping the file changes

1
$ git reset [file]

Revert everything to the last commit

1
$ git reset --hard

Synchronize

Get the latest changes from origin (no merge)

1
$ git fetch

Fetch the latest changes from origin and merge

1
$ git pull

Fetch the latest changes from origin and rebase

1
$ git pull --rebase

Push local chages to the origin

1
$ git push

Finally

When in doubt, use git help

1
$ git command --help

Or visit https://training.github.com/ for official GitHub training.

参考:
Git Commands and Best Practices Cheat Sheet,Simon Maple