Git常用操作

1. rebase

rebase提交
  • rebase分支的提交放在被rebase的后面

xy的files文件内容:

Hello chain
Hello hebei
Hello shijiazhuang
Hello qiaoxiqu

相关提交指令:

yuzhenteng@yuzhentengdeMac-mini xy % git add .
yuzhenteng@yuzhentengdeMac-mini xy % git commit -m'添加桥西区'
[main 85cabb5] 添加桥西区
 1 file changed, 2 insertions(+), 1 deletion(-)
yuzhenteng@yuzhentengdeMac-mini xy % git push
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 343 bytes | 343.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
To /Users/yuzhenteng/Desktop/demo/remote_repo/xy.remote
   1ad2042..85cabb5  main -> main

next 的files.txt内容:

Hello chain
Hello hebei
Helllo shijiazhuang
Hello nanchangjie
yuzhenteng@yuzhentengdeMac-mini next % git add .
yuzhenteng@yuzhentengdeMac-mini next % git commit -m'添加南长街'
[main d10f24f] 添加南长街
 1 file changed, 1 insertion(+), 1 deletion(-)
yuzhenteng@yuzhentengdeMac-mini next % git pull --rebase
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 323 bytes | 161.00 KiB/s, done.
From /Users/yuzhenteng/Desktop/demo/remote_repo/xy.remote
   1ad2042..85cabb5  main       -> origin/main
Auto-merging files/file.txt
CONFLICT (content): Merge conflict in files/file.txt
error: could not apply ad3ee72... 添加河北
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply ad3ee72... 添加河北

Hello chain
Hello hebei
<<<<<<< HEAD
Hello shijiazhuang
Hello qiaoxiqu
=======
Helllo shijiazhuang

>>>>>>> ad3ee72 (添加河北)

产生冲突解决如下:

yuzhenteng@yuzhentengdeMac-mini next % git add .
yuzhenteng@yuzhentengdeMac-mini next % git rebase --continue
[detached HEAD d279e54] 添加南长街
 1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/main.
yuzhenteng@yuzhentengdeMac-mini next % git log --oneline --graph --stat
* d279e54 (HEAD -> main) 添加南长街
|  files/file.txt | 1 +
|  1 file changed, 1 insertion(+)
* 77cba66 添加河北和南长街
|  files/file.txt | 1 +
|  1 file changed, 1 insertion(+)
* 85cabb5 (origin/main) 添加桥西区
|  files/file.txt | 3 ++-
|  1 file changed, 2 insertions(+), 1 deletion(-)
* 1ad2042 添加河北和石家庄
|  files/file.txt | 2 ++
|  1 file changed, 2 insertions(+)
* dcdeb8b files init
   files/file.txt | 1 +
   1 file changed, 1 insertion(+)

现在发现77cba66 添加河北和南长街 提交记录有误 因此需要修改:

yuzhenteng@yuzhentengdeMac-mini next % git rebase -i 85cabb5
Stopped at 77cba66...  添加河北
You can amend the commit now, with

  git commit --amend 

Once you are satisfied with your changes, run

  git rebase --continue
yuzhenteng@yuzhentengdeMac-mini next % git rebase --continue
Successfully rebased and updated refs/heads/main.
按提示进行:
yuzhenteng@yuzhentengdeMac-mini next % git commit --amend 

进入编辑页面

添加河北和南长街->添加河北

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Fri Aug 29 15:29:30 2025 +0800
#
# interactive rebase in progress; onto 85cabb5
# Last command done (1 command done):
#    edit 77cba66 添加河北和南长街->添加河北
# Next command to do (1 remaining command):
#    pick d279e54 添加南长街
# You are currently editing a commit while rebasing branch 'main' on '85cabb5'.
#
# Changes to be committed:
#       modified:   files/file.txt
#

然后:

yuzhenteng@yuzhentengdeMac-mini next %   git commit --amend 
[detached HEAD ca43296] 添加河北和南长街->添加河北
 Date: Fri Aug 29 15:29:30 2025 +0800
 1 file changed, 1 insertion(+)
yuzhenteng@yuzhentengdeMac-mini next %  git rebase --continue
Successfully rebased and updated refs/heads/main.
yuzhenteng@yuzhentengdeMac-mini next % git log --oneline --graph --stat
* e4163ef (HEAD -> main) 添加南长街
|  files/file.txt | 1 +
|  1 file changed, 1 insertion(+)
* ca43296 添加河北和南长街->添加河北
|  files/file.txt | 1 +
|  1 file changed, 1 insertion(+)
* 85cabb5 (origin/main) 添加桥西区
|  files/file.txt | 3 ++-
|  1 file changed, 2 insertions(+), 1 deletion(-)
* 1ad2042 添加河北和石家庄
|  files/file.txt | 2 ++
|  1 file changed, 2 insertions(+)
* dcdeb8b files init
   files/file.txt | 1 +
   1 file changed, 1 insertion(+)

*注意 -i 后面的提交id应该是77cba66的父id

2.merge

从远程仓库pull代码的时候,是一个快速合并。
xy端给files.txt添加华域城,并提交push代码

Hello chain
Hello hebei
Hello shijiazhuang
Hello qiaoxiqu
Hello nanchangjie
hello huayucheng

