git
开始
非常好的入门:
非常好的入门1[1]:
- 工作区:就是你在电脑里能看到的目录。
- 暂存区:英文叫stage, 或index。一般存放在 “.git目录下” 下的index文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)。
- 版本库:工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。
-
help
git help <verb>
-
init
-
config
123git config --global user.name "your name"git config --global user.email "your email"git config --global core.editor "vim #set vim as editor列出设置的config
git config --list
-
difftool
git config --global diff.tool bc3
没有默认的difftool,如果首次输入命令git difftool
,CentOS会自动推荐kompare。12Viewing: 'particles/BinReader.cc'Launch 'kompare' [Y/n]: ykompare的效果非常好。
-
completion by bash-completion[2]
操作
- add filename
- commit
git commit -m "your message"
- Amending the most recent commit message[3]
git commit --amend
will open your editor, allowing you to change the commit message of the most recent commit. Additionally, you can set the commit message directly in the command line with:git commit --amend -m "New commit message"
…however, this can make multi-line commit messages or small corrections more cumbersome to enter.Make sure you don’t have any working copy changes staged before doing this or they will get committed too. (Unstaged changes will not get committed.)
-
clone
git clone /path/to/repository
如果是远端服务器上的仓库,你的命令会是这个样子:
git clone username@host:/path/to/repository
git clone git@github.com:dunitian/Windows10.git "F:/Work/WP/Windows10" #到指定文件夹
git clone -b your_branch your_repo #指定branch
-
push
git push origin master:master
origin is the remote server; the branch name before the colon is local branch name, and that after the colon is remote branch name. e.g.:12git push origin HEAD:refs/for/branch1 # push HEAD branch to a remote branchgit push origin :refs/for/branch1 # delete remote branch -
checkout
git checkout hexo myfile
从hexo分支得到myfilegit checkout -b dev #-b参数表示创建并切换
git checkout -b dev your_SHA1 # 创建并切换到your_SHA1 commit
git checkout mastergit checkout --track local_branch origin/remote_branch
create a local branch based on a remote-tracking branch.(In recent versions of git the “–track” option is actually unnecessary since it’s implied when the final parameter is a remote-tracking branch, as in this example.)][4]
The “–track” option sets up some configuration variables that associate the local branch with the remote-tracking branch. These are useful chiefly for two things:- They allow git pull to know what to merge after fetching new remote-tracking branches.
- If you do git checkout to a local branch which has been set up in this way, it will give you a helpful message such as:
Your branch and the tracked remote branch 'origin/master’
have diverged, and respectively have 3 and 384 different
commit(s) each. -
stash[5]
储藏会处理工作目录的脏的状态 - 即,修改的跟踪文件与暂存改动 - 然后将未完成的修改保存到一个栈上,而你可以在任何时候重新应用这些改动。
git stash
临时存储当前状态
git stash list
git stash apply (--index)
找回临时存储的状态
git stash drop
删除stash -
branch
- git merge dev
合并指定分支到当前分支 - git branch -d dev #删除
git branch -r/-a
# 查看远程/所有分支;
git branch
查看本地分支git checkout mybfranch
# shift to another branch
- git merge dev
-
pull
git pull origin master
相当于git fetch origin # fetch to local repo
加上git merge origin/master # origin master shall be in local repo
-
fetch
git fetch origin hexo
从remote repo获取名为hexo的branch
丢弃
-
git rm
(then git commit)
git rm --cached file_a 从stage中删除
git rm file_a 从stage中删除,同时删除物理文件
git mv file_a file_b
-
git checkout -- file
(other functions about checkout is discribed above )
git checkout其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。- 删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:
git checkout -- test.txt
- 场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令
git checkout -- file
。
场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file
,就回到了场景1,第二步按场景1操作。
要重返未来,用git reflog
查看命令历史,以便确定要回到未来的哪个版本。
- 删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:
-
reset
git reset --hard HEAD^
git reset --hard 3628164
显示状态
-
status
-
log
git log --oneline --decorate # 加上--decorate 时,我们可以看到我们的标签
git log remotename/branchname # get remote commit. e.g., git log origin/hexo
Will display the log of a given remote branch in that repository, but only the logs that you have “fetched” from their repository to your personal “copy” of the remote repository.
-
reflog 查看历史命令
与git log
的区别:显示整个本地仓储的commit, 包括所有branch的commit, 甚至包括已经撤销的commit, 只要HEAD发生了变化, 就会在reflog里面看得到。 git log只包括当前分支的commit. -
remote
看当前配置有哪些远程仓库,可以用命令
git remote
执行时加上 -v 参数,你还可以看到每个别名的实际链接地址。 -
git ls-files #ls files in present branch
git ls-files -u #显示冲突的文件,-s是显示标记为冲突已解决的文件
-
diff
git diff #对比工作区和stage文件的差异
git diff --cached
对比stage和branch之间的差异git diff master remotes/origin/hexo #对比本地“master” branch和远程 "remotes/origin/hexo" branch
git diff origin/hexo #对比当前working tree和远程branch
rebase
git rebase 用法
stackoverflow
Git-rebase 小筆記 很好
- git rebase --skip
- git rebase --continue #use this when you solved conflicts.
- git rebase --abort #放弃当前rebase
tag
git tag <tagname>
git tag <tagname> -a
add an annotated tag
git push origin --tags
show
git show HEAD~4:altcoin_spot_future_hedging.py
显示HEAD之前的第四个commit中altcoin_spot_future_hedging.py
文件的内容
其他概念与原理
conflict
Resolving a merge conflict using the command line
remote
remote是一个相对的概念,一个存储在本地其他文件夹的repo也可以是remote[4]
“remotes” are just nicknames for other repositories, synonymous with a URL or the path of a local directory – you can set up extra remotes yourself with “git remote”, but “git clone” by default sets up “origin” for you.
origin
It’s a repo! 顾名思义,origin就是一个名字,它是在你clone一个托管在Github上代码库时,git为你默认创建的指向这个远程代码库的标签。
git remote add https://github.com/TimChen314/TimChen314.github.io.git # You can set # set origin by yourself
(远程仓库名)/(分支名) 这样的形式表示远程分支
Note that when git branch -a
is used, there is ‘remote’ in front of branch name, e.g.:
|
|
local_branch_name:remote_branch_name
# 这是push,pull的时候顺序反过来
远程分支和本地分支的名字相同,可以省略远程分支的名字
refspec
<source-name>:<destination-name>
is a refspec
upstream vs. origin
This should be understood in the context of GitHub forks (where you fork a GitHub repo at GitHub before cloning that fork locally)
- upstream generally refers to the original repo that you have forked
(see also “Definition of “downstream” and “upstream”” for more on upstream term)- origin is your fork: your own repo on GitHub, clone of the original repo of GitHub
branch & hash[6]
you need to understand that branch and tag names are just pointers to hash values, which represent a single commit
only two types of branches, and they are storaged in[4]:
|
|
- Local branches
- Remote-tracking branches
tips
-
git clone下载慢
git config --global http.postBuffer 524288000
-
hub
在终端中输入$ hub browse
,它将直接在浏览器中自动地打开 git 仓库的 URL。Boom! -
Make the current commit the only (initial) commit in a Git repository?
about github
[how to bring up an issue?](hoomd Build_NP_reaction.shhoomd Build_NP_reaction.sh)
错误
-
prompt to input passphrase time and time again:
Git enter long passphrase for every push
Note that you can use ssh-key only if you use ssh to build remote connections.
git remote -v
should looks like:
origin git@github.com:TimChen314/MDTackle.git (fetch)
but not:
origin https://github.com/TimChen314/MDTackle.git
-
Can’t push to GitHub because of large file which I already deleted
A git branch is a chain of commits, e.g., c-base -> c1 -> c2 -> c3 …
If uploading c1 failed, then even you fix the problem in c2 and c3, the uploading will still fail because of c1.
reference
超好: git cheatsheet
常用git
Removing sensitive data from a repository