Git常用命令——基本操作

Git常用命令——基本操作

Git命令总览

SwitchdeMacBook-Pro:git-tutorial switch$ git --help -a
usage: git [--version] [--help] [-C <path>] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

available git commands in '/usr/local/Cellar/git/2.9.3/libexec/git-core'

  add                       credential-store          index-pack                patch-id                  shortlog
  add--interactive          cvsexportcommit           init                      prune                     show
  am                        cvsimport                 init-db                   prune-packed              show-branch
  annotate                  cvsserver                 instaweb                  pull                      show-index
  apply                     daemon                    interpret-trailers        push                      show-ref
  archimport                describe                  log                       quiltimport               stage
  archive                   diff                      ls-files                  read-tree                 stash
  bisect                    diff-files                ls-remote                 rebase                    status
  bisect--helper            diff-index                ls-tree                   receive-pack              stripspace
  blame                     diff-tree                 mailinfo                  reflog                    submodule
  branch                    difftool                  mailsplit                 relink                    submodule--helper
  bundle                    difftool--helper          merge                     remote                    svn
  cat-file                  fast-export               merge-base                remote-ext                symbolic-ref
  check-attr                fast-import               merge-file                remote-fd                 tag
  check-ignore              fetch                     merge-index               remote-ftp                unpack-file
  check-mailmap             fetch-pack                merge-octopus             remote-ftps               unpack-objects
  check-ref-format          filter-branch             merge-one-file            remote-http               update-index
  checkout                  fmt-merge-msg             merge-ours                remote-https              update-ref
  checkout-index            for-each-ref              merge-recursive           remote-testsvn            update-server-info
  cherry                    format-patch              merge-resolve             repack                    upload-archive
  cherry-pick               fsck                      merge-subtree             replace                   upload-pack
  citool                    fsck-objects              merge-tree                request-pull              var
  clean                     gc                        mergetool                 rerere                    verify-commit
  clone                     get-tar-commit-id         mktag                     reset                     verify-pack
  column                    grep                      mktree                    rev-list                  verify-tag
  commit                    gui                       mv                        rev-parse                 web--browse
  commit-tree               gui--askpass              name-rev                  revert                    whatchanged
  config                    hash-object               notes                     rm                        worktree
  count-objects             help                      p4                        send-email                write-tree
  credential                http-backend              pack-objects              send-pack
  credential-cache          http-fetch                pack-redundant            sh-i18n--envsubst
  credential-cache--daemon  http-push                 pack-refs                 shell

git commands available from elsewhere on your $PATH

  credential-osxkeychain  lfs                     subtree

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
SwitchdeMacBook-Pro:git-tutorial switch$ 

常用Git命令 

SwitchdeMacBook-Pro:git-tutorial switch$ git
usage: git [--version] [--help] [-C <path>] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introduced a bug
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout   Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Reapply commits on top of another base tip
   tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository or a local branch
   push       Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
SwitchdeMacBook-Pro:git-tutorial switch$

基本操作

  1. git config (配置)

    1. 用户配置
      • git config --global user.name “Switch"
      • git config --global user.email “q547550831@outlook.com"
    2. 配置级别
      • --local[默认,高优先级]:只影响本仓库(会放在.git/config文件中)
      • --global[中优先级]:影响到所有当前用户的git仓库(会放在~/.gitconfig文件中)
      • --system[低优先级]:影响到全系统的git仓库(会放在/etc/gitconfig文件中)
    3. 配置别名
      • git config --global alias.shortname <fullcommand> :(可以使用git short name来完成fullcommand的操作)

  1. git init(初始化仓库)

    1. git init [path]
    2. git init [path] --bare

SwitchdeMacBook-Pro:git-tutorial switch$ git init
Initialized empty Git repository in /Users/switch/Documents/git-tutorial/.git/
SwitchdeMacBook-Pro:git-tutorial switch$ ls .git/
HEAD config description hooks info objects refs
SwitchdeMacBook-Pro:git-tutorial switch$

PS:.git文件夹中记录了该仓库的一切信息,非常重要。

SwitchdeMacBook-Pro:git-tutorial switch$ git status
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)
SwitchdeMacBook-Pro:git-tutorial switch$ 

  1. git status(查看状态变化,对状态的跟踪)

    1. 三对关系
      • 未跟踪 <————>已跟踪
      • 工作目录<————>暂存区
      • 暂存区<————>提交区
    2. 两种状态
      • 内容状态(工作目录<——>暂存区<——>提交区)
      • 文件状态(未跟踪<——>已跟踪)
SwitchdeMacBook-Pro:git-tutorial switch$ touch README.md
SwitchdeMacBook-Pro:git-tutorial switch$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

README.md

