当前位置:   article > 正文

算法之工程化内容(2)—— Git常用命令

算法之工程化内容(2)—— Git常用命令

目录

1. git初始化配置

2. 新建仓库

3. 工作区——>暂存区——>本地仓库

4. git reset回退版本

5. 查看差异 git diff

6. 删除文件git rm

7. .gitignore

8. vscode操作git

9. git分支、合并和删除

10. 解决合并冲突

11. 回退和rebase

12. 添加远程仓库

参考链接:

01.课程简介_哔哩哔哩_bilibili


1. git初始化配置

  1. 查看版本号:git -v
  2. 配置用户:git config --global user.name 'huanhaunwang'
  3. 配置邮箱:git config --global user.email 'whh@nit.zju.edu.cn'
  4. 保存配置:git config --global credential.helper store
  5. 查看配置:git config --global list

2. 新建仓库

  1. git clone http://github.xxx.com
  2. mkdir learn-git
  3. cd learn-git
  4. git init / git init my-repo # 初始化my-repo仓库
  5. cd .git
  6. ls -alltr

3. 工作区——>暂存区——>本地仓库

  1. 工作区——>暂存区:git add .
  2. 暂存区——>本地仓库:git commit -m 'xxx' # 'xxx'为commit的描述

  1. ###### 添加和提交文件
  2. 初始化本地仓库:git init
  3. 查看状态:git status
  4. 工作区-->暂存区:git add .
  5. --> 提交txt文件到暂存区:git add *.txt
  6. --> 删除暂存区内容:git rm --cached <file>
  7. 暂存区-->本地仓库:git commit -m 'description'
  8. 写内容到file1.txt:echo 'This is the first file' > file1.txt
  9. 提交记录:git log/ git log --oneline

4. git reset回退版本

git reset三种模式

工作区

暂存区

git reset --soft

git reset --hard

git reset --mixed

  • 查看暂存区内容:git ls-files
  • 当出现误操作时:查看历史所有操作 git reflog --> 回退指定版本号 git reset --hard xxx(版本号)

5. 查看差异 git diff

(工作区/暂存区/本地仓库,不同版本v1/v2/v3,不同分支)

  1. git diff v1_num v2_num
  2. git diff HEAD~ HEAD
  3. git diff HEAD~2 HEAD
  4. git diff HEAD~3 HEAD
  5. git diff HEAD~3 HEAD <file>
  6. git diff HEAD^ HEAD
  7. git diff <branch_name> <branch_name>

6. 删除文件git rm

  1. 工作区&暂存区同时删除:git rm <file> --> git commit -m 'delete <file>'
  2. 工作区删除-->暂存区删除-->本地仓库删除:rm <file> --> git add <file> / git add. --> git commit -m 'delete <file>'
  3. 暂存区缓存删除:git rm --cached <file>

7. .gitignore

  1. 忽略所有log日志:echo '*.log' > .gitignore --> git add . --> git commit -m 'commit .gitignore'
  2. 忽略temp下的所有内容:echo 'temp/' > .gitignore --> git add . --> git commit -m 'commit .gitignore'
  3. 忽略所有的.a文件:*.a
  4. 忽略任何目录名为temp/的文件夹

8. vscode操作git

9. git分支、合并和删除

  1. 查看分支:git branch
  2. 创建分支:git branch/checkout <branch-name>
  3. 切换分支:git switch <branch-name>
  4. 合并分支:切换到main/master主分支 --> git merge <branch-name>
  5. 删除分支:
  6. (1)已合并:git branch -d <branch-name>
  7. (2)未合并:git branch -D <branch-name>
  8. 查看分支创建&合并流程:git log --graph --oneline --decorate --all
  • git log --graph --oneline --decorate --all

10. 解决合并冲突

  1. 解决合并:vi <main_conflict.txt> --> git add . --> git commit -m 'solve conflict'
  2. 终止合并:git merge --abort

11. 回退和rebase

12. 添加远程仓库

  1. 克隆远程仓库:git clone ssh链接(此命令需要提前配好ssh -key.pub)
  2. 给远程仓库命别名:git remote add <远程仓库别名> ssh链接
  3. e.g. git remote add origin git@github.com:xxx/xxx/xxx.git
  4. 切换分支:git branch main/master
  5. 推送本地 --> remote远程:git push -u <远程仓库别名> <分支名>
  6. 查看远程仓库:git remote -v
  7. 拉取remote远程 --> 本地:git pull / git pull -u <远程仓库别名> <远程分支名>:<本地分支名>
  8. (1)远程-->本地:
  9. 1)手动修改远程
  10. 2)拉取到本地:git pull = git fetch + git merge
  11. (2)本地-->远程:
  12. 1)手动修改本地文件
  13. 2)添加至缓存区:git add.
  14. 3)提交到本地仓库:git commit -m 'description'
  15. 4)推送/更新远程仓库:git push
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/828015
推荐阅读
  

闽ICP备14008679号