git的初始用

git配置全局的用户名和邮箱

1
2
git config --global user.name xxkh
git config --global user.email xxx@gmail.com

查看用户名和邮箱

1
2
git config user.name
git config user.email

记住密码

在服务器上 clone 代码第一次通常会提示输入密码,为了下次不再提示,可以在 clone 后做如下操作

1
git config credential.helper store

初始化项目

1
2
3
4
5
6
7
8
cd project_root                             # 进入项目目录
git init # 初始化git仓库
git add . # 添加文件到仓库
git commit -m 'init commit' # 提交代码到本地仓库
git remote add origin ${repository_path} # 将项目关联到git server
git pull origin master # 同步代码
git push origin master # push代码到远程仓库
git clone ${repository_path} # 新的位置clone项目

重新提交

提交后如果发现遗漏可以使用 git commit –amend 重新提交

1
2
3
git commit -m 'initial commit'
git add forgotten_file
git commit --amend

撤销提交文件

1
2
3
4
5
6
7
8
git checkout -- <file>              # 取消对文件的修改。还原到最近的版本,废弃本地做的修改。
git reset HEAD <file>... # 取消已经暂存的文件。即,撤销先前"git add"的操作
git reset HEAD^ # 回退所有内容到上一个版本
git reset HEAD^ a.py # 回退a.py这个文件的版本到上一个版本
git reset –soft HEAD~3 # 向前回退到第3个版本
git reset –hard origin/master # 将本地的状态回退到和远程的一样
git reset 057d # 回退到某个版本
git revert HEAD # 回退到上一次提交的状态,按照某一次的commit完全反向的进行一次commit.(代码回滚到上个版本,并提交git)