Git使用及安装

Git常用命令

  1. git init #使用当前目录作为Git仓库
    git init newrepo #使用指定目录作为Git仓库
  2. git clone git.example.com:repositories/hello.git #获取
  3. git checkout -b branchname #创建并切换到分支
    git checkout -f $filename #还原被修改文件
    git checkout -f && git clean -df #清除当前所有修改
  4. git branch -D branchname #删除分支
    git branch -a #查看所有分支
  5. git add/rm #从本地仓库增删,结果将会保存到本机的缓存里面
    git add -A #提交全部修改
  6. git status #查看状态
  7. git commit -m “注释” #提交,把本机缓存中的内容提交到本机的 HEAD 里面
  8. git pull origin master #从远程仓库中下载新的改动
    git push origin master #把本地的 commit(提交) push 到远程服务器上
  9. git log #查看提交信息
    git log -n -2 #查看最近两次的提交
  10. git whatchhange #查看每个提交修改的文件
  11. git reset HEAD read.md #还原已经提交的文件
  12. git remote -v #查看下载地址
  13. git commit --amend -m #“修改 提交 说明” 修改最后一次提交注释
  14. git reset --hard “commit ID号” #回退到指定版本
  15. 标签操作
  • git tag -a 0.1.3 -m “Release version 0.1.3” #详解:git tag 是命令, -a 0.1.3是增加 名为0.1.3的标签, -m 后面跟着的是标签的注释
  • git push origin --tags #提交标签到远程服务器上 –tags参数表示提交所有tag至服务器端,普通的git push origin master操作不会推送标签到服务器端。
  • git tag -d 0.1.3 #删除标签的命令
  • git push origin :refs/tags/0.1.3 #删除远端服务器的标签
  • git show v1.0.0 #查看 tag 详情
  1. git cherry-pick #合并某次提交到另一个分支(必须同一个仓库)
  2. git fetch <远程主机名> #将某个远程主机的更新,全部取回本地
  3. git rebase #从父线上拉下来最新的代码到自己的分支
  4. git merge branch_name #从子分支的修改合并到父线上
  5. git submodule #开发过程中,经常会有一些通用的部分希望抽取出来做成一个公共库来提供给别的工程来使用,而此命令可以管理公共代码库的版本
  • git submodule add 仓库地址 路径 #为当前工程添
    其中,仓库地址是指子模块仓库地址,路径指将子模块放置在当前工程下的路径。注意:路径不能以 / 结尾(会造成修改不生效)、不能是现有工程已有的目录(不能順利 Clone)
  • git rm –cached #删除
    submodule的删除稍微麻烦点:首先,要在“.gitmodules”文件中删除相应配置信息。然后,执行“git rm –cached ”命令将子模块所在的文件从git中删除
  • git submodule update --init --recursive #下载的工程带有submodule
    当使用git clone下来的工程中带有submodule时,初始的时候,submodule的内容并不会自动下载下来的,此时,只需执行此命令
  1. 直接Clone连接时,设置记住密码
  • git config –global credential.helper cache #设置记住密码(默认15分钟)
  • git config credential.helper ‘cache –timeout=3600’ #自己设置时间
  • git config –global credential.helper store #长期记住密码
  1. 忽略提交某个指定的文件(不从版本库中删除)
  • git update-index --assume-unchanged test.c (忽略提交)
  • git update-index --no-assume-unchanged test.c (取消忽略指令)
  1. 差分包制作
  • git archive -o test.zip HEAD $(git diff ID1 ID2 --name-only)
  1. 推送所有分支
    • git push --all origin -u
  2. 待续 …

Git安装

  1. 安装git
    ubuntu: sudo apt-get install git
    windows: 下载git安装

  2. 打开终端
    ubuntu: 直接打开终端
    windows: 在git工作目录下—右键—Git Base Here

  3. 配置本机git的两个重要信息,user.name和user.email,中终端输入如下命令即可设置
    git config --global user.name “hanbo” #用户名
    git config --global user.email “hanbo@email.com” #用户邮箱
    git config --list #查看是否设置成功。

  4. 配置ssh key上传代码时使用这个 sshkey 来确认是否有上传权限
    ssh-keygen -t rsa -C “hanbo@email.com” #创建ssh
    会生成.ssh目录中两个文件id_rsa和id_rsa.pub。id_rsa:私钥;id_rsa.pub:公钥,打开id_rsa.pub赋值里面的内容;
    ubuntu路径/home/username/.ssh/id_rsa.pub
    windows路径C:\Documents and Settings\Administrator.ssh\id_rsa.pub

  5. 进入你的github,进入Profile Settings->SSH keys->Add SSH Keys,然后在Key那栏下面将公钥拷贝的内容粘贴进去,最后点击 Add key按钮添加。

  6. 验证:ssh -T git@github.com
    出现 Hi xxx! You’ve successfully authenticated, but GitHub does not provide shell access. 说明配置成功

  7. 如果使用局域网域名,则需要添加本地域名解析
    ubuntu: vi /etc/hosts
    windows: 管理员打开C:\Windows\System32\drivers\etc\hosts

    在最后添加: 192.168.1.11 git.example.com #IP 域名

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值