git的一些概念和技巧

1. 分支代表最后三个commit(即HEAD, HEAD^和HEAD~2),前一个commit,也用HEAD~1

2. 查看一个文件的改动历史git log (--pretty=oneline) -p <filename>,可以显示每次的改动内容, pretty配置了只显示一行

3. 查看一个文件的两个历史版本的区别 git diff <commitid1> <commitid2> <file>

4. 查看一个文件的某一历史版本 git show <commitid>:<path to file>

5. 查看一个文件和上个版本的改动:git show <file>

6. 将文件从git管理中去掉:git rm --cached <file>

7. 回滚到某个版本:先git branch backup,在分支里面改git log,然后 git reset --hard <commitid>

8. 在remote新建分支:git push -u origin <your branch>, origin就是远程repo的意思。在local新的仓库先git fetch;git checkout origin/<your_branch>

9. 提交到remote的分支,方法一:git push origin HEAD:refs/heads/your_branch,方法二:git checkout --track -b your_branch origin/your_branch里面通过--track设定当前的branch是默认的,然后直接git push就可以了,如果要提交某个branch,方法是:git push origin <branch name>,比如git push origin master

10. "git add -A": stage all, "git add .": stage new and modified without deleted, "git add -u": stage modified and deleted, without new

11. 使用per repo的author,git config --local --add user.name XXX, git config --local --add user.email XXX

12. merge之后出现出错,git fetch origin取得orign的最新信息,git reset --hard origin/master重置到最新

13. git diff 只显示文件名 git diff --name-only SHA1 SHA2

14. github 拿到branch方法: 1. git clone 2. git fetch origin <branch name> 3. git checkout <branch name>

15. 分支要merge,假设Abranch上新的提交,B branch希望拿到,在B上执行 git merge A,就可以了

16. 修改author:git commit --amend --author "XXX <XXX@gmail.com>"

原文地址:https://www.cnblogs.com/qiangxia/p/4334969.html