Summary of git common operations

Archive for my convenience~

Besides add、commit、push、pull, some operations also need to be mastered~ Prepare the required items before reviewing:

mkdir git-demo1
cd git-demo1
git init

basic operation

# 在工作区新增一个README.md文件,并写入 # Hello World

# 查看哪些原件做了修改(或者未提交到本地仓库)
git status

# 将全部文件加入暂存区(也可以指定某个文件)
git add .

# 将暂存区域的文件提交到本地仓库
git commit -m "feat: add README.md"

# 添加远程仓库
git remote add origin https://xxx/git-demo.git

# 将本地仓库的修改推送到远程仓库(这里的前提是添加了远程仓库~)
git push -u origin "master"

branch management

# 从当前分支新建分支并切换到该分支(master -> feature/test1)
git branch feature/test1

# 查看当前所在的分支(也会列出所有分支),现在在master分支
git branch

# 再次新建一个分支并【切换】到该分支(mater -> feature/test2)
git checkout -b feature/test2

# 查看当前所在的分支,现在在feature/test2分支
git branch

# 查看本地分支和远程分支
git branch -a

# 切换分支回到master
git checkout master

# 删除feature/test1分支(若当前在feature/test1分支,则无法删除所在分支)
git branch -D feature/test1


Advanced operation

Pull the code git pull -p

# 现阶段有两个分支 master和feature/test2
git branch

# 切换到feature/test2分支并推送到远程仓库
git checkout feature/test2
git push --set-upstream origin feature/test2

git chekcout master
git checkout -b feature/test1
git push --set-upstream origin feature/test1

git checkout master
# 这时候我们的远程仓库有三个分支,先在网页上删除feature/test1
# 拉取远程仓库代码,当前的分支,并不会删除feature/test1
git pull
# 查看本地分支和远程分支
git branch -a
# 结果如下
# feature/test1
# feature/test2
# * master
# remotes/origin/feature/test1
# remotes/origin/feature/test2
# remotes/origin/master

# 如果我们在git pull -p的话,会发现同步了远程仓库删除操作
git pull -p
git branch -a
# 结果如下
# feature/test1
# feature/test2
# * master
# remotes/origin/feature/test2
# remotes/origin/master

insert image description here

insert image description here

Staging files git stash

# 切换到 feature/test1分支
git checkout feature/test1
# 新增一个index.html文件,内容随意发挥
git add .
git commit -m "feat: add index.html"
git push

# 切换到 feature/test2分支
git checkout feature/test2
# 新增一个index2.html文件,内容随意发挥
git add .
git commit -m "feat: add index2.html"
git push


# 假设你现在还在feature/test2开发某个功能,需要临时切分支到feature/test1修复一些东西
# 如果这时候 git checkout feature/test1 是会报错的
# 因为你在feature/test2上的内容没有提交
# 这时候你可以选择提交代码,然后切换
# 如果你不想提交代码,又想要切分支的话,可以试试 git stash
git stash save "未完成的功能"
# 也可以直接
git stash 
# 这时候你就可以去test1开发啦,然后回到test2的时候执行

# 查看暂存的记录
git stash list
# stash@{0}: On test2: 未完成的功能

# 使用 git stash apply --index $num,它不会删除暂存记录
# 使用的是最新一次的暂存记录
git stash apply 
# 或者根据序号使用某次暂存,比如这次它是0
git stash apply --index 0
# 这时候的记录里就还有我们暂存的那一条

# 查看暂存的记录
git stash list

# 或者使用git stash pop 使用最近一次暂存的修改并删除
git stash pop
# 查看暂存的记录
git stash list
# 这时候的记录里就没有我们暂存的那一条了

Undo changes

# 如果我们修改了某个文件,且还没添加到暂存区,那么撤销的操作为:
git checkout -- index2.html
# 如果是多文件:
git checkout -- index2.html README.md

Undo the code after git add

# 在feature/test2我们修改了index2.html
git add .
# 如果想要撤销的话执行
git reset index2.html

Undo the code after git commit

undo all

# 先查看log
git log

# 这时候我有三条提交记录
# commit 0f46b36b4a6bc0082aa705f51897616d3d661daa (HEAD -> feature/test2)
# commit d598f45b535daf020f9eec9a152f494965f0eedc (origin/feature/test2)
# commit 500cd99aae50ff91b955b14099dbc9f6f2176e75

