- 约471字
- 技术
- 2015年4月22日
Git 是分布式版本控制系统,由 Linux 之父 Linus Torvalds 发起。和 svn 等版本控制的最大区别在于分布式,每个人在本地都有一份完整的代码历史库,在不联网的情况下就可以查所有历史并提交代码。为了让 git 新手更轻松入门,将工作中经常使用的 git 命令简单总结一下。
最常见命令
# 克隆一个远程项目, <repo> 为远程地址
git clone <repo>
# 查看状态
git status
# 提交 commit
git add -A # 将
git commit -m 'your comment'
# push 代码, 将本地改动同步到服务器
git push -u [remote-name] [branch-name] # 用于首次提交到远程服务器
git push # 最常用 push 命令
git push -f # 强行覆盖服务器上的,主要用于回滚服务器上提交历史
# pull 代码, 抓取远程数据并合并到本地
git pull [remote-name] [branch-name]
git pull
# 分支
git branch # 查看当前分支情况
git checkout -b <branch> 创建新分支,并切换到新分支
# 合并
git merge [branch-name]
git merge --squash [branch-name] # 创建一次提交而不是 merge 每一次提交
# 善用帮助
git help
git <command> -h
常见命令
# 修改远程 origin 地址
git remote -v
git remote set-url origin <repo>
# 查看提交历史
git log
git checkout <hash> # 用指定历史版本创建一个新的分支
git reset <hash> # 回到指定历史版本,不改动代码内容
git reset --hard <hash> # 回到指定历史版本,此时会丢失所有后续更改
更多实用功能
git stash # git pull 之前隐藏改动
git stash pop # 恢复改动
最常用的 Git 站点:
更多帮助:
- GitBook: http://git-scm.com/book/zh/v1