1 概述
常用场景:
2 一些典型场景
初始化
git config --global user.name "codelabs"
git config --global user.email my@codelabs.com
git init
git remote add origin https://github.com/username/repository.git
git pull origin master
提交
git add ./git add <file1> <file2> ...
git commit -m "Your commit message here"
git push
撤回
参考最后的链接。
合并提交
有三种办法,rebase reset merge,最后一个不熟。。。
git rebase -i HEAD~n
这里要注意,不能全改成s,第一个还是保留pick。
第二种就是
git reset --soft HEAD~n
git commit -m "Your new commit message"
这种简单,但是不能选择,只能全部提。而且会覆盖之前的提交历史。
更新代码
git pull origin master
git pull --rebase origin master
或者使用git fetch也可以,完了再merge一下。
不确定远端仓库名和分支名的话,可以用git remote -v和git branch -r查看。
用repo要简单一些。
repo sync .
这里再说说rebase。git pull = git fetch + git merge git pull --rebase = git fetch + git base
git rebase会少一个merge提交,整体看着干净一点。应用的区别就在这。。。
git merge的提交log
git rebase的提交log
使用Patch
-
找到要生成 patch 的提交范围:确定要生成 patch 的提交范围,可以是单个提交、一系列连续的提交,或者两个提交之间的差异。
-
使用 git format-patch 生成 patch:在命令行中执行以下命令:
git format-patch <start-commit-hash>..<end-commit-hash>
其中
<commit>
是要生成 patch 的提交范围的最后一个提交。git format-patch -n
可以生成最近n个提交的patch。
-
传输 patch 文件:将生成的 patch 文件传输给其他人或应用程序。您可以使用电子邮件、文件共享服务等方式发送文件。
-
应用 patch:其他人或应用程序可以使用
git apply
命令将 patch 应用到其本地代码库中。例如:git apply my_patch.patch
git am patch.file
git apply只是把patch拉到本地。am则是拉到本地之后,再增加一个提交。
强行恢复成和库上一致
先git fetch
git reset --hard xxx
git clean -df/-xf
合并分支
git checkout main
git merge feature
git add <conflicted_file>
git commit
git commit -m "Merge feature branch"
Git rebase和Git merge区别,然后rebase会丢失提交,所以一般用merge。
Git rebase也可以压缩提交,git rebase -i HEAD~3,将pick改为squash
只合并一个或者几个提交的话,可以用cherrypick
git cherrypick <commit-hash-1> <commit-hash-2> ...
3 最后总结几个我遇到的经典场景
1 最最典型场景
本地正在开发一个功能,突然来了一个紧急任务,怎么处理?
首先整理本地内容
可以git stash
也可以git commit。但是这样会导致后面提交多。
然后新建任务分支,所有新任务都在任务分支上处理。
完成后切换回主分支。
开发完成后,可以提交上去,如果还没完又要新开发,也可以再次git stash。
2 Git权限问题处理。
在一个服务器上,有多个人都在使用。git账号冲突的问题。
3 Git在本地修改几次后,和远端冲突。
基于Git开发时,一定要注意的一个地方就是分支。一定要注意当前的分支策略。比如一个新特性一定要新拉分支。很多从SVN升级上来的程序员就没有这个习惯,最后肯定是搞得务必尴尬。
其次就是要提交之前,一定要git pull --rebase一下。否则后面很麻烦。
实在都不行,就是最后的招数。
git fetch origin
git reset --hard origin/分支名
4 本地打包
git arch --format zip head > arch.zip
5 Repo出来后没有git关联
git本地分支如何与远端分支关联_git 本地分支关联远程分支-CSDN博客
6 Git换成本地服务器
大公司的时候会遇到这种问题。
[url "ssh://账户@xxx.xxx.xxx.xxx:xxx"]
insteadOf = ssh://账户@xxx.xxxxxxxxxx.com:xxx
[url "ssh://账户@xxx.xxx.xxx.xxx:xxx"]
pushinsteadof = ssh://账户@xxx.xxxxxxxxxx.com:xxx
pushinsteadof = ssh://账户@xxx.xxx.xxx.xxx:xxxxx
这样可以在实际拉取的时候换成其它服务器。
最后很推荐的是这个网站,玩游戏方式学习git
参考: