常用工具

Git常用指令

2019-05-07  本文已影响0人  张小Di

现在很多公司用的都是通过Git+Github来实现代码托管及版本控制,它们均是开源的,我们知道Github是一个面向开源及私有软件项目的托管平台。Git是分布式版本控制系统,Git可在官网下载,可根据自己使用的系统平台选择相应的版本下载。在windows平台上使用Git Bash工具。较以往的集中式版本控制系统,分布式版本控制不存在中央服务器单点故障的问题,因为每个客户端都存有仓库的镜像,及时某一台挂掉了,可以拉取其他端镜像文件并恢复。所以分布式版本控制更加安全可靠。

首先,Git与GitHub需要建立连接,是通过SSH认证来保证通信安全,故客户端先生成公钥,然后将公钥保存至服务器,后续Git连接服务器就不需要每次输入用户名和密码来验证了
  1. 获取公钥
    首先看之前有没有生成公钥,一般在c:/用户/pc文件.ssh,.ssh是否有id_rsa(私钥),id_rsa.pub(公钥)。如果没有,则需要生成公钥
    打开Git Bash面板,通过下方指令生成公钥
$ ssh -keygen -t 密钥类型 -C 注释
//密钥类型,默认ssh-2的RSA
//注释字段,一般设置为邮箱,方便用户标志密钥

查看并获取生成的公钥

$ cd ~/.ssh
$ cat 文件名
  1. 在GitHub配置公钥
    GitHub->setting->SSH and GPG keys
配置用户信息

当安装完 Git 应该首先设置用户名及邮件地址。 这样做很重要,因为每一个 Git 的提交都会使用这些信息,并且它会写入到你的每一次提交中,不可更改:

//初始化用户名和邮箱
$ git config --global user.name
$ git config --global user.email
//查看config配置信息
$ git config --list
//查看config配置中单个信息,如用户名
$ git config user.name
常用指令

克隆远程仓库文件至本地
可通过HTTP或SSH克隆项目至本地
HTTP:可直接 clone 项目,但在每次 push 时需验证用户名和密码。
SSH:clone 的项目你必须是拥有者或者管理员,且要在 clone 前添加 SSH Key。当再次push到远程服务器时就不需要输入用户名和密码了。

$ git clone 仓库地址(SSH/HTTP地址)

初始化本地仓库

$ git init

工作区文件添加至缓存区

//将工作区所有文件添加至缓存区
$ git add .
//将工作区指定文件添加至缓存区
$ git add 文件名
//将工作区所有文件添加至缓存区
$ git add -A

将缓存区文件提交至本地仓库

$ git commit -m "提交信息"
$ git commit -a

连接远程仓库,并推送本地仓库至服务器

//首先要连接远程服务器
$ git remote add origin GitHub仓库地址
//远程版本合并到本地版本中(如果远程仓库已有文件存在)
$ git pull origin master
//本地仓库文件推送至远程
$ git push origin master

查看信息

//显示工作路径下已修改文件
$ git status
//显示与上次提交版本文件不同
$ git diff
//历史提交记录
$ git log
$ git log --author="用户名"
$ git log -p 文件名
//查看所有分支
$ git branch
//查看所有远端分支
$ git branch -r
//创建新的分支
$ git branch 分支名
//产出分支
$ git branch -d 文件名
//切换分支
$ git checkout 分支名
//创建名切换到新的分支
$ git checkout -b 分支名

撤销本地修改

//移除缓存区所有文件
git reset HEAD
//放弃工作目录下所有修改
git reset --hard HEAD
//放弃指定文件的本地修改
git checkout HEAD 文件名
//重置未指定版本,并放弃该版本之后所有修改
git reset --hard 提交编号
//重置为指定版本,并将之后修改标记并添加到缓存区的修改
git reset 提交编号
其他

Git命令设置别名,如status

$ git config --global alias.别名 status

修改commit提交过的消息

$ git commit --amend "注释"

强制推送(本地项目可替换服务器项目)

$ git push --force origin

Git帮助信息,即相关指令

$ git --help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --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.

推荐文档:
Git让你从入门到精通,看这一篇就够了!
git实用知识梳理)(一)最基础的知识

上一篇下一篇

猜你喜欢

热点阅读