Git基本语法整理
git config 查看参数
view all of your settings and where they are coming from using :
git config --list --show-origin
git config <key> eg:git config user.name
设置签名 (不关联账号)
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
设置作者 (Editor)
$ git config --global core.editor emacs
在win10中需要指明编译器位置
In the case of Notepad++(或者vim), a popular programming editor, you are likely to want to use the 32-bitversion, since at the time of writing the 64-bit version doesn’t support all plug-ins. If you are on a32-bit Windows system, or you have a 64-bit editor on a 64-bit system, you’ll type something like this:
$ git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe'-multiInst -notabbar -nosession -noPlugin"
请求帮助
git help <verb>
git <verb> --help
man git-<verb>
eg:
git help config
初始化git仓库
git init
追踪文件
git add <name>
eg:git add *.c
提交文件
git commit -m 'commit message' <file_name>
eg:
git commit -m 'first commit' hello.txt
查看当前状态
git status
简便形式:
git status -s or git status -short
New files that aren’t tracked have a ?? next to them, new files that have been added to the staging area have an A, modified files have an M and so on. There are two columns to the output --the lefthand column indicates the status of the staging area and the right-hand column indicates the status of the working tree. So for example in that output, the README file is modified in the working directory but not yet staged, while the lib/simplegit.rb file is modified and staged. The Rakefile was modified, staged and then modified again, so there are changes to it that are both staged and unstaged.
ignoring files git忽略的文件
查看该文件 :
cat .gitignore
官方的文档: https://github.com/github/gitignore
查看具体的不同
It’s important to note that git diff by itself doesn’t show all changes made since your last commit — only changes that are still unstaged. If you’ve staged all of your changes, git diff willgive you no output.
-
工作区与暂存区比较
git diff
-
if you want to see what you’va staged that will go into your staged changes to your last commit
git diff --staged
-
see all of you’ve staged so far
git diff --cached
remove files
- 删除已追踪的文件的时候,使用第一条时需要手动添加修改到暂存区,而第二条命令会自动将删除记录添加到暂存区
rm <file_name>
git rm <file_name>
- 从暂存区删除而在工作区中保留
git rm --cache <file_name>
- 或在
.gitignore
中配置 (已经加入暂存区的需要用上一条命令删除)- 同时删除多个文件
git rm log/\*.log
Note the backslash () in front of the *. This is necessary because Git does its own filename expansion in addition to your shell’s filename expansion. This command removes all files that have the .log extension in the log/ directory. Or, you can do something like this:
git rm \*~
This command removes all files whose names end with a ~
重命名
git mv <name1> <name2>
mv <name1> <name2>
// 相当于复制了一个文件,所以需要执行以下指令删除源文件,并加入暂存区
git rm \<name1>
git add \<name2>
查看项目历史
git log
//以时间倒序排序的
- 可加的参数:
-p
or-patch
展示diff
-n
n 为正整数,仅展示最近的N条commit
--stat
查看简短的diff
--pretty
仅显示哈希值和commit message
--pretty=oneline
一条信息只占一行
--pretty=format:"%h - %an,%ar : %s "
自定义输出格式 progit的第42页对应有格式解释
--graph
用图表示分支情况
--since=2.weeks
限制时间
- 其他有用的参数 在44页
undoing Things
git commit --amend
本次提交会与上一次提交合并为一个commit,并覆盖上一次的提交信息
eg:
git commit -m 'Initial commit'
git add forgotten_file
git commit --amend
Unstaging a Staged File
git reset HEAD <file>
git restore --staged<file>
Unmodifying a Modified File
git checkout --<file>
git restore HEAD <file>
It’s important to understand that git checkout – is a dangerous command. Any local changes you made to that file are gone–Git just replaced that file with the last staged or committed version. Don’t ever use this command unless you absolutely know that you don’t want those unsaved local changes.
showing remote 显示远程链接
git remote -v
使用-v
能够展示url
添加远程仓库链接
git remote add <name> <url>
//name 作为该远程仓库的代号,可代替url
获取远程仓库的信息
git fetch <remote>
So, git fetch origin fetches any new work that has been pushed to that server since you cloned (or last fetched from) it. It’s important to note that the git fetch command only downloads the data to your local repository — it doesn’t automatically merge it with any of your work or modify what you’re currently working on. You have to merge it manually into your work when you’re ready.
git pull
pushing to your remote
git push <remote> <branch>
查看远程仓库的信息
git remote show <name>
重命名以及移除
git remote rename <name1> <name2>
将name1换成name2
git remote remove <name3>
移除name3
Tagging
list tag
git tag
git tag -l "V1.8*"
annotated tags
git tag -a v1.4 -m "my vision 1.4"
git show v1.4
查看信息
lightweight tags
git tag v1.4
查看的时候只会显示commit message
tagging later
git tag -a v1.3 <commit_hash_key>
sharing tags
by default the
git push
command doesn’t transfer tags to remote servers.
git push <remote_name> <tagname>
deleting tags
git tag -d <tagname>
git push <remote_name> --delete<tagname>
删除远程tag
checkout tags
git checkout v1.3
别名 Git Aliases
git config --global alias.<short_name> <full_name>
eg:git config --global alias.br branch
- 短句也行
eg:git config --glabal alias.unstage 'reset HEAD --'
As you can tell, Git simply replaces the new command with whatever you alias it for. However,maybe you want to run an external command, rather than a Git subcommand. In that case, you start the command with a ! character. This is useful if you write your own tools that work with a Git repository. We can demonstrate by aliasing git visual to run gitk
git config --global alias.visual '!gitk'
branch
creat a new branch
git branch testing
创建新分支,而新分支的头部在最近的一次commit,即与当前的HEAD指向的位置一样。
跳转分支
git checkout <branch_name>
git switch <branch_name>
创建并跳转新分支
git checkout -b <new branch name>
git switch -b <new branch name>
合并分支
git merge <branch name>
合并当前分支与指定分支
- fast forward 表示合并同一分支的前后不同版本,实质是指针的向前移动
删除分支
git branch -d <branch name>
产生冲突
- 进入冲突文件,修改冲突的地方
- 运行
git commit
来提交,完成合并
分支管理
展示所有分支
git branch
带* 为当前分支git branch -v
同时展示最后一次commit message
展示远程分支
git ls-remote <remote>
git remote show <remote>
更新远程库的本地库
git fetch <remote>
更新远程库
git push <remote> <branch>
将<branch>推送到远程
git push <remote> <branch>:<branch_name>
将本地库中的<branch>
push 到远程库中的<awesomebranch>
中
拉取远程库中新的分支
使用git fetch
时,只会获取一个不可以编辑的记录,需要新建一个分支才可以
git checkout -b <locoal_branch_name> <remote>/<remote_branch_name>
checking branches
Checking out a local branch from a remote-tracking branch automatically creates what is called a “tracking branch” (and the branch it tracks is called an “upstream branch”).
When you clone a repository, it generally automatically creates a master branch that tracks
origin/master
. However, you can set up other tracking branches if you wish — ones that track branches on other remotes, or don’t track the master branch. The simple case is the example you just saw, runninggit checkout -b <branch> <remote>/<branch>
. This is a common enough operation that Git provides the--track
shorthand:
git checkout --track origin/serverfix
更改追踪的分支
git branch -u <remote>/<branch>
If you already have a local branch and want to set it to a remote branch you just pulled down, or want to change the upstream branch you’re tracking, you can use the -u or --set-upstream-to option to git branch to explicitly set it at any time.
tips
- 可以使用
@{u}
或@{upstream}
来代替所在分支追踪的远程分支
When you have a tracking branch set up, you can reference its upstream branch with the @{upstream} or @{u} shorthand. So if you’re on the master branch and it’s tracking origin/master, you can say something likegit merge @{u}
instead ofgit merge origin/master
if you wish.
查看分支追踪情况
git branch -vv
pulling
git fetch
会拉取远程库中所有你没有的信息,同时不更改工作区git pull
本质上是git fetch
+git merge
会自动合并已追踪的分支
删除远程分支
git push <remote> --delete <branch>
只是删除指针,会保留数据一段时间
rebasing
在git中合并分支的方法有两种,分别是
merge
和rebase
.merge
会创建新的commit,而rebase
则是将原来的commit重新执行在合并的分支上
git rebase <branch>
git rebase --onto master server client
This basically says, “Take the client branch, figure out the patches since it diverged from the server branch, and replay these patches in the client branch as if it was based directly off the master branch instead.” It’s a bit complex, but the result is pretty cool