# 这时候我想回退到d598f45b535daf020f9eec9a152f494965f0eedc 那么可以使用
git reset --hard  d598f45b535daf020f9eec9a152f494965f0eedc

# 这样再次执行git log就只有两条记录了,且之前修改的代码已经不见了
# 如果你想更新远程代码 需要执行 git push -f

revoke only own

# 上述撤销操作 适用于一个人开发的时候,多人开发的时候是不行的
# 假设当前的记录是 你提交了(1),A提交了一次(2),你提交了(3),A提交了一次(4)
# 这时候如果你想删除3,如果执行git reset的话,那么4的记录也会被删除

# 先假装我是2个人开发
# 我在index1.html开发,A在index2.html开发
# 新增内容<p>我第一次开发</p>
git add .
git commit -m "feat: 我第一次开发"

# 新增内容<p> A第一次开发</p>
git add .
git commit -m "feat: A第一次开发"

# 新增内容<p>我第二次开发</p>
git add .
git commit -m "feat: 我第二次开发"

# 新增内容<p>A第二次开发</p>
git add .
git commit -m "feat: A第二次开发"

# 查看日志
git log
# 50c11d768673a0309fad28a2383b188ec19c13a7 feat: A第二次开发
# d3baef3ebeb198b9bbed89c071bc72fc71228010 feat: 我第二次开发
# 375a73f6900bc40967089e5e83f4d3a73d6e70e8 feat: A第一次开发
# f1ec3a4f803a6c6604245e33e8ec2bf2dc9b30cc feat: 我第一次开发

# 这时候我们想要删除自己的第二次开发,那么可以借助 git revert
git revert d3baef3ebeb198b9bbed89c071bc72fc71228010
# 也可以是 git revert d3baef3ebeb198b9bbed89c071bc72fc71228010 -m ”feat: revert xxx“
# 这时候你会发现 index1.html中的代码只有<p>我第一次开发</p>
# git log中会出现revert信息


# 实际情况中 git revert还是会有冲突的情况,请妥善处理~~

Merge commit information

In the actual development process, sometimes the same function will be repaired Nand submitted, Neach committime , and they can be merged "fix: 修复某些bug"with the help of :git rebase

# <p>修复第一次</p>
git add .
git commit -m "fix: 修复index.html"

# <p>修复第二次</p>
git add .
git commit -m "fix: 修复index.html"

# <p>修复第三次</p>
git add .
git commit -m "fix: 修复index.html"

# git log
# 05237ec91aa44ab78be1cc162c2739a6f1030181 fix: 修复index.html
# b1cd9f47598996e75730e63819fc09c933e45ec5 fix: 修复index.html
# bc8f437103fe7d126bd899025cca65bb44f8b496 fix: 修复index.html
# bcc524fd3e1655725ab12b29571386b85118f221 Revert "feat: 我第二次开发"
# commit f8a8f0ab9e1af8079bca372e8ed5e767a40b8a03  feat: A第二次开发

# 现在要将 Revert "feat: 我第二次开发" 之前的三次 fix: 修复index.html 合并
# 所以取 Revert "feat: 我第二次开发" 的commitid为基准
git rebase -i bcc524fd3e1655725ab12b29571386b85118f221

This section will appear next:

pick bc8f437 fix: 修复index.html
pick b1cd9f4 fix: 修复index.html
pick 05237ec fix: 修复index.html

# Rebase bcc524f..05237ec onto bcc524f (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.

Press on the keyboard insertto enter the edit mode, and then delete the information, the final result is:

pick bc8f437 fix: 修复index.html

# Rebase bcc524f..05237ec onto bcc524f (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#

After editing, press ESCthe key to exit the editing mode, press :wq+ enterto save and exit, and view the record:

bc8f437103fe7d126bd899025cca65bb44f8b496 fix: 修复index.html
bcc524fd3e1655725ab12b29571386b85118f221 Revert "feat: 我第二次开发"

commitAlready merged~

Sourcetree

If you don't like to use the command line, use Sourcetree~

How to use reference: git visualization tool Sourcetree uses a full strategy (including various git conflict resolution)

Supongo que te gusta

Origin blog.csdn.net/qq_34086980/article/details/131457115
Recomendado
Clasificación