Git的一些实用命令

git 随笔

转眼之间我来到公司已经一个月了,回想着一个月的变化,接触的东西还是有很大的变化。比如 git。网上也有很多关于git的基础命令,比如Chillax’s blog–Git常用的基础命令,但是我更想实例化些。下面我就把这些命令放在具体的实例当中来演示。


一、如何把本地的项目放到自己的远程仓库(‘repository’)

在这个例子当中我是使用的 github 上的远程仓库,如果没有 github 的账号申请一个也是so easy.

  1. 首先进入到你想要上传的项目文件夹,然后初始化:git init

    1
    git init
  2. 添加远程仓库,在此默认你已经在 github 创建了你的版本仓库,比如我的远程仓库地址是: git@github.com:carpentercloud/testdist.git

    1
    git remote add origin git@github.com:carpentercloud/testdist.git
  3. 远程仓库已经添加好,接下来就是把本地文件上传到远程仓库

    1
    2
    3
    git add . #跟踪所有文件,别忘了add与.有个空格
    git commit -m "写下你对提交的描述" #提交所跟踪的文件
    ggpush #推送到远程仓库

二、如何在github的仓库中展示自己的项目,即 index.html

这里要说一下,要想在github上看到自己的项目展示,首先你得新建一个分支,废话不多说,还是上图来的快些

  1. 点击branch,输入新的分支名字
    dd
  2. 点击设置
    dd
  3. 找到Github Pages,你就能看到生成的项目展示地址
    dd

三、Git常用的一些命令

  1. 查看已有的配置信息

    1
    git congfig -l
  2. 获取帮助,例如获取config命令的帮助信息

    1
    git help congfig
  3. 初始化新仓库

    1
    git init
  4. 跟踪文件(将文件加到暂存区)

    1
    2
    git add 文件名 #具体某个文件
    git add . #跟踪全部文件
  5. 提交更新

    1
    git commit -m "你的描述"
  6. 上传/推送到仓库

    1
    git push origin master #推送到主分支
  7. 从远程仓库克隆

    1
    git clone git@github.com:youname/project.git
  8. 检查当前文件状态

    1
    git status
  9. 创建.gitignore文件

    1
    touch .gitignore
  10. 查看修改之后有没有暂存起来

    1
    git diff
  11. 移除文件

    1
    git rm 文件名
  12. 重命名文件

    1
    git mv oldname.txt newname.txt
  13. 查看提交历史

    1
    git log
  14. 取消暂存文件

    1
    git reset HEAD 文件名
  15. 添加远程仓库(远程仓库已建好)

    1
    git remote add origin 你的远程仓库地址
  16. 从远程仓库抓取数据(还没有用过)

    1
    git fetch [remote-name]
  17. 远程仓库的删除

    1
    git remote rm origin
  18. 创建分支

    1
    git branch branch-name
  19. 切换分支

    1
    2
    git checkout branch-name
    git checkout -b branch-name #创建并切换的该分支

  • 查看所有分支

    1
    git branch
  • 删除分支

    1
    2
    git branch -d branch-name
    git branch -D branch-name #强制删除,不会提示错误信息
  • 合并分支

    1
    git merge branch-name #先切换到主分支再合并
  • 暂存本地修改和恢复本地修改

    1
    2
    3
    4
    5
    6
    7
    git stash # save uncommitted changes
    # pull, edit, etc.
    git stash list # list stashed changes in this git
    git show stash@{0} # see the last stash
    git stash pop # apply last stash and remove it from the list
    git stash --help # for more info

以上基础命令参考博客地址:Chillax’s blog–Git常用的基础命令.
如果想进一步理解git可以查看:常用 Git 命令清单


-- EOF --