git is used in actual development - solving problems

foreword

  • The git code version management tool breaks the conventional physical transmission, update, merge, and rollback to improve development efficiency and traceability.

  • The information on the Internet will have all the commands are very complete and many, which makes it unfriendly to the students who have just learned about it, and it is difficult to use it in practice.

  • Everyone has their own habit of using git. This article starts with the problems they encountered for your reference.

  • This article mainly records the basic use, reasons and solutions of git in my actual development.

actual use

1. For the first time to develop and build your own branch - create your own branch, generally your own initials

// 我们在本地创建自己分支-创建并切换到这个分支(他会把主分支代码带过来),这个时候你就和当前主分支代码一样
git checkout -b xxx
// 当我们开发完一个功能,或者一个模块就可以提交了
git add .
// 计较到暂存区的备注信息-写清楚方便后期出问题回滚
git commit -m "你本次提交的代码备注信息"
// 提交到远程仓库-第一次提交会报错正常,复制它提供的命令即可
git push

2. Undo the temporary storage area and modified files

// 直接写代码修改了很多文件,git status查看是红色,在工作区。是绿色在暂存区
// git commit -m '注释' 之后可以通过代码来回滚
// 这条命令会标注你修改了哪些文件,和撤销的命令,颜色是绿色(没有git add .前在工作区)
// 这条命令会标注你修改了哪些文件,和撤销的命令,颜色是红色(git add .后在暂存区)
git status
// 这条命令查看你修改过哪些文件哪些代码
git diff


// 工作区撤销更改
// 查看状态
git status
// 复制命令和文件路径 撤销某一个文件修改
git restore 文件路径
// 撤销工作区全部文件更改
git checkout .


// 暂存区撤销更改(git add . 之后-一般不用)
// 查看状态-颜色绿色
git status
// 复制命令和文件路径 撤销某一个文件修改
git restore --staged 文件路径
// 撤销暂存区全部文件
git reset HEAD .
// 再次查看状态就会和上面一样

2.1 View status in the workspace - the color is red - there are undo commands and modified files and paths as shown in the figure below

 2.2 Check the status of the temporary storage area - the color is red - there are undo commands and modified files and paths as shown in the figure below

3. Copy your own branch code to the main branch such as -master

// 切换到主分支master
git checkout master
// 拉取主分支远程仓库代码-因为多人开发,你要保证自己本地主分支要和远程主分支代码一样,才不会出错
git pull
// 把需要合并的分支合并过来
git merge xxx
// 这个时候不出意外一般都会出意外,不要慌。
// 第一种情况,会出现一个弹框,第一行是黄色的字有你分支名字,下面有几行看不懂的文字。
// 这主要是因为本地仓库的文件跟远程端文件有冲突造成的,注释为什么合并。先不要:wq
// 我们应该在英文输入法的前提下。按下键盘i键,发现第一行信息可以删,备注本次合并原因
// 完了之后按下ESC键,输入:wq 按下回车键,最后在提交记录中就会看见这次备注信息。
// 第二种情况 代码冲突,会在git窗口命令最后面(master/忘记了)形式出现,解决冲突即可。
// 来到vscode 工作树,(左边第三个),点击屏幕是分成3份,上面2份,下面一份(是最终结果)
// 我们可以点击上面2份中一个自己想要代码,右上角3个点使用代码,点击下面的接受合并,就解决完一个冲突。
// 解决完之后我会提交记录一次
git add .
git commit -m "解决什么功能合并冲突"
// 将本地主分支提到远程,在远程提交记录中就会发现刚刚的提交记录
git push

4. The remote branch is forced to overwrite the local branch

// 把远程dev分支覆盖到本地dev分支
git reset --hard origin/dev
// 把远程分支覆盖到本地任意一个分支-可以随意组合
// git pull --force <远程主机名> <远程分支名> :<本地分支名>
git pull --force origin dev:dev

5. Delete remote branch or local branch

// 删除本地分支-删除本地dev分支
git branch --delete dev
//简写
git branch -d dev
// 删除远程仓库分支-删除远程dev分支,也会删除追踪分支
git push origin --delete dev

6. Code version rollback - come to the branch that needs to be rolled back (such as qh)

// 仔细查看本地commit提交记录,确定好要回滚版本
// 查看本地详细的提交记录-会有一长串版本号
git log
// 查看本地简略的提交记录-会有版本号
git reflog
// 确定好自己要回滚的版本-选择当前的版本号当前代码回滚之后还在代码就会在当前版本
// 他是有2种方式回滚-如果是代码错了救不了了回滚肯定是使用第二个--hard不要错误代码
// git reset --soft 版本号-把指针回退到指定邦本,会保留之前代码
// git reset --hard 版本号-把指针回退到指定邦本,代码不保留(回滚版本之后写的代码都会删除)
git reset --hard 版本号
// 此时需要把本地回滚分支代码同步到远程仓库(qh远程仓库名)
git push -f origin qh
// 简写 git push -f
// 此时远程的分支提交记录也会覆盖掉,就完成代码版本回滚
​

7. Other commands

// 拉取远程仓库代码-第一次拉取
git clone 远程仓库地址
// 将当前目录下修改的所有代码从工作区添加到暂存区 
git add .
// 将缓存区内容添加到本地仓库
git commit -m '注释'
// 查看工作区代码相对于暂存区的差别
// 红色是在工作区,绿色是在暂存区
git status
// 查看哪些文件修改过
git diff
// 列出本地存在分支,当前分支用*标记
git branch
// 查看远程仓库分支列表
git branch -r
// 查看远程仓库和本地仓库所有分支
git branch -a
// 查看本地分支的最后一次提交
git branch -v
// 查看本地哪些分支已经合并到当前分支
git branch --merged
// 查看所有未合并工作的分支
git branch --no -merged

Notice:

1. Local warehouse (area division during vscode development) first we need to understand

Every time we write code directly in the workspace, git status is red when viewed, and commands can be used to undo changes

git add . Add the modified code in the workspace to the temporary storage area. When the git status is green, you can command to undo the changes

git commit -m 'comment' submits the code in the temporary storage area to the local warehouse, and can only roll back to undo the code

2. The git operation is not allowed to copy and paste the keyboard, but can use the mouse to copy and paste.

3. Pay attention to the Chinese and English input method formats of the keyboard at all times.

4. Every submission should clearly write why it was submitted and why it was merged (preferably a function or page), so as to facilitate rollback.

5. No matter when you report an error or conflict, don't panic. Translate it first, if you don't understand it, just copy it and search on Baidu to see what it is.

6. As long as there is a submission record and code. Don’t worry if the code crashes, you can roll back everything.


Summarize:

After this process, I believe that you have also had a preliminary deep impression on the use of git in actual development-solving problems, but the situation we encounter in actual development must be different, so we must understand its principle, in case Inseparable from the original. Come on, hit the workers!

Please point out any deficiencies, thank you -- Fengguowuhen

Guess you like

Origin blog.csdn.net/weixin_53579656/article/details/129967496