Git命令行指令总结


持续更新中。。。

Git命令行指令总结

在这里插入图片描述

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

在本地没有项目的时候 使用clone
在本地有项目的情况下和远程不同步的情况下 使用pull 拉取更新
在本地已有项目主分支的情况下还想下载项目的其他分支使用fetch

0 秘钥

0.0 配置用户名 邮箱

git config --global user.name "自已的用户"
git config --global user.email "自己的邮箱"

配置本地用户名

git config --local user.name "自已的用户"

在这里插入图片描述
推送后,依然会被github记录为自己推送,并记录contributions

配置本地邮箱名

git config --local user.name "自已的邮箱"

在这里插入图片描述
推送后,依然会被github记录为一次推送,并不记录为自己的contributions

0.1 生成秘钥

ssh-keygen -t rsa -C "自己的邮箱与上面邮箱一致"

-t 指定密钥类型,默认是 rsa ,可以省略。
-C 设置注释文字,比如邮箱。
-f 指定密钥文件存储文件名。

1.生成公钥了:ssh-keygen
2.接着会确认存放公钥的地址,默认就是上面说的路径,直接enter键确认
3.接着会要求输入密码和确认密码,如果不想设置密码直接不输入内容 按enter键

0.2 查看本地秘钥

切换到文件夹,用户(一般都是Administrator)下的.ssh文件夹

cd ~/.ssh

查看公钥

vim id_rsa.pub
OR
cat id_rsa.pub

也可以直接输入命令 :

cat ~/.ssh/id_rsa.pub

在这里插入图片描述
复制以上秘钥
在这里插入图片描述
将公钥复制并添加至远程仓库(这里是GitHub)

1 仓库管理

新建仓库

git init

在本地新建一个Git仓库

在这里插入图片描述
在所在文件夹内会出现一个如下 .git文件
在这里插入图片描述
在文件中添加一个文件
在这里插入图片描述

git status

git status命令用于显示工作目录暂存区 的状态。使用此命令能看到那些修改被暂存到了, 哪些没有, 哪些文件没有被Git tracked到。git status不显示已经commit到项目历史中去的信息。看项目历史的信息要使用git log.
在这里插入图片描述

git fetch

Download objects and refs from another repository
从远程分支拉取代码

git fetch
git fetch origin
git fetch origin master

git fetch origin master 表示从远程库origin中获取最新的版本,只会将remotes文件夹(存放着本地库所关联的远程库)的commit id更新至最新,heads文件夹不会改动,即不会修改本地库的HEAD。

git fetch + git merge = git pull

git pull

git add

使用git add命令将 新建文件 添加到 暂存区 之后
添加文件暂存区
在这里插入图片描述
添加文件夹暂存区
在这里插入图片描述

git rm 移除文件

usage: git rm [<options>] [--] <file>...

-n, --dry-run         dry run
-q, --quiet           do not list removed files
--cached              only remove from the index
-f, --force           override the up-to-date check
-r                    allow recursive removal
--ignore-unmatch      exit with a zero status even if nothing matched


git rm -r --cached <file>... 
git rm --cache <file>

暂存区中删除文件。但是本地文件还存在, 只是不希望这个文件被版本控制

删除前
删除前
在这里插入图片描述删除前 在这里插入图片描述
删除后,暂存区中已经删除,本地文件内容还存在
git rm <file>
git rm -r <path>

同时删除当前工作目录和暂存区的文件或者目录,即本地的文件也被删除了

git rm -f <file>

在这里插入图片描述

符号解释
-r级联删除目录
–cached只删除暂存区的文件或者目录
file要删除的文件或目录,以空格隔开填写多个,文件名支持通配符。

git reset

链接: Git Reset 三种模式.

git reset HEAD -- <file name>

撤销的是暂存区中文件的修改
在这里插入图片描述

git commit

git-commit - Record changes to the repository

git commit 主要是将暂存区里的改动给提交到本地的版本库。每次使用git commit 命令我们都会在本地版本库生成一个40位的哈希值,这个哈希值也叫commit-id,
commit-id在版本回退的时候是非常有用的,它相当于一个快照,可以在未来的任何时候通过与git reset的组合命令回到这里.

git commit -m 'message'

使用双引号可以在message中添加单引号

git commit -m "message ' code"

直接提交已经跟踪过的文件的修改

git commit -am 'message'

git commit -a  -m 'message'

OPTIONS

-a
–all
Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.

-m
–message=
Use the given as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs.

