git命令

git工作过程图解
在这里插入图片描述

Workspace:工作区
Index/Stage:暂存区
Repository:仓库区(或者本地仓库)
Remote:远程仓库

一、新建代码库

# 在当前目录新建一个Git代码库
$ git init

# 新建一个目录,将其初始化为Git代码库
$ git init [project_name]

拉取代码之前需要配置ssh key,有了权限之后,拉取代码不需要输入密码,比较方便

  • 下面以码云ssh key配置为例
    1、git配置ssh公钥,打开git命令终端【git bash 窗口】
ssh-keygen -t rsa -C"xxxxxxxx@163.com"

2.之后连续三次enter(也可以设置相应的密码)

# 显示(表示保存文件【需要生成多个公钥可以自定义命名】)
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/admin/.ssh/id_rsa):
//直接enter

//密码(一般不需要,直接enter)
Enter passphrase (empty for no passphrase):

//重复输入密码(继续enter)
Enter same passphrase again:

在这里插入图片描述
3、如上图所示,生成公钥成功,在码云仓库添加公钥

  • 上面信息中有公钥和私钥保存的路径,一般都是默认路径
  • /c/Users/用户名/.ssh/
    在这里插入图片描述
  • 将公钥保存到码云上
    在这里插入图片描述
    在这里插入图片描述
  • 将公钥复制到公钥填写位置保存即可
    在这里插入图片描述
# 下载一个项目和它的历史代码
$ git clone [url]
$ git clone git@gitee.com:li_myheart/django_frame.git
Cloning into 'django_frame'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

二、配置

  • Git设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。
# 显示当前Git配置
$ git config --list
# 编辑Git配置文件
$ git config -e --global
# vim编辑器一样操作,编辑配置内容

在这里插入图片描述

# 设置提交代码时的用户信息
$ git config --global user.name "jack"
$ git congfig --global user.email "xxx@163.com"
  • 更改之后信息nameemail address会改变
    在这里插入图片描述

三、增加/删除文件

# 添加指定的文件到暂存区
# 先切换到文件所在的路径
$ git add file1 file2

在这里插入图片描述

# 添加指定的目录到暂缓区,包括子目录
$ git add dir_name
# 添加每一个变化前,都会请求确认
# 对于同一个文件多处变化,可以实现分次提交
$ git add -p

# 删除工作区文件,并且将这次删除放入暂缓区
$ git rm file1 file2 ...
# 停止追踪指定文件,并且该文件会保留在工作区
$ git rm --cached file
# 改名文件,并且将这个改名放入暂缓区
$ git mv file_original file_renamed

四、提交代码

# 提交暂存区到仓库
$ git commit -m "discription message"
# 提交暂缓区的指定文件到仓库区
$ git commit file1 file2 -m "discription message"
# 提交工作区自上次commit之后的变化,直接到仓库区
$ git commit -a
# 提交时显示所有diff信息
$ git commit -v
  • 显示的详细信息
    在这里插入图片描述
# 使用一次新的commit,替代上一次提交
# 如果代码没有变化,则用来改写上一次的commit的提交信息
$ git commit --amend -m "discription message"
# 重做上一次commit,并包括指定的文件的变化
$ git commit --amend file1 file2

# 代码提交到本地仓库后推送到远程仓库
# 完整写法,分支不存在会创建分支
git push [远程主机名] [本地分支名]:[远程分支名]
$ git push origin dev:dev

# 省略写法,同样分支不存在会创建分支
git push [远程主机名] [本地分支名]
$ git push dev:dev
git push [远程分支名]
$ git push dev

# 省略[本地分支名]
$ git push :dev
# git会删除远程主机的dev分支,即用一个空分支更新deb分支,相当于
$ git push origin --delete dev

# 如果本地分支与远程分支有追踪关系
$ git push

# 如果当前分支和多个主机之间存在追踪关系,可以用命令设置默认主机
$ git push -u origin master
# 设置后可以
$ git push

# 将本地的所有分支推送到远程主机
$ git push -all [远程主机名]

#当服务器上版本和本地版本不一致时,push会发生冲突,提示使用pull解决冲突,
除非必要,否侧不要使用force
$ git push -force [远程主机]

五、分支

# 列出所有本地分支
$ git branch

# 列出所有远程分支
$ git branch -r

# 列出所有本地分支以及远程分支
$ git branch -a

# 新建一个分支
$ git branch branch_name
# 切换到指定分支,并更新工作区
$ git checkout branch_name
# 新建一个分支, 并且切换到改分支
$ git checkout -b branch_name

# 新建一个分支,指向指定commit
$ git branch branch_name [commit]
# 新建一个分支,与指定的远程分支建立追踪关系
$ git branch --track branch_name remote_branch
# 切换到上一个分支
$ git checkout -

