ToB企服应用市场:ToB评测及商务社交产业平台

标题: [每周一更]-(第70期):常用的GIT操作命令 [打印本页]

作者: 熊熊出没    时间: 2023-12-13 08:40
标题: [每周一更]-(第70期):常用的GIT操作命令

1、增删文件
  1. # 添加当前目录的所有文件到暂存区
  2. $ git add .
  3. # 添加指定文件到暂存区
  4. $ git add <file1> <file2> ...
  5. # 添加指定目录到暂存区,包括其子目录
  6. $ git add <dir>
  7. # 删除工作区文件,并且将这次删除放入暂存区
  8. $ git rm [file1] [file2] ...
  9. # 停止追踪指定文件,但该文件会保留在工作区
  10. $ git rm --cached [file]
  11. # 改名文件,并且将这个改名放入暂存区
  12. $ git mv [file-original] [file-renamed]
复制代码
把文件名 file1 添加到 .gitignore 文件里,Git 会停止跟踪 file1 的状态。
2、分支
  1. # 列出所有本地分支
  2. $ git branch
  3. # 列出所有本地分支和远程分支
  4. $ git branch -a
  5. # 新建一个分支,但依然停留在当前分支
  6. $ git branch [branch-name]
  7. # 新建一个分支,并切换到该分支
  8. $ git checkout -b [new_branch] [remote-branch]
  9. # 切换到指定分支,并更新工作区
  10. $ git checkout [branch-name]
  11. # 合并指定分支到当前分支
  12. $ git merge [branch]
  13. # 选择一个 commit,合并进当前分支
  14. $ git cherry-pick [commit]
  15. # 删除本地分支,-D 参数强制删除分支
  16. $ git branch -d [branch-name]  命令:git branch -d 分支名
  17. # 删除远程分支
  18. $ git push [remote] :[remote-branch] 命令:git push origin --delete branch名
  19. # 创建远程分支
  20. (1)git checkout -b test 在当前分支下创建test分支,并进去到test分支
  21. (2)git push origin test 将test分支推送到远程
  22. (3)git branch --set-upstream-to=origin/test  test  //将本地分支 test 关联到远程分支 test中
  23. (4)git branch -a  // 查看远程分支
  24. # 同步本地和远程分支
  25. (1)远程开好分支,本地 拉取,分支名test
  26. git checkout -b test origin/test    //检出远程的feature-branch分支到本地
  27. (2)建立分支管理关系
  28. git branch --set-upstream-to=origin/humx humx
  29. or
  30. git branch -u origin/humx  humx 、 git branch -u origin/humx
  31. 结果:Branch 'humx' set up to track remote branch 'humx' from 'origin'.
复制代码
3、提交
  1. # 提交暂存区到仓库区
  2. $ git commit -m [message]
  3. # 提交工作区与暂存区的变化直接到仓库区
  4. $ git commit -a
  5. # 提交时显示所有 diff 信息
  6. $ git commit -v
  7. # 提交暂存区修改到仓库区,合并到上次修改,并修改上次的提交信息
  8. $ git commit --amend -m [message]
  9. # 上传本地指定分支到远程仓库
  10. $ git push [remote] [remote-branch]
  11. ### 4、拉取
  12. # 下载远程仓库的所有变动 (Git only)
  13. $ git fetch [remote]
  14. # 显示所有远程仓库 (Git only)
  15. $ git remote -v
  16. # 显示某个远程仓库的信息 (Git only)
  17. $ git remote show [remote]
  18. # 增加一个新的远程仓库,并命名 (Git only)
  19. $ git remote add [remote-name] [url]
  20. # 取回远程仓库的变化,并与本地分支合并,(Git only), 若使用 Git-SVN,请查看第三节
  21. $ git pull [remote] [branch]
  22. # 取回远程仓库的变化,并与本地分支变基合并,(Git only), 若使用 Git-SVN,请查看第三节
  23. $ git pull --rebase [remote] [branch]
  24. # git clone https://xxx/xxx.git --depth 1
  25. 该命令只是拉取最近一直的提交记录,如果想拉取全部历史:  git pull --unshallow
  26. # git仓库地址进行更新操作
  27. git remote set-url origin git仓库地址
  28. # git拉取某一个tag的分支仓库
  29. git clone --branch v14.1.16 https://gitee.com/baijunyao/laravel-bjyblog.git
复制代码
5、撤销
  1. # 恢复暂存区的指定文件到工作区
  2. $ git checkout [file]
  3. # 恢复暂存区当前目录的所有文件到工作区
  4. $ git checkout .
  5. # 恢复工作区到指定 commit
  6. $ git checkout [commit]
  7. # 重置暂存区的指定文件,与上一次 commit 保持一致,但工作区不变
  8. $ git reset [file]
  9. # 重置暂存区与工作区,与上一次 commit 保持一致
  10. $ git reset --hard
  11. # 重置当前分支的指针为指定 commit,同时重置暂存区,但工作区不变
  12. $ git reset [commit]
  13. # 重置当前分支的HEAD为指定 commit,同时重置暂存区和工作区,与指定 commit 一致
  14. $ git reset --hard [commit]
  15. # 新建一个 commit,用于撤销指定 commit
  16. $ git revert [commit]
  17. # 将未提交的变化放在储藏区
  18. $ git stash
  19. # 将储藏区的内容恢复到当前工作区
  20. $ git stash pop
  21. # 文件会回到暂存之前的状态
  22. $ git reset HEAD
  23. # 回退上一步制定位置
  24. $ git reset --hard commitid
复制代码
6、查询
  1. # 查看工作区文件修改状态
  2. $ git status
  3. # 查看工作区文件修改具体内容
  4. $ git diff [file]
  5. # 查看暂存区文件修改内容
  6. $ git diff --cached [file]
  7. # 查看版本库修改记录
  8. $ git log
  9. # 查看某人提交记录
  10. $ git log --author=someone
  11. # 查看某个文件的历史具体修改内容
  12. $ git log -p [file]
  13. # 查看某次提交具体修改内容
  14. $ git show [commit]
  15. # 查询远程仓库路径
  16. git remote -v
  17. # 本地分支和远程分支的关联关系
  18. git branch -vv
  19. # 查看具体的某一个commit信息
  20. git show commitId
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4