git笔记
第一次使用配置
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
生成ssh key
ssh-keygen -t rsa -C "you@example.com"
home 中生成 .ssh 目录,里面有两个文件:
- id_rsa(私钥)注意保管
- id_rsa.pub(公钥)添加到github
初始化仓库
从远程仓库添加
git clone http://... # http可能需要输入账号密码
git clone git@... # ssh需要提前添加公钥到远程仓库
本地建立仓库并添加到远程
git init
git remote add origin http://...
git add .
git commit -m "init"
git push -u origin master # -u 设置默认主机
切换远程仓库
修改命令
git remote set-url origin url
先删后加
git remote rm origin
git remote add origin git@github.com:sheng/demo.git
修改config文件
如果你的项目有加入版本控制,那可以到项目根目录下,查看隐藏文件夹, 发现.git文件夹,找到其中的config文件,就可以修改其中的git remote origin地址了。
分支管理
git branch -a # 查看所有分支
git branch -r # 查看远程分支
git fetch # :相当于是从远程获取最新版本到本地,不会自动merge
git fetch origin master:tmp
git diff tmp
git merge tmp
git pull # :相当于是从远程获取最新版本并merge到本地
git pull origin remote_branch # (远程分支)
git checkout master # 切换分支
文件管理
git add .
git commit -m "comment"
git push origin master
git status # 查看工作空间状态
更新子模块
$ git submodule update --init --recursive