Git 基础命令与相关术语


  笔记基本上整理自网站 Try Git,一套很不错的 Git 在线入门教程。

git init

To initialize a Git repository here


  • A folder used for storing multiple files.


  • A directory where Git has been initialized to start version controlling your files.

git status

See what the current state of the project is.


  • Files are ready to be committed.


  • Files with changes that have not been prepared to be committed.


  • Files aren’t tracked by Git yet. This usually indicates a newly created file.


  • File has been deleted and is waiting to be removed from Git.

git add <file>

Add files to the staging area.

Staging Area

  • A place where we can group files together before we “commit” them to Git.

add all

  • You can also type git add -A . where the dot stands for the current directory, so everything in and beneath it is added. The -A ensures even file deletions are included.

git reset

You can use git reset <filename> to remove a file or files from the staging area.


A “commit” is a snapshot of our repository. This way if we ever need to look back at the changes we’ve made (or if someone else does), we will see a nice timeline of all changes.

git commit -m “msg”

  • To store our staged changes we run the commit command with a message describing what we’ve changed.

‘-a’ option

  • Auto removes deleted files with the commit.
git commit -am "msg"

git log

a journal that remembers all the changes we’ve committed so far

git log –summary

  • see more information for each commit. You can see where new files were added for the first time or where files were deleted. It’s a good overview of what’s going on in the project.

git remote

git remote add <remote name> <repository URL>
git remote add origin

Git doesn’t care what you name your remotes, but it’s typical to name your main one origin.

It’s also a good idea for your main repository to be on a remote server like GitHub in case your machine is lost at sea during a transatlantic boat cruise or crushed by three monkey statues during an earthquake.

git push

Tells Git where to put our commits when we’re ready.

git push -u <remote name> <branch name>
git push -u origin master

The -u tells Git to remember the parameters, so that next time we can simply run git push.

git stash

Sometimes when you go to pull you may have changes you don’t want to commit just yet. One option you have, other than commiting, is to stash the changes.

Use the command git stash to stash your changes, and git stash apply to re-apply your changes after your pull.

git pull

git push <remote name> <branch name>
git pull origin master

Check for changes on remote repository and pull down any new changes.

git diff

A good overview of changes we have made and lets us add files or directories one at a time and commit them separately.

git diff HEAD

  • Show what is different from our last commit. HEAD points to your most recent commit by default.

git diff –staged

  • Look at changes within files that have already been staged.

git checkout – <target>

Changed files back to how they were at the last commit, namely get rid of all the changes since the last commit.

--: promise the command line that there are no more options after the ‘–’, avoid switching to the branch of the same name.

git branch

List local branches of now repository.

git branch <name>

  • Create a new branch.

When developers are working on a feature or bug they’ll often create a copy (aka. branch) of their code they can make separate commits to. Then when they’re done they can merge this branch back into their main master branch.

git checkout <branch>

  • Switch to certain branch.

git checkout -b new_branch

  • Checkout and create a branch at the same time.

git rm <file>

Not only remove the actual files from disk, but will also stage the removal of the files for us.

git rm -r folder

  • Recursively remove all folders and files from the given directory.

git merge <branch>

Merge your changes from the given branch into current branch.

Merge Conflicts

Delete Branch

git branch -d <branch>

  • Delete a local branch. -d won’t let you delete something that hasn’t been merged.

-f and -D

  • Force delete the branch that hasn’t been merged.

Add the -f(–force) option or use -D which combines -d -f together into one command.

Delete Remote Branch

git branch -r -d <remote name>/<branch name>
git push <remote name>: <branch name>
git branch -r -d origin/branch-name
git push origin :branch-name
  1. 1. git init
    1. 1.1. Directory
    2. 1.2. Repository
  2. 2. git status
    1. 2.1. staged
    2. 2.2. unstaged
    3. 2.3. untracked
    4. 2.4. deleted
  3. 3. git add
    1. 3.1. Staging Area
    2. 3.2. add all
  4. 4. git reset
  5. 5. Commit
    1. 5.1. git commit -m “msg”
    2. 5.2. ‘-a’ option
  6. 6. git log
    1. 6.1. git log –summary
  7. 7. git remote
  8. 8. git push
  9. 9. git stash
  10. 10. git pull
  11. 11. git diff
    1. 11.1. git diff HEAD
    2. 11.2. git diff –staged
  12. 12. git checkout –
  13. 13. git branch
    1. 13.1. git branch
    2. 13.2. git checkout
    3. 13.3. git checkout -b new_branch
  14. 14. git rm
    1. 14.1. git rm -r folder
  15. 15. git merge
    1. 15.1. Merge Conflicts
  16. 16. Delete Branch
    1. 16.1. git branch -d
    2. 16.2. -f and -D
    3. 16.3. Delete Remote Branch