Git 常用命令

config相关

-- 查看配置列表
git config --list
-- 添加配置,注意,如果配置已存在,会覆盖修改
git config --global user.name "YourName"
git config --global user.email "YourEmail"
-- 修改配置,比如修改邮箱
git config --global --replace-all user.email "NewEmail"
-- 删除配置,比如删除错误邮箱
git config --global --unset user.Email

账号密码相关

git config --global user.name "your GitHub name"   -- 设置用户名
git config --global user.emial "your GitHub email"   -- 设置邮箱
git config user.name                     -- 查看用户名
git config user.email                    -- 查看邮箱

获取SSH key

ssh-keygen -t rsa -C "your_email@your_email.com"

生成成功后,打开生成目录下的.ssh/id_rsa.pub文件,将其里面的内容粘贴到你的GitHub账户(右上角)
Settings -> SSH and GPG keys -> New SSH key, 其Title任意命名,内容粘贴到Key中,粘贴。

链接验证,输入命令:
ssh -T git@github.com
输出结果: You’ve successfully authenticated, but GitHub does not provide shell access 
表示链接成功

克隆

cd 进入到你设定的目录,输入命令:
git clone https://github.com/###/***.git

查看状态

git status

添加

cd 进入到指定目录
git add .               -- 后面的点表示,添加所有文件到暂存区 
git add new.txt         -- 添加当前目录下文件
git add Dir/New/new.txt   -- 指定目录添加指定文件

重命名

-- oldName为原有名字, newName为新名字
git mv oldName newName

--注意查看命令
git status

删除

-- 使用如下命令,或者直接删除
git rm test.text   

-- 提交时,注意使用命令:
git status         -- 查看状态
git add.           -- 将修改的文件添加到暂存区

撤销本地修改

-- 撤销对所有已修改但未提交的文件,不包括新增的文件
git checkout . 

-- 撤销对指定文件file.txt的修改
git checkout file.txt 

或者:
-- 恢复到上一版本,慎用,即使已提交到缓存区也会被干掉
git reset

提交

git commit -m "写入你提交的日志"

本地仓库同步到远程仓库

git push origin master

 更新远程仓库到本地

//方法一:合并方式进行同步
$ git fetch origin master       // 从远程的origin仓库的master分支下载代码到本地的origin master
$ git log -p master.. origin/master // 比较本地的仓库和远程参考的区别
$ git merge origin/master       // 把远程下载下来的代码合并到本地仓库,远程的和本地的合并

//方法二: 强制方式同步
git pull --rebase origin master

大概流程图:

 分支相关

-- 查看本地已经存在所有分支名(带 * 号的表示当前分支)
git branch

--  查看远程分支列表
git branch -r

-- 查看本地和远程分支列表
git branch -a

-- 查看本地分支信息
git branch -v

-- 查看本地分支更多信息
git branch -vv

-- 查看所有分支信息
git branch -av

-- 新建分支
git branch branch_name 
-- 检出指定名字的分支
git checkout branch_name

-- 创建新分支并检出的话,也可以使用如下命令:
git checkout -b branch_name 

-- 将新建的分支推送到远程中,一般远程分支也本地分支同名,也可以任意命名
git push origin branch_name:origin_branch_name

-- 将本地分支代码提交到远程分支
git add .
git commit -m "提交日志"
git push origin origin_branch_name

-- 重命名分支
git branch -m oldBranchName newBranchName

-- 强制重命名分支
git branch -M oldBranchName newBranchName

mac查看分支相关

-- 在Mac终端中,如果看不到需要的分支相关,可使用命令:
git fetch

-- 如果想在mac终端显示Git当前所在分支,可查看如下网址的方法:
https://segmentfault.com/a/1190000018992493

本地分支合并相关

-- 检出主干分支
git checkout master
-- 查看当前分支状态
git branch 

-- 本地合并(使用merge输入哪个分支名,表示将该分支合并到当前分支中)
git merge branch_name

-- 将本地代码推送到远程主干
git push origin master

本地分支删除相关

-- 删除本地分支(远程分支并未删除)
git branch -d branch_name

-- 删除远程分支
git push origin --delete origin_branch_name

-- 清空分支缓存相关
git fetch --prune origin
原文地址:https://www.cnblogs.com/SkyflyBird/p/10726722.html