哈喽~|

LH寒酥

园龄:1年7个月粉丝:1关注:1

2023-08-06 10:04阅读: 9评论: 0推荐: 0

git常用命令


工作区→暂存区→版本库

设置用户名、邮箱

  1. git config --global user.name xxx : 设置全局用户名, 信息记录在~/.gitconfig 文件中
  2. git config --global user.email xxx@xxx.com : 设置全局邮箱地址, 信息记录在~/.gitconfig 文件中

配置成git仓库

  1. git init : 将当前目录配置成git仓库, 信息记录在隐藏的.git文件中

常用命令

  1. git add xxx : 将xx文件添加到暂存区
    • git add . : 将所有待加入暂存区的文件加入暂存区
  2. git commit -m "备注信息" : 将暂存区的内容提交到当前分支
  3. git status : 查看仓库状态
  4. git diff xxx : 查看xx文件相对于暂存区修改了哪些内容
    • git diff HEAD : 查看与最新提交的差别

删除命令

  1. git rm --cached xx : 将文件从仓库索引目录中删掉, 不管理了
  2. git restore --staged xx : 将xx从暂存区里移除, 管理
  3. git restore xxgit checkout - xx : 将xx文件的修改撤销(未加入暂存区)

查看命令

  1. git log : 查看当前分支的所有版本
    • git log --pretty=oneline : 用一行来显示
  2. git reflog : 查看HEAD指针的移动历史(包括被回滚的版本)
  3. git log --graph : 以图标形式查看分支
  4. git branch -a : 同时查看本地和远程仓库分支信息

代码回滚

  1. git reset --hard HEAD^git reset --hard HEAD~ : 将代码回滚到上一个版本
    • git reset --hard HEAD^^ : 往上回滚两次, 以此类推
    • git reset --hard HEAD~100 : 回滚100个版本
  2. git reset --hard 版本号 : 回滚到某一特定版本

远程仓库

  1. git remote add origin git@git.acwing.com:xxx/XXX.git : 将本地仓库关联远程仓库
  2. git push -u(第一次需要-u以后不需要) : 将当前分支推送到远程仓库
    • git push origin branch_name : 将本地的某个分支推送到远程仓库
  3. git clone git@git.acwing.com:/xxx/XXX.git : 将远程仓库XXX下载到当前目录下
  4. git push --set-upstream origin branch_name : 设置本地的 branch_name 分支对应远程仓库的 branch_name 分支(上传分支)
  5. git push -d origin branch_name : 删除远程仓库的 branch_name 分支
  6. git branch --set-upstream-to=origin/branch_name1 branch_name2 : 将远程的 branch_name1 分支与本地的 branch_name2 分支对应
  7. git pull : 将远程仓库的当前分支与本地仓库的当前分支合并
  8. git pull origin branch_name : 将远程仓库的 branch_name 分支与本地仓库的当前分支合并
  9. git checkout -t origin/branch_name : 将远程的 branch_name 分支拉取到本地

分支命令

  1. git branch branch_name : 创建新分支
  2. git branch : 查看所有分支和当前分支
  3. git checkout -b branch_name : 创建并切换到 branch_name 这个分支
  4. git checkout branch_name : 切换到 branch_name 这个分支
  5. git merge branch_name : 将分支 branch_name 合并到当前分支上
  6. git branch -d branch_name : 删除本地仓库的 branch_name 分支

stash暂存

  1. git stash : 将工作区和暂存区中未提交的修改存入栈中
  2. git stash list : 查看栈中所有元素
  3. git stash pop : 将栈顶存储的修改恢复到当前分支, 同时删除栈顶元素
  4. git stash drop: 删除栈顶存储的修改
  5. git stash apply: 将栈顶存储的修改恢复到当前分支, 但不删除栈顶元素

本文作者:LH寒酥

本文链接:https://www.cnblogs.com/rimliuhan/p/17609123.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   LH寒酥  阅读(9)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起