nothing added to commit but untracked files present (use "git add" to track)
SwitchdeMacBook-Pro:git-tutorial switch$ ls
README.md
SwitchdeMacBook-Pro:git-tutorial switch$ 

PS:未跟踪状态。

  1. git add(添加文件内容到暂存区,同时文件被跟踪,变成已跟踪状态)

SwitchdeMacBook-Pro:git-tutorial switch$ git add README.md
SwitchdeMacBook-Pro:git-tutorial switch$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

new file:   README.md

SwitchdeMacBook-Pro:git-tutorial switch$

PS:添加README.md内容到暂存区,变成已跟踪状态。

PS:可以使用git add 文件目录的形式对该文件批量添加,但是有时候里面有些文件,并不需要跟踪,那么这时候就可以通过配置.gitignore文件来忽略匹配文件(仅能作用于未跟踪文件)。

可以到 https://github.com/github/gitignore寻找gitignore的相应配置

  1. git rm(从暂存区删除文件)

    1. git rm --cached :仅从暂存区删除
    2. git rm :从暂存区与工作目录删除
    3. git rm $(git ls-files --deleted):删除所有被跟踪,但是在工作目录被删除的文件


SwitchdeMacBook-Pro:git-tutorial switch$ vim README.md
SwitchdeMacBook-Pro:git-tutorial switch$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

new file:   README.md

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:   README.md

SwitchdeMacBook-Pro:git-tutorial switch$ 

PS:修改了README.md文件之后,工作目录和暂存区同时存在着不同的README.md文件。


  1. git commit(根据暂存区内容创建一个提交记录)

    1. git commit -m 注释 :可以加入注释,使用log命令可以查看

SwitchdeMacBook-Pro:git-tutorial switch$ git commit -m 'initial commit'
[master (root-commit) 53a1a79] initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
SwitchdeMacBook-Pro:git-tutorial switch$ git status
On branch 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:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
SwitchdeMacBook-Pro:git-tutorial switch$

PS:暂存区的文件提交到了提交区,形成一个提交历史。
    1. git commit -a -m 注释 :直接提交 

SwitchdeMacBook-Pro:git-tutorial switch$ git commit -a -m 'full commit'
[master 4757e41] full commit
 1 file changed, 1 insertion(+)
SwitchdeMacBook-Pro:git-tutorial switch$ git status
On branch master
nothing to commit, working tree clean
SwitchdeMacBook-Pro:git-tutorial switch$

  1. git log(查看提交历史信息)


默认log形式
SwitchdeMacBook-Pro:git-tutorial switch$ git log
commit 4757e417cb400c7cb6a45985ec5317fd51efaba4
Author: switch <q547550831@outlook.com>
Date:   Wed Aug 17 21:21:34 2016 +0800

    full commit

commit 53a1a79890c2675aab41afe78c320a34c6ea7284
Author: switch <q547550831@outlook.com>
Date:   Wed Aug 17 21:15:58 2016 +0800

    initial commit
SwitchdeMacBook-Pro:git-tutorial switch$ 


单行log形式
SwitchdeMacBook-Pro:git-tutorial switch$ git log --oneline
4757e41 full commit
53a1a79 initial commit
SwitchdeMacBook-Pro:git-tutorial switch$ 


图状log形式
SwitchdeMacBook-Pro:git-tutorial switch$ git log --graph
*
commit 4757e417cb400c7cb6a45985ec5317fd51efaba4
| Author: switch <q547550831@outlook.com>
| Date:   Wed Aug 17 21:21:34 2016 +0800
|
|     full commit
|  
*
commit 53a1a79890c2675aab41afe78c320a34c6ea7284
  Author: switch <q547550831@outlook.com>
  Date:   Wed Aug 17 21:15:58 2016 +0800
 
      initial commit
SwitchdeMacBook-Pro:git-tutorial switch$ 

图状单行log形式
SwitchdeMacBook-Pro:git-tutorial switch$ git log --graph --oneline
*  
56fdf65 resolve
| \  
| * 101fda8 next five
| * 31397de next four commit
*
| 32dd26e three commit
|/  
*
4757e41 full commit
*
53a1a79 initial commit
SwitchdeMacBook-Pro:git-tutorial switch$ 


  1. git diff(显示不同版本差异)

    1. git diff :显示工作目录与缓存区的差异
    2. git diff --cached [<reference>] :暂存区与某次提交差异,默认为HEAD
    3. git diff <reference> :工作目录与某次提交的差异


  1. git checkout(将文件内容从暂存区赋值到工作目录)

    1. git checkout <file>:撤消本地修改
    2. git checkout HEAD <file>:将内容从上次提交复制到工作目录
  2. git reset(将文件内容从上次提交复制到暂存区)

    1. git reset HEAD <file>:撤消暂存区修改




-------------参考《网易云课堂.Java Web开发入门》


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值