常用命令 | git
一、git 配置
1. 配置账户
git config --global user.name <username> #设置用户名
git config --global user.email <email> #设置邮箱地址
2. 生成 ssh key
ssh-keygen -t rsa -C <email>
3. 在远程版本库上配置完 ssh key 后测试是否成功
ssh -T git@github.com #以 github 为例
二、关联远程仓库
1. 以 github 为例
- 将本地项目上传至远程仓库(以下命令在本地项目的目录下操作)
- 在 github 上创建新的库
git init初始化git add -A添加项目下所有文件git commit -m "<commit_message>"提交到 gitgit remote add origin git@github.com:<github_name>/<repository——name>.git连接远程仓库git push -u origin master推送至远程仓库(第一次推送)
- 关联github已有项目
- 如果是别人的项目先 fork 到自己的仓库(optional)
git clone git@github.com:<github_name>/<repository——name>.git将项目 clone 至本地
三、常用命令
1. 基本命令
git status #查看 git 状态
git pull #从远程仓库获取更新内容
git add #将修改添加到缓存
git commit -m "msg" #提交修改
git push #提交更新
git log #查看提交记录
2. 分支(默认为master)
git branch <branch_name> #创建新的分支
git branch #查看分支情况(前面带 * 表示当前分支)
git checkout <branch_name> #切换分支
git merge <branch_name> #切换回 master 后合并分支
git branch -d <branch_name> #删除无用分支
四、其他
1. 拉取远程分支代码
git clone -b <branch_name> git@github.com:<github_name>/<repository——name>.git
2. 切换至远程分支代码
git fetch origin <branch_name> #检测远程分支
git checkout -b <branch_name> origin/<branch_name> #本地创建并切换分支
git pull origin <branch_name> #拉取远程分支更新内容
3. 在分支基础上创建新分支
git checkout -b <new_branch_name> #基础上创建新分支
git push origin <new_branch_name> #创建远程新分支
4. 使用 git pull --rebase
git pull时带上 --rebase 参数可以避免污染分支
- 无冲突顺序
...coding...
git add
git commit -m "<message>"
git pull --rebase
git push
- 有冲突顺序
...coding...
git add
git commit -m "<message>"
git pull --rebase #rebase 中断
...solving...
git add
git rebase --continue
git push

浙公网安备 33010602011771号