GIT 使用

来源:互联网 发布:java 单个文件压缩 编辑:程序博客网 时间:2024/05/22 00:33

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 -vorigin  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 -husage: 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=falsecredential.helper=osxkeychainuser.name=贺贺user.emai=hyc***@sinosoft.com.cnuser.email=zhulinger520@163.comfilter.lfs.smudge=git-lfs smudge %ffilter.lfs.required=truefilter.lfs.clean=git-lfs clean %fcore.repositoryformatversion=0core.filemode=truecore.bare=falsecore.logallrefupdates=truecore.ignorecase=truecore.precomposeunicode=trueremote.origin.url=https://github.com/springHyc/InterviewLibrary.gitremote.origin.fetch=+refs/heads/*:refs/remotes/origin/*branch.master.remote=originbranch.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

参考

  • 远程分支
  • git-简明指南
  • 保存密码