Git
入门
配置用户名和邮箱地址
git config user.name "xxx"
git config user.email "[email protected]"
设置文件名大小写敏感
git config core.ignorecase false
修改远程仓库中已提交的作者信息
- 创建一个你的repo的全新裸clone,命令如下:
git clone --bare https://github.com/<user>/<repo>.git
- 复制下面的脚本,并修改
OLD_EMAIL
、CORRECT_NAME
、CORRECT_EMAIL
变量
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="<[email protected]>"
CORRECT_NAME="<correct_name>"
CORRECT_EMAIL="<[email protected]>"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
- 使用
git log
查看Git历史检查是否有错误。 - 把正确历史push到GitHub仓库,命令为:
git push --force --tags origin 'refs/heads/*'
。
克隆仓库
git clone [email protected]:torvalds/linux.git <folder-name>
git clone [email protected]:torvalds/linux.git --depth=1
git clone --single-branch --branch=<branch> --depth=1
克隆私有仓库
git clone htts://<username>@github.com/<username>/<repo_name>
设置多组SSH Key
vi ~/.ssh/config
vi ~/.ssh/id_new
chmod 600 ~/.ssh/id_new
设置SSH推送
fatal: The remote end hung up unexpectedly
git config http.postBuffer 524288000
cp ~/.ssh/id_rsa ~/.ssh/id_rsa.old
ssh-keygen -t rsa -C "[email protected]"
cat ~/.ssh/id_rsa.pub
git remote -v
git remote rm origin
git remote add origin [email protected]:xxx/xxxx.git
git push origin
git push -u origin master
移除大文件重新推送
# stage our giant file for removal, but leave it on disk.
git rm --cached <giant_file>
# Amend the previous commit with you change. Simply making a new commit won't work, as you need to remove the file from the unpushed history as well.
git commit --amend -CHEAD
# push our rewritten, smaller commit.
git push
Git 删除仓库内已缓存的*.log文件
git rm --cached logs/xx.log
git commit -m "We really don't want Git to track this anymore!"
查看已被忽略的文件
git status --ignored
Git删除错误提交的commit回退版本
git reset --hard <commit_id>
# 回退到上一次的提交,如果是上n次就将1改成对应的数字
git reset --hard HEAD~1
git push origin HEAD --force
Git推送
git remote add origin https://github.com/xxx/yyyy.git
git push -u origin master
git cherry-pick <commitid1> <commitid2>
git rebase -i HEAD~4 --aboveAll
git rebase -i HEAD~2
git commit --amend
git rebase -i HEAD~2
git branch -f master HEAD
git rebase caption master
合并多次commit
比如下图的commit历史,想要把"Second change"和"Third change"这两个commit合并到一起,
+---------------------------------------------+
commit 5f60c4fee5a0fd718d52c04d62824dd6be471aa8
Author: test
Date: Tue Oct 28 10:31:28 2014 +0800
Fourth change.
commit fe30349283caea32f7c8c2d4f357a288c629b23e
Author: test
Date: Tue Oct 28 10:30:58 2014 +0800
Third change.
+---------------------------------------------+
http://zhidao.baidu.com/question/1303239896978196539.html
http://segmentfault.com/q/1010000000430041
分支
本地分支
基于develop
分支检出bugfix
分支
git checkout -b bugfix develop
相当于
git branch bugfix
git checkout bugfix
远程分支
删除远程分支
使用git push origin :<branch-name>
,注意冒号前面的空格不能少,原理是把一个本地空分支push到远程server上,相当于删除该远程分支。
使用git branch -r -d origin/<branch-name>
,只能删除本地分支,不能删除远程分支。
在Git v1.7.0之后,可以使用这种语法删除远程分支:git push --delete origin <branch-name>
。
示例:删除远程分支feature-ci
# 切换到本地仓库的develop分支
git checkout develop
# 删除本地仓库中feature-ci分支
git branch -d feature-ci
# 删除远程仓库中feature-ci分支
git push origin :feature-ci
其他
修正“Permissions 0664 for '~/.ssh/id_rsa' are too open.”
chmod 0600 ~/.ssh/id_rsa
git push 出错
出错信息:
Counting objects: 4801, done.
error: pack-objects died of signal 9)
error: failed to push some refs to 'git@gitserver:repo.git'
git config pack.windowMemory 32m
暂存修改
git stash
git stash pop
git rev-list master --max-count=10
git revert HEAD
git revert HEAD~1