Git系列:常用命令

IT技術派發表於2020-10-31

一.背景

作為一名程式設計師,怎麼能不懂Git那些常用命令呢?於是花費一點時間來總結Git命令。關於安裝的話,就不講了。

二.初始化和幫助

1.配置全域性的使用者名稱稱和使用者郵箱

git config --global user.name "使用者名稱" && git config --global user.email "郵箱賬號"

git config --list

git config user.name

[root@centos-master ~]# git config --global user.name "robin" && git config --global user.email "code9342@gmail.com"
[root@centos-master ~]# git config --list
user.name=robin
user.email=code9342@gmail.com
[root@centos-master ~]# git config user.name  
robin
[root@centos-master ~]# 

2.檢視幫助

git help -a 

git help <command> 

[root@centos-master ~]# git help -a
usage: git [--version] [--help] [-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/libexec/git-core'

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

'git help -a' and 'git help -g' lists available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.

三.站在構建者的角度上需要掌握的命令

1.建立一個倉庫

 

 

 

 

 

 2.基本命令

[root@centos-master git]# ls
[root@centos-master git]# mkdir my-project
[root@centos-master git]# cd my-project/
[root@centos-master my-project]# git init
Initialized empty Git repository in /opt/git/my-project/.git/
[root@centos-master my-project]# touch index.html
[root@centos-master my-project]# git add .
[root@centos-master my-project]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   index.html
#
[root@centos-master my-project]# git commit -m 註釋
[master (root-commit) ce8369e] 註釋
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 index.html
[root@centos-master my-project]# git status
# On branch master
nothing to commit, working directory clean
[root@centos-master my-project]# git remote add origin https://github.com/CodeInterface/my-project.git
[root@centos-master my-project]# git push -u origin  master
Username for 'https://github.com': CodeInterface
Password for 'https://CodeInterface@github.com':
Counting objects: 3, done.
Writing objects: 100% (3/3), 219 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/CodeInterface/my-project/pull/new/master
remote:
To https://github.com/CodeInterface/my-project.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
[root@centos-master my-project]#