Git Tips

git patch 怎么合进去

带提交信息

*某次提交(含)之前的几次提交:

 git format-patch 【commit sha1 id】-n

n指从sha1 id对应的commit开始算起n个提交。

eg:

git format-patch  2a2fb4539925bfa4a141fe492d9828d030f7c8a8 -2

某个提交的patch:

git format-patch 【commit sha1 id】 -1

eg:

git format-patch  2a2fb4539925bfa4a141fe492d9828d030f7c8a8 -1

某两次提交之间的所有patch:

git format-patch 【commit sha1 id】..【commit sha1 id】 

eg:

git format-patch  2a2fb4539925bfa4a141fe492d9828d030f7c8a8..89aebfcc73bdac8054be1a242598610d8ed5f3c8

不带提交信息的方式(不推荐)

git diff > patch_file.patch
git apply patch_file.patch 

本地创建空分支后push

git checkout --orphan 新分支
rm -rf * #删除已有文件
#自行添加文件
git add .
git commit -a
git push -u origin 新分支

备注: origin是默认的远端名称,可以查看.git/config核对

重新提交commit

本地修复后,想再次提交,而不是发起一个全新的提交,可以在commit时追加参数--amend

git commit --amend
git push -u origin branch_name

重写git提交历史记录中的username和email

git commit --amend --author="Author Name <[email protected]>" --no-edit

也可以这么操作:


#Reset your email to the config globally:

git config --global user.email [email protected]

#Now reset the author of your commit without edit required:

git commit --amend --reset-author --no-edit
# bash
git filter-branch -f --commit-filter '
        if [ "$GIT_AUTHOR_NAME" != "<old-username>" ] || [ "$GIT_AUTHOR_EMAIL" != "<[email protected]>" ];
        then
                GIT_AUTHOR_NAME="<new-username>";
                GIT_AUTHOR_EMAIL="<[email protected]>";
                git commit-tree "$@";
        else
                git commit-tree "$@";
        fi' HEAD

也可以使用rebase来修改整个项目的历史:

git config --global user.name "New Author Name"
git config --global user.email "<[email protected]>"
#单个历史
git commit --amend --no-edit --reset-author
#整个项目历史
git rebase -r --root --exec "git commit --amend --no-edit --reset-author"

删除上游git仓库中的lfs

clone本地gerrit仓库

git clone "http://[email protected]/a/xxx/graphic_standard" && (cd "graphic_standard" && mkdir -p .git/hooks && curl -Lo `git rev-parse --git-dir`/hooks/commit-msg http://[email protected]/tools/hooks/commit-msg; chmod +x `git rev-parse --git-dir`/hooks/commit-msg)

cd graphic_standard

添加上游仓库

git remote add upstream https://gitee.com/openharmony/graphic_standard.git

抓取上游代码

git fetch upstream

抓取上游lfs

git lfs fetch upstream --all

checkout上游分支

git checkout upstream/OpenHarmony-3.1-Release

卸载lfs

git lfs uninstall
rm .gitattributes
touch .gitattributes
git lfs ls-files | sed -r 's/^.{13}//' > files.txt
while read line; do git rm --cached "$line"; done < files.txt
while read line; do git add "$line"; done < files.txt
git add .gitattributes
rm files.txt
git commit -m "unlfs"
git lfs ls-files
rm -rf .git/lfs

切为本地新分支

git checkout -b OpenHarmony-3.1-Release

推送到gerrit

git push -u origin OpenHarmony-3.1-Release

文档信息