资料:
https://blog.youkuaiyun.com/ZTZY520/article/details/54023395
版本回退:
列出commit id:
-
用
git log
可以查看提交历史,以便确定要回退到哪个版本。 -
用
git reflog
查看命令历史,以便确定要回到未来的哪个版本。
$ git log --pretty=oneline
根据commit id 进行reset
$ git reset --hard 1094a
HEAD is now at 83b0afe append GPL
在Git中,用HEAD
表示当前版本,也就是最新的提交1094adb...
(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^
,上上一个版本就是HEAD^^
,当然往上100个版本写100个^
比较容易数不过来,所以写成HEAD~100
。
$ git reset --hard HEAD^
提交代码
git add
命令实际上就是把要提交的所有修改放到暂存区(Stage),然后,执行git commit
就可以一次性把暂存区的所有修改提交到分支。
$ git add readme.txt
$ git commit -m "git tracks changes"
用git diff HEAD -- readme.txt
命令可以查看工作区和版本库里面最新版本的区别:
$ git diff HEAD -- readme.txt
撤销修改
撤销工作区修改
$ git checkout -- readme.tx
撤销暂存区修改
$ git reset HEAD readme.txt
从版本库删除文件
$ git rm test.txt
$ git commit -m "remove test.txt"
远程仓库
创建SSH Key,在用户主目录里找到.ssh
目录,里面有id_rsa
和id_rsa.pub
两个文件,这两个就是SSH Key的秘钥对,id_rsa
是私钥,不能泄露出去,id_rsa.pub
是公钥
$ ssh-keygen -t rsa -C "youremail@example.com"
登陆GitHub,打开“Account settings”,“SSH Keys”页面:
然后,点“Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub
文件的内容
要关联一个远程库,使用命令
$ git remote add origin git@server-name:path/repo-name.git;
关联后,使用命令,第一次推送master分支的所有内容;
$ git push -u origin master
此后,每次本地提交后,只要有必要,就可以使用命令,推送最新修改;
$ git push origin master
统计代码量
git log --author="tornmy" --pretty=tformat: --numstat | grep -v thirdparty |awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
Commands list