GIT使用
-
1.安装Git
- 官网直接下,默认安装https://git-scm.com/downloads
-
2.配置用户和邮箱
- git config – global user.name “xxx”
- git config – global user.email “xx@xx.com”
-
3.创建目录
- mkdir xxx
- cd xxx
- pwd -> 查看创建目录的路径
-
4.把目录变成Git仓库
- git init
-
5.把文件添加到暂存区
- git add xxx
-
6.把文件提交到仓库
- git commit -m “xxx”
- -m 后的参数是本次提交的说明
- git commit -m “xxx”
-
7.git其他命令
- git status -> 查看仓库当前状态
- git diff -> 查看状态的详细内容
- git log -> 查看提交历史
- –graph 可以看到分支合并图
- –pretty=oneline 可以将每条记录变成一行
- –abbrev-commit
- git reset --hard HEAD^ – 回退到某个版本
- HEAD ^ -> 上一个版本
- HEAD ^^ -> 上两个版本
- HEAD ~100 -> 上100个版本
- cd37e5f8d9f -> 指定版本号
- git reflog -> 查看命令历史
- git checkout – xxx -> 撤销工作区、暂存区的修改
- git rm xxx -> 删除文件
连接远程仓库
-
1.创建SSH Key
- ssh-keygen -t rsa -C “xx@xx.com”
- 成功后会在上级目录下创建id_rsa、id_rsa.pub两个文件
-
2.将id_rsa.pub复制到GitHub上
- 登录GitHub ->> 点settings ->> SSH and GPG keys ->> New SSH key
- tilte任意填,把id_rsa.pub复制到key里
-
3.在GitHub上创建新仓库
- 点New repository -> 在Repository name里填入名称 -> Create repository
- 其他默认
-
4.关联GitHub仓库
- git remote add origin git@github.com:名称/仓库名.git
-
5.把本地库的内容推送到远程库上
- git push -u origin xxx
- -u -> 仅在第一次推送时使用
- xxx -> 分支名
- git push -u origin xxx
-
6.克隆远程库
- git clone git@github.com:名称/仓库名.git
-
7.查看远程库的信息
- git remote
- -v -> 显示更详细的信息
- git remote
-
8.创建远程分支到本地
- git checkout -b xxx origin/xxx
-
9.指定本地分支与远程分支连接
- git branch --set-upstream-to=origin/xxx xxx
-
10.从远程获取分支的最新提交
- git pull
-
11.删除远程库
- git remote rm xxx
分支
-
1.创建分支
- git branch xxx
-
2.切换分支
- git switch xxx / git checkout xxx
-
3.创建并切换分支
- git switch -c xxx / git checkout -b xxx
- -c / -b -> 创建并切换分支
- git switch -c xxx / git checkout -b xxx
-
4.查看分支
-
git branch
-
“*” -> 表示当前分支
-
-
5.合并分支
- git merge [–no-ff] xxx -> 将指定分支合并到当前分支
- –no-ff -> 表示禁用Fast forward
- git merge [–no-ff] xxx -> 将指定分支合并到当前分支
-
6.删除分支
- git branch -d xxx
- -D ->强行删除
- git branch -d xxx
-
7.保存当前分支进度
- git stash
-
8.查看保存的分支进度
- git stash list
-
9.恢复分支工作进度
- git stash pop
-
10.指定恢复分支工作进度
- git stash apply stash@{0}
-
11.复制特定的提交到当前分支
- git cherry-pick 提交码
-
12.把本地未push的分叉提交历史整理成直线
- git rebase
标签
-
打标签
- git tag [-a] xxx [-m] “xxx”
- -a -> 指定标签名
- -m -> 指定说明文字
- git tag [-a] xxx [-m] “xxx”
-
给历史提交打标签
- git tag xx 提条id
-
查看所有标签
- git tag
-
查看标签信息
- git show xxx
-
删除标签
- git tag -d xxx
-
推送标签到远程
- git push origin xxx -> 推送某个标签
- git push origin --tags --> 推送所有未推送的标签
-
删除远程标签
- git push origin :refs/tags/xxx