关于git:常用的GIT命令

5次阅读

共计 2361 个字符,预计需要花费 6 分钟才能阅读完成。

废话少说,间接上代码:

# 生成 ssh keys
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

# 全局配置
git config --global user.name "<username>"
git config --global user.email "<username>@xxx.com"

# 查看日志
git log
git log --pretty=oneline                # 单行日志
git log --graph --abbrev-commit         # 图形日志 + 简洁 id

# 查看 HEAD 日志
git reflog                              # 查问每一次的命令日志(通常和回滚一起应用,查找 commitId)# 撤销批改
git restore --staged <fileName>         # 还原 staged 区文件
git restore <fileName>                  # 还原工作区文件(还原到上一个 staged 或者版本库)# 查看批改
git diff <fileName>                     # 查看工作区和(staged|版本库)外面最新版本的区别
git diff HEAD -- <fileName>             # 查看(工作区 |staged 区)和版本库外面最新版本的区别

# 删除文件
git rm <fileName>                       # 从版本库中删除该文件

# 回滚
git reset --hard                        # 以后节点(勾销所有更改)git reset --hard HEAD^                  # 上一个节点
git reset --hard HEAD^^                 # 上两个节点
git reset --hard HEAD~100               # 上 100 个节点(100 可批改)git reset --hard <commitId>             # 指针 HEAD 挪动到指定节点
git reset --soft xxx                    # 软回滚,不回滚代码,只回滚提交操作

# 穿梭
git checkout <commitId>                 # HEAD 指针指向某个节点,以复原过后的文件(常常用来从此节点创立新分支)git checkout <commitId> <fileName>      # 把以后状态的某个文件内容笼罩为某个版本,会影响你以后的工作区(不要轻易做作这个操作)# 撤销
git revert <commitId>                   # 撤销一次提交(git 会从新做一个新的提交来撤销批改,而不是删除某次提交)git revert -m 1 <commitId>              # 撤销 merge 合并,- m 前面带的参数值能够是 1 或者 2,示意要保留哪个分支的批改。(master 上执行 git merge dev,1:master,2:dev)# 咱们要记住,因为咱们摈弃过之前 dev 合并过去的 commit,下次 dev 再往 master 合并,之前摈弃过的其实是不蕴含在外面的。咱们把之前 master 那个带有【反操作】的 commit 给撤销掉就能够从新 merge 了。# 提交
git commit --amend                      # 批改上一次提交正文

# 创立分支
git branch <branch>                     # 创立分支
git checkout <branch>                   # 切换分支
git switch <branch>                     # 切换分支
git checkout -b <branch>                # 创立并切换分支
git switch -c <branch>                  # 创立并切换分支
git branch -d <branch>                  # 删除分支
git branch -D <branch>                  # 强制删除分支,当分支从未被合并过,会提醒应用 - D 来强制删除,避免提交失落
git checkout -b <branch> origin/<branch>               # 创立并对应近程分支
git branch --set-upstream-to <branch> origin/<branch>  # 对应近程分支
 
# 创立标签
git tag                                 # 查看所有标签
git tag <tagName>                       # 打标签
git tag <tagName> <commitId>            # 给特定提交打标签
git tag -a <tagName> -m "<description>" # 附加标签阐明
git show <tagName>                      # 查看标签信息
git push origin --tags                  # 推送本地所有标签
git push origin <tagName>               # 推送单个标签
git tag -d <tagName>                    # 删除本地标签
git push origin :refs/tags/<tagName>    # 删除近程标签

# 合并
git merge <branch>                                  # 合并 <branch> 到以后分支,无抵触的话就是 fast-forward 模式
git merge <branch> --no-off -m "< 阐明 >" <branch>     # 禁止应用 fast-forward 模式,这样 merge 时生成一个新的 commit,会保留分支信息
git merge --abort                                   # 放弃正在 merge 的操作

# 工作现场
git stash                               # 保留现场
git stash push -m "< 阐明 >"              # 保留现场并增加阐明
git stash list                          # 列出所有现场
git stash pop                           # 复原最新的现场,并从 stash list 中删除
git stash apply <stash>                 # 复原指定的现场,然而不从 stash list 中删除
git stash drop <stash>                  # 删除指定的现场

# 复制提交
git cherry-pick <commitId>              # 将其余分支的提交“复制”(git 将主动做一次提交,所以 commitId 是不同的)到以后分支

# 查看 remote 库信息
git remote -v                           # 查看 remote 库
git remote add origin <remote>          # 增加 remote 库

# 推送 remote 分支
git push -u origin master               # 第一次推送 master 分支的所有内容
git push origin <branch>                # 推送分支

# 配置 remote 地址
git remote add origin http://xxx.com/xxx.git
git remote set-url origin http://xxx.com/xxx.git
正文完
 0