git技巧

Published by rcdfrd on 2023-01-01
git pull origin master
git add .
git commit -m "$(date)"
git push origin master

# 修改 .gitignore 后要删除文件 git rm --cached . && git add .

# git push -f origin master  # 使远程修改丢失,一般是不可取的
# 冲突 参考 https://blog.csdn.net/michaelshare/article/details/79108233
git rebase / git merge  # rebase是存在危险的操作

# 查看所有分支:
git branch -a
# 在本地新建一个分支:
git branch branchName

git checkout --orphan emptybranch # 创建一个空白的分支
echo '# new branch' >> README.md
git add README.md
git commit -m 'new branch'

git checkout -b iss53  # 新创建分支并切换
# 切换到你的新分支:
git checkout branchName
# 将新分支发布在github上:
git push origin branchName
# 在本地删除一个分支:
git branch -d branchName
# 在github远程端删除一个分支:
git push origin :branchName # (分支名前的冒号代表删除)


# git的4个区域Disk, Staging, Local, Remote
git init    # 初始化
git diff    # 查看Disk在Staging上修改了什么 
# 撤销还未缓存的修改
# git status -> Changes not staged for commit(红色)
git checkout <changed_file>     
git restore  <changed_file>    # 等价
# 撤销缓存区中的修改
# git status -> Changes to be commited(绿色)
git reset <changed_file>
git restore --staged <changed_file> # 等价
# 撤销Disk和Staging中的修改
git checkout HEAD <chaned_file>
# 撤销提交的版本   (只撤销 commit)
git reset --soft HEAD~1
# 撤销提交的版本   (撤销 commit 和 add)
git reset HEAD~1
# 完全回滚  (撤销已经提交的版本、已经添加的缓存、本地的修改)
git reset --hard HEAD~1
#和git reset不同,git revert 本质上是增加一个反相的commit 
git revert HEAD
#私有分支上可以使用 reset 不过同步时要使用 git push -f

# 合并多个commit
git rebase -i 想要合并提交的最旧的更旧一个
# 把除了第一行外的pick改成s

# patch 教程
git format-patch <commit id> -n  # 某次提交含之前的n次提交
git format-patch -M master # 当前分支所有超前master的提交
git diff (file) > change.diff
git diff <commit id> <commit id> > change.diff
# .diff文件只是记录文件改变的内容,不带有commit记录信息
# .patch文件带有记录文件改变的内容,也带有commit记录信息
# 应用 patch
git apply --stat patch
git apply --check patch
git apply patch
# 冲突解决
git apply --reject patch