git基本操作

clone远端项目

1
2
git clone https://github.com/jquery/jquery.git
git clone username@host:/path/to/repository

远程主机

1
2
3
4
# 克隆版本库的时候,所使用的远程主机自动被Git命名为origin
git remote -v
# 将你的仓库连接到某个远程服务器
git remote add origin <server>

提交,拉取

1
2
3
4
5
# 注意,分支写法是<来源地>:<目的地>
# git fetch <远程主机名> <分支名>,使用"远程主机名/分支名"读取。比如origin主机的master:origin/master
git fetch origin master
git pull <远程主机名> <远程分支名>:<本地分支名>
git push <远程主机名> <本地分支名>:<远程分支名>

分支

1
2
3
4
5
6
7
8
9
10
11
12
# 合并分支
git merge <branch>
# 查看
git branch -a
# 新建
git checkout -b branch_1
# 切换
git checkout master
# 删除
git branch -d branch_2
# 差异
git diff branch_1 branch_2

恢复

1
2
3
4
5
# 替换掉本地改动
git checkout -- <filename>
# 丢弃本地的所有改动与提交,获取服务器最新的版本
git fetch origin
git reset --hard origin/master

历史记录

1
git log
文章目录
  1. 1. clone远端项目
  2. 2. 远程主机
  3. 3. 提交,拉取
  4. 4. 分支
  5. 5. 恢复
  6. 6. 历史记录