GIT 使用

Git 简介

Git是什么?
Git是目前世界上最先进的分布式版本控制系统(没有之一)。
Git有什么特点?简单来说就是:高端大气上档次!

上面是国内某牛人的git介绍。想详细了解git的同学可以访问一下的网址,花几分钟的时间学习一下世界上最先进的分布式版本控制系统!

Git常用命令

  • git clone username@host:/path/to/repository(地址) – 可以从git服务器上下载一个项目
  • git pull origin master(分支名称) – 将当前分支master分支上的所有远程仓库中的代码下载到本地仓库
  • git fetch – 将当前目录下的分支都下载到本地,但是不合并分支
  • git checkout -b branch – 新建branch分支
  • git branch – 查看本地分支
  • git branch -a – 查看当前文件的所有分支,包括远程分支
  • git status -s – 查看当前分支下的所有有改动的文件
  • git diff xxx(文件) – 查看当前文件具体有哪些改动
  • git add (文件) – 将某一个改动的文件放到本地缓存中去
  • git add * – 将当前分支下的所有改动的文件全部放到本地缓存中去
  • git commit -m “注释” – 将当前add的文件提交到本地仓库中去
  • git push origin master(分支名称) 将当前分支的所有本地仓库中的改动提交到远程仓库中
  • git checkout -b [分支名] [远程名]/[分支名]
  • git rm – 删除文件

  • 删除本地文件,并更新到远程仓库中

    0. 删除本地文件
    1. git status -s // 查看有更改的文件
    2. $ git rm interView/javaScript/text.txt  // 在本地仓库中删除更改手动删除的文件
    3. $git commit -m "chore: 删除多余的文件"
    4. $git push origin master  // 提交

    这样就将本地删除的文件同步到远程仓库中了。

工作流

你的本地仓库由 git 维护的三棵“树”组成。第一个是你的* 工作目录,它持有实际文件;第二个是 暂存区(Index),它像个缓存区域,临时保存你的改动;最后是 HEAD*,它指向你最后一次提交的结果。

添加和提交

你可以提出更改(把它们添加到暂存区),使用如下命令:

git add <filename>
git add *

这是 git 基本工作流程的第一步;使用如下命令以实际提交改动:

git commit -m "代码提交信息"

现在,你的改动已经提交到了 HEAD,但是还没到你的远端仓库。

推送改动

你的改动现在已经在本地仓库的 HEAD 中了。执行如下命令以将这些改动提交到远端仓库:

git push origin master

可以把 master 换成你想要推送的任何分支。

如果你还没有克隆现有仓库,并欲将你的仓库连接到某个远程服务器,你可以使用如下命令添加:

git remote add origin <server>

如此你就能够将你的改动推送到所添加的服务器上去了。

远程仓库

上面的命令git remote add <name> <url>可以将你的本地仓库连接到某个远程服务器。同样的的,一个项目也可以连接到多个远程服务器。其中name也就是这个远程仓库的名称,url是这个远程仓库的URL地址。例如

git remote add outOrigin https://github.com/springHyc/InterviewLibrary.git

可以使用git remote -v来查看该项目的远程仓库的连接情况。eg:

ehe:InterviewLibrary hehe$ git remote -v
origin  https://github.com/springHyc/InterviewLibrary.git (fetch)
origin  https://github.com/springHyc/InterviewLibrary.git (push)

同样的可以使用命令git remote remove <name>删除一个连接的远程仓库地址。

查看更多有关remote的命令,可以使用git remote --h来进行查看。

hehe:InterviewLibrary hehe$ git remote -h
usage: git remote [-v | --verbose]
   or: git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>
   or: git remote rename <old> <new>
   or: git remote remove <name>
   or: git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
   or: git remote [-v | --verbose] show [-n] <name>
   or: git remote prune [-n | --dry-run] <name>
   or: git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]
   or: git remote set-branches [--add] <name> <branch>...
   or: git remote get-url [--push] [--all] <name>
   or: git remote set-url [--push] <name> <newurl> [<oldurl>]
   or: git remote set-url --add <name> <newurl>
   or: git remote set-url --delete <name> <url>

    -v, --verbose         be verbose; must be placed before a subcommand

查看/修改用户名密码

用户名和邮箱地址是本地Git客户端的一个变量,不随git库而改变。每次commit都会用用户名和邮箱纪录。github的contributions统计就是按邮箱来统计的。

  • 查看用户名和邮箱地址
$ git config user.name
$ git config user.email
  • 修改用户名和邮箱地址
$ git config --global user.name "username"
$ git config --global user.email "email"
  • 查看git配置
$ git config --list

各个配置项的含义

hehe:InterviewLibrary hehe$ git config --list // 查看命令
core.trustctime=false
credential.helper=osxkeychain
user.name=贺贺
user.emai=hyc***@sinosoft.com.cn
user.email=zhulinger520@163.com
filter.lfs.smudge=git-lfs smudge %f
filter.lfs.required=true
filter.lfs.clean=git-lfs clean %f
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
remote.origin.url=https://github.com/springHyc/InterviewLibrary.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

https或http方式设置记住用户名和密码的方法

GitHub上下载的项目,尤其是团队共享的项目很可能每次提交时都需要输入账号密码,很是麻烦。可以使用一下命令来解决这个麻烦。

在输入完账号密码后,可以执行下面的命令来记住密码。

  • 设置记住密码(默认15分钟):
    git config --global credential.helper cache
  • 如果想自己设置时间,可以这样做:
    git config credential.helper 'cache --timeout=3600'
    这样就设置一个小时之后失效
  • 长期存储密码:
    git config --global credential.helper store

参考

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值