The -m option is mutually exclusive with -c, -C, and -F.

git push

git-push - Update remote refs along with associated objects

git push的一般形式为 git push <远程主机名> <本地分支名> <远程分支名> ,
例如 git push origin master:refs/for/master ,即是将本地的master分支推送到远程主机origin上的对应master分支,
origin 是远程主机名,第一个master是本地分支名,第二个master是远程分支名。

git push origin [local Branch]:[remote Branch]

本地分支名在前,远程分支名在后
当远程分支不存在时,该操作会在远程新建一个分支

git push origin [local Branch]:[remote Branch]
git push origin [local Branch]

如果远程分支被省略,如上则表示将本地分支推送到与之存在追踪关系的远程分支(通常两者同名),如果该远程分支不存在,则会被新建

git push origin [local Branch]

在这里插入图片描述
git push origin [空]:[branchName]

git push origin --delete [branch name] 删除远端服务器分支

删除远端服务器分支

git push origin --delete [branch name]

在这里插入图片描述

在 git push origin : 命令中,冒号 “:” 前面的参数是要推送的本地分支名,这里没有提供,相当于推送一个本地空分支。冒号 “:” 后面的参数是要推送到服务器的远端分支名。

git push origin	[空]:[branchName]

如果省略本地分支名,则表示删除指定的远程分支,因为这等同于推送一个空的本地分支到远程分支,等同于 git push origin --delete master

git push --set-upstream origin [local branch name]把本地的新分支,和远程的新分支关联

首先需要将当前工作分支调整到新建分支(即将需要关联的本地分支)
如果远程分支名不存在,则会在远程新建该分支

git push --set-upstream origin [local branch name]

在这里插入图片描述

git restore --staged …

git 提示我们可以使用git rm --cached …来删除暂存区内不想要的文件

2 分支Branch

2.1git branch

git branch 查看当前使用分支

查看当前使用分支(结果列表中前面标*号的表示当前使用分支)

git branch
git branch -a 查看所有分支

查看所有分支

git branch -a
git branch -r 查看远程分支

查看远程分支

git branch -r
git branch [newBranch] 新建分支

新建分支,但未跳转到新建分支

git branch [newBranch]

git branch -d [分支名] 删除本地分支

删除本地分支

git branch -d [分支名]

假设删除Branch01,则须将当前所在分支切换到非Branch01分支上,否则不能删除该分支
在这里插入图片描述

error: Cannot delete branch 'Branch01' checked out at 'D:/Documents/04GtiRepository/desktop-tutorial'

有可能会提示

error: The branch 'Branch02' is not fully merged.
If you are sure you want to delete it, run 'git branch -D Branch02'.
分支“ Branch02”未完全合并。

此时使用

git branch -D Branch02

能成功删除


git checkout -b [newBranch] 新建分支并跳转

新建分支,并跳转到新建分支

git checkout -b [newBranch]
git checkout [branch name] 切换分支

切换分支

git checkout [分支名]
git checkout -b [Branch0x] origin/[Branch0y]拉取远程分支,并创建本地分支

拉取远程分支,并创建本地分支

 git checkout -b [Branch0x] origin/[Branch0y]

新创建的分支名Branch0x 与 远程分支名Branch0y 可以不一样,但是不推荐。

2.2重命名分支名

-m
–move
Move/rename a branch and the corresponding reflog.
-M
Shortcut for --move --force.

  1. 修改本地分支名
    在这里插入图片描述
    在这里插入图片描述
    若想将分支改为大写字母,则会提示已经存在,此时可以使用-M强制删除

  2. 删除远程分支名
    在这里插入图片描述

  3. 将本地修改后的分支推送到远程
    在这里插入图片描述

git mv 文件重命名

git mv [source] [destin]

在这里插入图片描述
重命名后,修改的文件默认被commit,可以直接push到远程仓库

Git 小技巧

中断正在运行的 git 指令
Ctrl + C
jik即可

gitee

简易的命令行入门教程:
Git 全局设置:

git config --global user.name “Jorgen7062”
git config --global user.email “jorgen7062@foxmail.com”
创建 git 仓库:

mkdir SelfBuilt001
cd SelfBuilt001
git init
touch README.md
git add README.md
git commit -m “first commit”
git remote add origin https://gitee.com/jorgen7062/SelfBuilt001.git
git push -u origin master
已有仓库?

cd existing_git_repo
git remote add origin https://gitee.com/jorgen7062/SelfBuilt001.git
git push -u origin master

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值