git版本管理常见操作命令
-
安装git
不用啰嗦,无论win(双击下一步)还是Linux(用yum -y install git)都比较简单
-
git配置信息
git config --global user.name "yiran"---设置用户名 git config --global user.email "wb-lj462873@cainiao.com" --设置邮箱 git config --global --list 查看配置信息
-
生成公钥私钥
ssh-keygen -o
win默认生成路径在C:\Users\你的用户名\.ssh
linux默认在/home/yiran/.ssh,其中/home/yiram是用户自己新建的目录
-
配置公钥
1.需要将步骤三中生成的公钥id_rsa.pub文件内容拷贝,添加到gitlab,github,gitee,自己公司的代码管理平台中的ssh密钥中配置
-
拉取代码库中代码
git clone git@118.118.116.171:chengzhi/web-test.git
-
创建分支命令
git branch (branchname)
-
查看本地分支
git branch
-
查看远程分支
git branch -r
-
切换分支
git checkout branchname git switch branchname
-
创建分支并切换
git checkout -b <name> git switch -c <name>
-
删除分支
git branch -D branchname git branch -d branchname
-
添加到暂存区
// 将所有变动的文件保存到暂存区 git add . // 添加一个或多个文件到暂存区 git add [file1] [file2] ... // 添加指定目录到暂存区,包括子目录 git add [dir]
-
提交代码
git commit -m 'message'
如果commit时候发现提交注释错误,修改注释
git commit --amend // 这时候会进入vim编辑器,修改完成你要的注释后保存即可
-
查看仓库当前的状态,显示有变更的文件
git status
-
查看历史提交记录
git log
-
从远程获取代码库
git fetch
-
下载远程代码并合并
// 拉取master最新代码合并到brantest git pull origin master:brantest 等同于 git fetch+git merge
-
合并分支命令
// 将branchname分支合并到本地分支 git merge branchname
-
上传远程代码并合并
git push // 因为远程没有dev分支 git push --set-upstream origin branchname
-
代码撤销
-- 数字代表提交次数 git reset --mixed HEAD~1 git reset --soft HEAD~1 git reset --hard HEAD~1 或者 git reset --mixed (提交版本号-aacbce2) git reset --soft (提交版本号-aacbce2) git reset --hard (提交版本号-aacbce2)
–mixed (git reset的默认参数,即不添加参数的默认值)
不删除工作空间改动代码,撤销commit 和 撤销git add . 操作,回退到工作区
–soft
不删除工作空间的改动代码 ,撤销commit,不撤销git add file–hard
删除工作空间的改动代码,撤销commit且撤销add