注:用于个人记录git命令,记录日常使用过以及查询过的git用法,仅供参考与备忘。
git工作区、暂存区、版本库
- 工作区:working diretory,就是你在电脑里能看到的目录。
- 暂存区:英文叫stage, 或index。一般存放在 ".git目录下" 下的index文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)。
- 版本库:工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。
把文件往Git版本库里添加的时候,是分两步执行的:
第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;
第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。
git rm --cached <file>
删除暂存区或分支上的文件, 但本地又需要使用, 只是不希望这个文件被版本控制。
运行之后会出现
deleted: <file>
和
untracked files:
<file>
git撤销add和撤销commit
git checkout . #撤销工作区所有修改
git checkout -- <file> #撤销工作区某个文件修改,
#这个操作很危险,会清除工作区中未添加到暂存区的改动!
git checkout HEAD . #直接撤销add到暂存区的内容,并撤销工作区内的修改。
#结果会是nothing to commit, working tree clean
git checkout HEAD -- <file> #对暂存区文件:直接撤销修改(修改不会进入工作区)
#对工作区修改:撤销其修改
#git checkout HEAD这个命令也是极具危险性的,因为不但会清除工作区中未提交的改动,也会清除暂存区中未提交的改动。
git reset HEAD #撤销所有文件的add
git reset HEAD -- <file> #撤销add。清空add命令向暂存区提交的关于file文件的修改(Ustage)
#-- 的作用是:将这之后的每个参数都视为文件名,若只有一个文件,可以不加 --
git reset --soft HEAD^ #撤销commit。
branch - How to remove local (untracked) files from the current Git working tree - Stack Overflow
清理本地untracked files
Step 1: is to show what will be deleted by using the -n
option:
# Print out the list of files and directories which will be removed (dry run)
git clean -n -d
Step 2:Clean Step - beware: this will delete files:
# Delete the files from the repository
git clean -f
- To remove directories, run
git clean -f -d
orgit clean -fd
- To remove ignored files, run
git clean -f -X
orgit clean -fX
- To remove ignored and non-ignored files, run
git clean -f -x
orgit clean -fx
Again see the git-clean
docs for more information.
Options
-f
,--force
If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to run unless given
-f
,-n
or-i
.
-x
Don’t use the standard ignore rules read from .gitignore (per directory) and
$GIT_DIR/info/exclude
, but do still use the ignore rules given with-e
options. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git reset) to create a pristine working directory to test a clean build.
-X
Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.
-n
,--dry-run
Don’t actually remove anything, just show what would be done.
-d
Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use
-f
option twice if you really want to remove such a directory.
git diff, git show查看工作区修改、暂存区修改、commit修改
How to get a list of all files that changed between two Git commits? - Stack Overflow
#查看工作区修改
git diff
git diff filename #指定文件
#查看暂存区文件修改内容(git add的文件所作的修改)
git diff --cached
#Git 1.6.1 及更高版本还允许使用下面的,效果是相同的,但更好记些
git diff --staged
git diff --staged filename #指定文件
#对比修改文件
# For files changed between a given SHA and your current commit:
git diff --name-only <starting SHA> HEAD
# or if you want to include changed-but-not-yet-committed files:
git diff --name-only <starting SHA>
# More generally, the following syntax will always tell you which files changed between two commits (specified by their SHAs or other names):
git diff --name-only <commit1> <commit2>
#查看上次commit修改详细内容
git show
git show filename #指定文件
#查看某次commit修改详细内容
git show commitid
git show commitid filename #指定文件
diff the same file between two different commits on the same branch
git - How do I diff the same file between two different commits on the same branch? - Stack Overflow
查看某一文件两个commit直接的修改内容
From the git diff
manpage:
git diff [--options] <commit> <commit> [--] [<path>...]
For instance, to see the difference for a file "main.c" between now and two commits back, here are three equivalent commands:
$ git diff HEAD^^ HEAD main.c
$ git diff HEAD^^..HEAD -- main.c
$ git diff HEAD~2 HEAD -- main.c
list only the names of files that changed between two commits
git - How to list only the names of files that changed between two commits? - Stack Overflow
git diff --name-only SHA1 SHA2
where you only need to include enough of the SHA to identify the commits. You can also do, for example
git diff --name-only HEAD~10 HEAD~5
to see the differences between the tenth latest commit and the fifth latest (or so).
git不克隆历史记录, Clone git repository without history?
git clone --depth 1 git@github.com:USERNAME/PROJECT.git
克隆下来内容的只有GitHub上最后一条的commit信息。
为不同的项目设置不同的用户名和邮箱
#找到项目所在目录下的 .git/文件夹,进入.git/文件夹,然后执行如下命令分别设置用户名和邮箱:
git config user.name "username"
git config user.email "example@example.com"
#然后执行命令查看config文件:cat config
#发现里面多了刚才配置的用户名和邮箱信息,即成功为该项目单独设置了用户名和邮箱
#为所有项目设置默认的用户名和邮箱,则执行如下命令(即多了"--global"修饰,添加为全局变量):
git config --global user.name "username"
git config --global user.email "example@example.com"
config文件:
[user]
name = username
email = example@example.com
修改最近一条的commit的信息,How to modify existing, unpushed commit messages?
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"
若要修改push到远程分支的信息,需要
git push <remote> <branch> --force
# Or
git push <remote> <branch> -f
警告:强制推送将用您本地的状态覆盖远程分支。
或者使用git rebase:
// n is the number of commits up to the last commit you want to be able to edit
git rebase -i HEAD~n
clone远程指定分支 How do I clone a specific Git branch? - Stack Overflow
法1:直接clone某个分支
git clone -b <branch> <remote_repo>
# Example:
git clone -b my-branch git@github.com:user/myproject.git
法2:先clone master再checkout到指定分支
#clone master分支
git clone git@github.com:user/myproject.git
#checkout 新分支
git checkout -b <branch-name> <origin/branch_name>
#example:
git checkout -b dev origin/dev
本地创建新项目 并push到github上
github上创建空白项目,不勾选readme .gitignore license
本地仓库:
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:USERNAME/project.git
git push -u origin master
本地创建新分支 并push到远程
#创建并切换分支
git branch new-branch
git checkout new-branch
#以上两句等效于:
git checkout -b new-branch
git push -u origin new-branch
# 如果未设置上游分支,需要添加
git remote add origin git@github.com:username/project.git
git push -u origin new-branch
#附:
#删除远程
git remote remove origin
创建空分支
https://www.cnblogs.com/wangyingblock/p/10431183.html
背景:项目进行中,需要创建一个空分支。在Git中创建分支,是必须有一个父节点的,也就是说必须在已有的分支上来创建新的分支,如果你的工程已经进行了一段时间,这个时候是无法创建空分支的。
解决方法:
使用 git checkout的--orphan参数:
git checkout --orphan 2.0.2
该命令会生成一个叫2.0.2的分支,该分支会包含父分支的所有文件。但新的分支不会指向任何以前的提交,就是它没有历史,如果你提交当前内容,那么这次提交就是这个分支的首次提交。
删除所有文件:
我们想要空分支,所以我们需要把当前内容全部删除,用git命令
git rm --cached -r . #删除cached内容
git rm -rf . #删除工作区内容
注意:最后的‘.’不能少。
提交分支:
如果没有任何文件提交的话,分支是看不到的,所以我们需要创建一个新文件,然后提交则新创建的branch就会显示出来。
echo '# new branch' >> README.md
git add README.md
git commit -m 'new branch'
最后push到远程仓库,则新的空分支就创建成功了。
git push origin 2.0.2
合并unrelated分支 fatal: refusing to merge unrelated histories
Git refusing to merge unrelated histories on rebase - Stack Overflow
git merge dev --allow-unrelated-histories
删除分支
#删除本地分支
git branch -d branch-name
#删除github远程分支
git push origin :branch-name
分支名前的冒号代表删除
查看上游分支 How can I see which Git branches are tracking which remote / upstream branch? - Stack Overflow
git branch -vv # doubly verbose!
#查看远程分支
git branch -r
#查看所有分支
git branch -a
命令行查看分支图 git log - Pretty git branch graphs - Stack Overflow
git log --graph --oneline --all #查看所有commit
git log --graph --oneline --all -10 #仅查看10条commit
# I have two aliases I normally throw in my ~/.gitconfig file:
[alias]
lg1 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
lg = !"git lg1"
#命令
git lg/git lg1
Many of the answers here are great, but for those that just want a simple one line to the point answer without having to setup aliases or anything extra, here it is:
git log --all --decorate --oneline --graph
Not everyone would be doing a git log all the time, but when you need it just remember:
"A Dog" = git log --all --decorate --oneline --graph
git config --global alias.adog "log --all --decorate --oneline --graph"
-