yuzhenteng@yuzhentengdeMac-mini xy % git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   files/file.txt

no changes added to commit (use "git add" and/or "git commit -a")
yuzhenteng@yuzhentengdeMac-mini xy % git commit -am'添加华域城、'
[main 821eed4] 添加华域城、
 1 file changed, 1 insertion(+)
yuzhenteng@yuzhentengdeMac-mini xy % git push
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 345 bytes | 345.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
To /Users/yuzhenteng/Desktop/demo/remote_repo/xy.remote
   e4163ef..821eed4  main -> main

next端给files.txt添加华域大厦:

Hello chain
Hello hebei
Hello shijiazhuang
Hello qiaoxiqu
Hello nanchangjie
Hello huayudasha

yuzhenteng@yuzhentengdeMac-mini next % git status -sb
## main...origin/main
 M files/file.txt
yuzhenteng@yuzhentengdeMac-mini next % git commit -am'添加华域大厦'
[main ccb34ad] 添加华域大厦
 1 file changed, 1 insertion(+)
yuzhenteng@yuzhentengdeMac-mini next % git pull
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 325 bytes | 162.00 KiB/s, done.
From /Users/yuzhenteng/Desktop/demo/remote_repo/xy.remote
   e4163ef..821eed4  main       -> origin/main
Auto-merging files/file.txt
CONFLICT (content): Merge conflict in files/file.txt
Automatic merge failed; fix conflicts and then commit the result.

此时产生冲突

Hello chain
Hello hebei
Hello shijiazhuang
Hello qiaoxiqu
Hello nanchangjie
<<<<<<< HEAD
Hello huayudasha
=======
hello huayucheng
>>>>>>> 821eed43490f578d6f55a94a8574eaaf70b12bd0

我们希望用三路合并, 设置方法:

yuzhenteng@yuzhentengdeMac-mini next % git config merge.conflicstyle diff3

回退代码并重新pull

yuzhenteng@yuzhentengdeMac-mini next % git pull
Auto-merging files/file.txt
CONFLICT (content): Merge conflict in files/file.txt
Automatic merge failed; fix conflicts and then commit the result.

产生冲突并解决:

yuzhenteng@yuzhentengdeMac-mini next % git log --oneline --graph --stat
*   75f6bf7 (HEAD -> main, origin/main) 合并冲突
|\  
| * 821eed4 添加华域城、
| |  files/file.txt | 1 +
| |  1 file changed, 1 insertion(+)
* | a626ec6 添加华域大厦
|/  
|    files/file.txt | 2 ++
|    1 file changed, 2 insertions(+)
* e4163ef 添加南长街
|  files/file.txt | 1 +
|  1 file changed, 1 insertion(+)
* ca43296 添加河北和南长街->添加河北
|  files/file.txt | 1 +
|  1 file changed, 1 insertion(+)
* 85cabb5 添加桥西区
|  files/file.txt | 3 ++-
|  1 file changed, 2 insertions(+), 1 deletion(-)
* 1ad2042 添加河北和石家庄
|  files/file.txt | 2 ++
|  1 file changed, 2 insertions(+)
* dcdeb8b files init
   files/file.txt | 1 +
   1 file changed, 1 insertion(+)

3. stash

我们有时会遇到这样的情况,正在dev分支开发新功能,做到一半时有人过来反馈一个bug,让马上解决,但是新功能做到了一半你又不想提交,这时就可以使用git stash命令先把当前进度(工作区和暂存区)保存起来,然后切换到另一个分支去修改bug,修改完提交后,再切回dev分支,使用git stash pop来恢复之前的进度继续开发新功能。下面来看一下git stash命令的常见用法

使用方法

1. git stash / git stash save /git stash push

git stash 存储工作区和缓存区

yuzhenteng@yuzhentengdeMac-mini next % git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   files/file.txt

no changes added to commit (use "git add" and/or "git commit -a")
yuzhenteng@yuzhentengdeMac-mini next % git stash push -m'将华域大厦改为工农路'
Saved working directory and index state On main: 将华域大厦改为工农路
2. git stash list
yuzhenteng@yuzhentengdeMac-mini next % git stash list       
stash@{0}: On main: 添加yzt
stash@{1}: On main: 将华域大厦改为工农路
3. git stash pop [stash_id] 和 git stash apply

1、git stash pop
恢复具体某一次的版本,如果不指定stash_id,则默认恢复最新的存储进度

yuzhenteng@yuzhentengdeMac-mini next % git stash pop stash@{1}
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   files/file.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped stash@{1} (b658b5517410b640b05c1fa11929f271f1680da9)
yuzhenteng@yuzhentengdeMac-mini next % git stash list         
stash@{0}: On main: 添加yzt

2、git stash apply: 将堆栈中的内容应用到当前目录,不同于git stash pop,该命令不会将内容从堆栈中删除,也就说该命令能够将堆栈的内容多次应用到工作目录中,适应于多个分支的情况。

yuzhenteng@yuzhentengdeMac-mini next % git stash apply stash@{1}
Auto-merging files/file.txt
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   files/file.txt

no changes added to commit (use "git add" and/or "git commit -a")

4. hook

可以在pre-commit.sample中写相关shell程序,用来语法检查。然后将sample后缀去掉。添加可执行权限即可。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容