git常用命令

git cmd

安装

yum install git
yum install git-doc git-svn git-email git-gui gitk

安装过程可能会提示错误 Requires: libcurl.so.3 ,解决方法:

vim /etc/yum.repos.d/rpmforge.repo
...
enabled = 0 # 把原值 1 改成 0
...

配置

# 配置账号信息 
git config --global user.name your_name
git config --global user.email your_email
# 配置自动换行
git config --global core.autocrlf input # 提交到git是自动将换行符转换为 lf

# 配置密钥
ssh-keygen -t rsa -C your_email         # 生成密钥
ssh -T [email protected]                   # 测试是否成功

# 配置命令别名
git config --global alias.st status     # git st
git config --global alias.co checkout   # git co
git config --global alias.br branch     # git br
git config --global alias.ci commit     # git ci

# 查看配置
git config --list

# 获取帮助信息
git help config

新建仓库

git init    # 初始化

从现有仓库克隆

git clone url
git clone url dir # 克隆到自定义文件夹

查看仓库状态

git status

本地命令

git add *                                   # 跟踪新文件
git add -u [path]                           # 添加[指定路径下]已跟踪文件
git ls-files                                # 查看已跟踪文件列表

rm *&git rm *                               # 取消跟踪并移除本地文件
git rm -f *                                 # 取消跟踪并移除本地文件
git rm --cached *                           # 取消跟踪
git mv file_from file_to                    # 重命名跟踪文件

git commit                                  # 提交更新
git commit -m 'message'                     # 附加说明的提交更新
git commit -a                               # 跳过使用暂存区,把所有已经跟踪过的文件暂存起来一并提交
git commit --amend                          # 修改最后一次提交

git reset -- *                              # 取消已经暂存的文件
git reset HEAD *                            # 取消已经暂存的文件
git reset --mixed HEAD *                    # 取消已经暂存的文件
git reset --soft HEAD~n                     # 重置到指定版本,不会修改暂存区和工作区
git reset --hard HEAD~n                     # 重置到指定版本,会修改暂存区和工作区

git revert HEAD                             # 撤销前一次操作
git revert HEAD~                            # 撤销前前一次操作
git revert commit                           # 撤销指定操作

git checkout -- *                           # 取消工作区文件的修改(暂存区文件覆盖工作区文件)
git checkout -- .                           # 暂存区覆盖工作区
git checkout branch|tag|commit -- *         # 切换到 branch 分支

git diff *                                  # 查看指定文件的差异
git diff --stat                             # 查看简单的 diff 结果
git diff                                    # 比较工作区和暂存区之间的差异
git diff --cached                           # 比较暂存区和当前分支 HEAD 之间的差异
git diff HEAD                               # 比较工作区和当前分支 HEAD 之间的差异
git diff branch_name                        # 比较工作区和分支 branch_name 之间的差异
git diff branch1 branch2                    # 比较两个分支之间的差异
git diff commit1 commit2                    # 比较两次提交之间的差异

git log                                     # 查看最近的提交日志
git log --pretty=oneline                    # 单行显示提交日志
git log --graph                             # 使用 ASCII 码拼接成图形显示分支合并                            
git log -p [file]                           # 查看每次详细修改内容的 diff
git log -num                                # 显示第几条log(倒数)

git stash                                   # 将工作区现场(已跟踪文件)储藏起来,等以后恢复后继续工作
git stash list                              # 查看保存的工作现场
git stash apply                             # 恢复工作现场
git stash drop                              # 删除 stash 内容
git stash pop                               # 恢复的同时直接删除 stash 内容
git stash apply stash@{n}                   # 恢复指定的工作现场,当你保存了不只一份工作现场时

分支

git branch                                  # 列出本地分支
git branch -r                               # 列出远端分支
git branch -a                               # 列出所有分支
git branch -v                               # 查看各分支最后一个提交对象的信息
git branch --merge                          # 查看已合并到当前分支的分支
git branch --no-merge                       # 查看未合并到当前分支的分支
git branch branch_name                      # 新建 branch_name 分支
git branch -m old new                       # 重命名分支
git branch -d branch_name                   # 删除 branch_name 分支
git branch -D branch_name                   # 强制删除 branch_name 分支
git branch --set-upstream branch_name origin/branch_name    # 将本地 branch_name 分支与远程 branch_name 分支之间建立链接

git checkout branch_name                    # 切换到 branch_name 分支
git checkout -b branch_name                 # 新建并切换到 branch_name 分支
git checkout -b branch2 branch1             # 基于 branch1 新建 branch2 分支,并切换

git merge branch_name                       # 将 branch_name 分支合并到当前分支
git merge --squash branch_name              # 合并并且压缩,将 branch_name 上的 commit 压缩为一条

git cherry-pick commit_sha1                 # 拣选合并,将 commit 合并到当前分支
git cherry-pick -n commit_sha1              # 拣选多个 commit 合并,合并完后可以继续拣选下一个 commit

git rebase branch_name                      # 将 branch_name 分支之上超前的提交,衍合到当前分支
git rebase --onto branch_name commit_sha1   # 限制回滚范围,rebase 当前分支从 commit 以后的提交
git rebase --interactive                    # 交互模式  
git rebase --continue                       # 处理完冲突继续衍合
git rebase --skip                           # 跳过    
git rebase --abort                          # 取消衍合

远端

Git 是一个分布式版本管理工具,所以可以支持多个仓库。服务器上的仓库在本地称为远端(remote)或源(origin)。

git remote add origin_name url              # 添加源

git remote                                  # 显示全部源
git remote -v                               # 显示全部源及其详细信息
git remote show origin_name                 # 查看指定源的全部信息

git remote rename origin1 origin2           # 重命名源
git remote rm origin_name                   # 删除源

远端分支

git fetch origin remotebranch[:localbranch] # 拉取远端分支[到本地指定分支]
git merge origin/branch                     # 合并远端上指定分支到本地当前分支
git pull origin remotebranch:localbranch    # 拉取远端分支到本地指定分支并合并(pull = fetch + merge)

git push origin branch                      # 推送当前分支到其链接的远端分支
git push origin localbranch:remotebranch    # 推送本地指定分支到远端上指定分支
git push origin :remotebranch               # 删除远端指定分支

git checkout -b [--track] localbranch origin/remotebranch   # 基于远端分支新建本地分支[同时设置跟踪]

标签

当项目到一定阶段时,给不同的提交或分支打标签做标记是很有用的。

git tag                                     # 列出现有标签    

git tag tag_name                            # 新建标签
git tag -a tag_name -m 'message'            # 新建带注释标签

git checkout tag_name                       # 切换到标签

git push origin tag_name                    # 推送分支到源上
git push origin --tags                      # 一次性推送所有分支

git tag -d tag_name                         # 删除标签
git push origin :refs/tags/tag_name         # 删除远程标签

你可能感兴趣的:(git)