工做区(Working Directory)
暂存区(stage)
版本库(Repository)
中央服务器
git add(stage) -> 暂存区 -> git commit -> 本地版本库 -> git push 服务器
Git的版本库里存了不少东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为咱们自动建立的第一个分支master,
以及指向master的一个指针叫HEADjava
清除缓存,使.gitignore生效git rm -r --cached .
linux
git init git add . 或者 git add name.file git commit -m "first commit" 或者 git commit -m "first commit" name.file git remote add origin https://gitee.com/*.git git push -u origin master 或者 git push (只会提交暂存区的文件)
添加文件的时候,咱们用git add
命令,修改代码也是用这个命令git add
,是否是以为很奇怪。git
windows
在windows下第一次输入用户名和密码后,会自动记录用户名和密码,下次提交的时候不须要重复输入。github
linux/mac os
在用户~目录下shell
$ cd $ vi .git-credentials https://username:password@github.com 若是你用的是gitee的话 https://username:password@gitee.com $ git config --global credential.helper store $ cat .gitconfig [credential] helper = store
git fetch和get pull都是将服务器代码更新到本地windows
git fetch origin master git log -p master..origin/master git merge origin/master
另一种方式缓存
git fetch origin master:tmp git diff tmp git merge tmp
git pull:从远程获取最新版本并合并到本地,至关于git fetch和get merge命令。服务器
git reset
git statusfetch
首先,在Git中,用HEAD表示当前版本,上一个版本就是HEAD^,上上一个版本就是HEAD^^,往上100个版本写成HEAD~100
Git容许咱们在历史版本中切换,使用命令git reset
能够回退到任意一个历史版本中。
git reset有三个选项,--hard、--mixed、--soft指针
//仅仅只是撤销已提交的版本库,不会修改暂存区和工做区
git reset --soft 版本库ID
//仅仅只是撤销已提交的版本库和暂存区,不会修改工做区
git reset --mixed 版本库ID
//完全将工做区、暂存区和版本库记录恢复到指定的版本库
git reset --hard 版本库ID
$ git log commit 611dd5e339d3748e8b54620a203988*** Author: xxx <xxx@163.com> Date: Sat Nov 11 18:08:28 2017 +0800 first commit $ git reset --hard commit_id //将全部的文件都恢复到id版本 //恢复到上一个版本 $ git reset --hard HEAD^ // 恢复到前两个版本 $ git reset --hard HEAD~2
用git log能够查看提交历史,以便肯定要回退到哪一个版本。
用git reflog查看命令历史,以便肯定要回到将来的哪一个版本.
从工做区撤销,其实就是删除后从新检出
rm -f hello.c git checkout [commit_id] hello.c
从暂存区撤销git reset HEAD 文件名
首先把文件添加到.gitignore
里面忽略掉,而后提交使.gitignore生效。
执行命令,删除版本库中文件但不删除本地文件
git rm --cached .DS_Store //--cached不删除本地,只删除暂存区的文件 git commit -m "remove ds_store" //提交 git push //将修改提交带版本库,而后查看版本库就看到文件删除了
git rm -r --cached xxx.iml //-r 若是存在子文件则递归删除 (git add xxx.iml) //若.gitignore文件中已经忽略了xxx.iml则能够不用执行此句 git commit -m "delete file" git push
git checkout -- test.c git checkout -- '*.c' git checkout branch-name //切换分支
git mv -f old_filename new_filename git add -u new_filename git commit -m "rename" git push
git add的几个选项
git add -u:将文件的 修改、删除,添加到暂存区。
git add .:将文件的 新建、修改,添加到暂存区。
git add -A:将文件的 新建、修改、删除,添加到暂存区。
warning: LF will be replaced by CRLF in projects.iml.
The file will have its original line endings in your working directory.
在linux和mac os中换行符表示为LF,可是在windows中卫CRLF。git config --global core.autocrlf true
git config --global core.autocrlf true
Configure Git on Windows to properly handle line endings
解释:core.autocrlf是git中负责处理line endings的变量,能够设置三个值--true,inout,false.
设置成三个值会有什么效果呢?
【1】If core.autocrlf is set to true, that means that any time you add a file to the git repo that git thinks is a text file, it will turn all CRLF line endings to just LF before it stores it in the commit.。
设置为true,添加文件到git仓库时,git将其视为文本文件。他将把crlf变成lf。
【2】If core.autocrlf is set to false, no line-ending conversion is ever performed, so text files are checked in as-is. This usually works ok。
设置为false时,line-endings将不作转换操做。文本文件保持原来的样子
git diff是对比本地仓库(local repository)和远程仓库(remote repository)之间的差别,不包括未提交的和stage内的。
//修改任意本地文件,而后执行下面命令 $ git diff master origin/master //控制台无任何信息 $ git add . $ git diff master origin/master $ git commit -m "update" $ git diff master origin/master $ diff --git a/excel-upload/src/test/java/org/xiaog/config/UploadConfigTest.java b/excel-upload/src/test/java/org/xiaog/config/UploadConfigTest.java index a6067c5..7ba72d7 100644 --- a/excel-upload/src/test/java/org/xiaog/config/UploadConfigTest.java +++ b/excel-upload/src/test/java/org/xiaog/config/UploadConfigTest.java ....
git init --bare
建立一个裸仓库,只管理版本信息,不会有work tree,也就是不会有代码文件,也不会有.git文件夹,这个裸仓库只有.git文件夹里面的内容。
git remote -v
能够看到origin的信息。分别是push和fetch的git地址,且指向的是同一个git服务器地址。git push
和git push origin master:master
默认状况下,这两个命令实际上是同样的,都是将本地仓库的代码提交到远程仓库的master分支。第一个master是本地后面的值远程仓库master