git常用指令

来源:互联网 发布:中超2017点球数据 编辑:程序博客网 时间:2024/06/06 06:57

Git指令

  1. 配置SSH

    • $ ssh-keygen -t rsa -C “email address”
  2. 创建仓库

    • git init 初始化仓库
    • git clone [remote repository] 复制远程仓库到本地
  3. 查看指令

    • git status 查看将被提交的修改
    • git diff [filename] 查看文件修改的内容
    • git log 查看提交历史
    • git reflog 查看执行过的命令
    • git remote : 查看远程仓库信息
    • git remote -v 查看更详细的远程仓库信息
  4. 回退指令

    • git reset - -hard [HEAD] 彻底回退到某个版本,本地的源码也会变为上一个版本的内容
    • git reset - -soft [HEAD] 回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可
    • git reset - -mixed [HEAD] 默认方式,回退到某个版本,只保留源码,回退commit和index信息
    • git checkout - - [filename] 撤销在工作区修改
    • git reset HEAD [filename] 撤销在缓存区的修改
    • git commit –amend 修改commit的注释
  5. 分支指令

    • git branch [branch] 建立新分支
    • git branch -f [branch] [commit id] 移动分支到指定提交点
    • git branch -d [branch] 删除分支
    • git branch -D [branch] 强制删除未合并的分支
    • git branch - -set-upstream branch-name origin/branch-name 建立本地分支和远程分支的链接关系,没有则报错“no tracking information”
    • git checkout -b [branch] 建立并切换到新分支
    • git checkout [branch] 切换分支
    • git checkout [HEAD] 移动HEAD指针
    • git checkout -b [branch] [origin/branch] 创建branch分支,关联远程分支
  6. 合并指令

    • git merge [branch] 合并某分支到当前分支(完整性)
    • git merge - -no-ff [branch] 强制合并时生成一个新的commit,保存完整性
    • git rebase [branch] 合并到指定分支(线性)
    • git rebase [branch1] [branch2] 合并branch2到branch1(线性)
    • git cherry-pick [branch]… 提取若干commit到当前指针下
    • git rebase -i [HEAD~i] 对之前(包括自己)的i个提交点,改变排序和移除无用提交点
  7. 删除指令

    • git rm [filename] 删除文件
    • git push origin :[branch] 删除远程分支
  8. 远程指令

    • git commit 提交修改
    • git commit -m “描述” 有描述的提交
    • git remote add origin [remote repository] 关联远程仓库
    • git push origin [branch] 推送到远程分支
    • git push -u origin [branch] 第一次推送建立远程追踪
    • git push origin [branch1]^:[branch2] 上传branch1前一个Commit到远程branch2分支
    • git pull 拉取远程分支
      等效于git fetch [origin/branch]; git merge [origin/branch]
    • git pull - -rebase 拉取远程分支
      等效于git fetch [origin/branch]; git rebase [branch] [origin/branch]
    • git fetch 拉取远程仓库的所有更新,并更新本地的远程分支
    • git fetch origin [branch] 拉取远程branch分支,放在本地的branch分支
    • git fetch origin [branch1]~1:[branch2] 拉取远程branch1分支的上一个commit到本地的branch2分支
    • git fetch origin :[branch] 本地创建一个新分支
  9. 暂停指令

    • git stash 暂停当前分支(未加入缓存区)
    • git stash list 查看暂停任务
    • git stash apply 恢复暂停任务
    • git stash drop 删除暂停任务
    • git stash pop 恢复暂停任务并删除该任务
  10. 标签指令

    • git tag [tag] 在当前分支打标签
    • git tag 查看所有标签
    • git tag [tag] [commit id] 对指定的提交打标签
    • git show [tag] 显示标签位置的信息
    • git tag -d [tag] 删除标签
    • git push origin [tag] 推送某个标签到远程仓库
    • git push origin - -tags 推送全部尚未推送的本地标签到远程仓库
    • 删除远程标签
      git tag -d [tag] 删除本地标签
      git push origin :refs/tags/[tag] 删除一个远程标签
    • git describe [branch]/[commit id] 查看距离分支/提交点的tag距离
  11. 自定义git

    • git config - -global color.ui true 显示颜色
    • git config - -global alias.st status 使st等效status
    • git config - -global user.name [username] 配置用户名
    • git config - -global user.email [eamil] 配置邮箱
    • git config - -global core.editor vi 使用vi编辑器
0 0
原创粉丝点击