# 建立追踪关系,在现有分支与指定的远程分支之间
$ git branch --set -upstream -to branch_name remote_branch

在这里插入图片描述

# 合并指定分支到当前分支
$ git merge branch_name
# 选择一个commit,合并进当前分支
$ git cherry-pick [commit]
# 删除分支
$ git branch -d branch_name
# 删除远程分支
$ git push origin --delete branch_name
$ git branch -dr remote_branch

六、标签

# 列出所有tag
$ git tag

# 新建一个tag在当前commit
$ git tag tag_name commit_name

# 删除本地tag
$ git tag -d tag_name

# 删除远程tag
$ git push origin :refs/tags/tag_name

# 查看tag信息
$ git show tag_name
# 提交到指定tag
$ git push remote_name tag_name
# 提交所有tag
$ git push remote_name --tags
# 新建一个分支,指向某个tag
$ git checkout -b branch_name tag_name

七、查看信息

# 显示有变更的文件
$ git status
# 显示当前分支的版本历史信息
$ git log
# 显示commit历史,以及每次commit发生的变更文件
$ git log --stat
# 搜索提交历史,根据关键词
$ git log -s keyword
# 显示某个commit之后的所有变动,每个commit占据一行
$ git log tag_name HEAD --pretty=format:%s
# 显示某个commit之后所有变动,其“提交说明”必须符合搜索条件
$ git log tag_name HEAD --grep feature

# 显示某个文件的历史版本,包括文件改名
$ git log --follw file_name
$ git whatchaned file_name
# 显示指定文件相关的每一次diff
$ git log -p file_name
# 显示分支图
$ git log --oneline --graph --all

# 显示所有提交的用户,按提交数排序
$ git shortlog -sn
# 显示指定文件是从什么人什么时间修改过
$ git blame file_name 

# 显示暂缓区和工作区的代码差异
$ git diff

# 显示暂缓区和上一次commit的差异
$ git diff --cached file_name
# 显示工作区与当前分支最新commit之间的差异
$ git diff HEAD
# 显示两次提交之间的差异
$ git diff [first_brnach] [second_branch]
# 显示写了多少行代码
$ git diff --shortstat "@{0 day ago}"
# 显示某次提交的源数据和内容变化
$ git show [commit]

# 显示某次提交发生变化的文件
$ git show --name-only [commit]
# 显示某次提交时,某个文件的内容
$ git show [commit]:[file_name]
# 显示当前分支的最近几次提交
$ git reflog

# 从本地master拉取代码更新当前分支:branch一般为master
$ git rebase branch_name

八、远程同步

# 更新远程仓库
$ git remote update
# 下载远程仓库所有变动
$ git fetch remote_name
# 显示所有远程仓库
$ git remote -v

# 显示某个远程仓库信息
$ git remote show remote_name

# 增加一个新的远程仓库并命名
$ git remote add [remote_name] [url]

# 取回远程仓库的变化,并与本地分支合并
$ git pull [remote_name] [branch_name]

# 上传本地指定分支到远程仓库
$ git push [remote_name] [branch_name]

# 强行推送当前分支到远程仓库,即使冲突
$ git push [remote_name] -force

# 推送所用分支到远程仓库
$ git push [remote_name] --all

九、撤销

# 恢复暂缓区的指定文件到工作区
$ git checkout [file]

# 恢复某个commit的指定文件到暂缓区和工作区
$ git checkout [commit] [file]

# 恢复暂缓区的所有文件到工作区
$ git checkout

# 重置暂缓区的指定文件,与上一次commit保持一致,但是工作区不变
$ git reset [file]

# 重置暂缓区与工作区,与上一次commit保持yiz
$ git reset --hard

# 重置当前分支的指针为指定的commit,同时重置暂缓区, 但是工作区不变
$ git reset [commit]
# 重置当前分支的HEAD为指定commit,同时重置暂缓区和工作区,与指定的commit一致
$ git reset --hard [commit]

# 重置当前HEAD为指定的commit,但是保持暂缓区和工作区不变
$ git reset --keep [commit]

# 新建一个commit,用来撤销指定的commit
# 后者所有变化都将被前者抵消,并且应用到当前分支
$ git revert [commit]

# 暂时将未提交的变化移除,稍后再移入
$ git stash
$ git stash pop

十、查询commit信息

# 查询commit id
$ git log [file_name]

在这里插入图片描述
根据commit id查询包含提交的所有分支,配合上面的一些查询指令也用到了commit id

# 查询本地所有分支
$ git branch --contains [commit id]
# 查询所有远程分支
$ git branch -r --contains [commit id]
# 查询本地和远程所有的分支
$ git branch -a --contains [commit id]

十一、其他

# 生成一个可供发布的压缩包
$ git archive
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值