查看“Git:基本操作”的源代码
←
Git:基本操作
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
[[category:Git]] == 常用操作 == [[File:Git基本操作.jpg|center|800px]] == 仓库 == {| class="wikitable" |- ! 命令 !! 说明 !! 备注 |- | '''<code>git init</code>''' | 初始化Git仓库 | # <code>git init</code>:在当前目录初始化Git仓库 # <code>git init <newrepo></code>:在指定目录(<code><newrepo></code>)初始化Git仓库 # |- | '''<code>git clone</code>''' | 克隆Git仓库 | # <code>git clone <repo></code>:克隆仓库(<code><repo></code>)到当前目录 # <code>git clone -o<远程主机名> <repo></code>:克隆仓库(<code><repo></code>)当前目录,并为远程主机设置主机名 # <code>git clone <repo> <directory></code>:克隆仓库(<code><repo></code>)到指定目录(<code><directory></code>) # |} * <code>git clone</code>可以所用不同的协议,包括<code>ssh</code>,<code>git</code>,<code>https</code>等: *# <code>git clone git@github.com:fsliurujie/test.git</code>:SSH协议 *# <code>git clone git://github.com/fsliurujie/test.git</code>:GIT协议 *# <code>git clone https://github.com/fsliurujie/test.git</code>:HTTPS协议 *: (常用ssh,因为速度较快,还可以配置公钥免输入密码) == 修改 == {| class="wikitable" |- ! style="width:8%"|命令 !! 说明 !! 备注 |- | '''<code>git add</code>''' | 将工作区新增或修改的文件添加到暂存区 | # <code>git add [file1] [file2]</code>:添加一个或多个文件到暂存区; # <code>git add [dir]</code>:添加指定目录到暂存区(包括子目录); # <code>git add .</code>:添加当前目录下的所有文件到暂存区; # <code>git add -f [file]</code>:强制添加存在于<code>.gitignore</code>的文件; |- | '''<code>git commit</code>''' | 提交暂存区到本地仓库 | # '''<code>git commit -m [message]</code>''':提交暂存区到本地仓库; # '''<code>git commit [file1] [file2] -m [message]</code>''':提交暂存区的指定文件; # '''<code>git commit ([file1] [file2]) -am [message]</code>''':直接提交(跳过<code>git add</code>); |- | '''<code>git status</code>''' | 查看仓库当前的状态,显示有变更的文件 | <syntaxhighlight lang="PowerShell"> Microsoft Windows [版本 10.0.19041.508] (c) 2019 Microsoft Corporation。保留所有权利。 D:\git\eijux>git status On branch master Your branch is up to date with 'eijux/master'. Changed not staged for commit: (use "git add <file>..." to include in what will be committed) (use "git restore --staged <file>..." to unstage) new file: 1.txt Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: 2.txt Untracked files: (use "git add <file>..." to include in what will be committed) 3.txt </syntaxhighlight> # Changed not staged for commit:修改文件,未add # Changes to be committed:修改并add,未commit # Untracked files:新增文件,从未add |- | '''<code>git diff</code>''' | 比较文件的不同,即暂存区和工作区的差异 | # '''<code>git diff ([file])</code>''':查看暂存区和工作区的改动; # '''<code>git diff --cached ([file])</code>''':(或<code>git diff --staged ([file])</code>)暂存区和上一个commit的差异; # '''<code>git diff HEAD ([file])</code>''':查看工作区与当前分支最新commit之间的差异; # '''<code>git diff [branchName] [file]</code>''':当前分支的文件与[branchName]分支的文件进行比较; # '''<code>git diff [commitId] [file]</code>''':当前分支的文件与[commitId]提交的文件进行比较; # '''<code>git diff --stat</code>''':显示摘要而非整个diff; # '''<code>git diff [first-branch]...[second-branch]</code>''':查看两次提交之间的差异; |- | '''<code>git stash</code>''' | 保存工作现场 | # '''<code>git stash list</code>''':查看所有保存的工作现场; # '''<code>git stash apply <stash></code>''':恢复stash(但不删除); # '''<code>git stash drop <stash></code>''':删除stash; # '''<code>git stash pop <stash></code>''':恢复并删除stash; # <stash>形如“stash@{0}”,可在list中看到: #* e.g. <code>git stash pop stash@{0}</code> |- | '''<code>git reset</code>''' | 回退版本 | <code>git reset [--soft | --mixed | --hard] [HEAD]</code><br/> 关于参数: # <code>--mixed</code>:(默认)重置缓存区未指定<code>commit</code>(工作区不变) # <code>--soft</code>:将<code>HEAD</code>指向指定<code>commit</code>(工作区与缓存区不变) # <code>--hard</code>:重置工作区、缓存区未指定<code>commit</code> 关于[HEAD]: # 当前版本:<code>HEAD^</code>或<code>HEAD~0</code> # 上一个版本:<code>HEAD^</code>或<code>HEAD~1</code> # 上上一个版本:<code>HEAD^^</code>或<code>HEAD^2</code> # 上上上一个版本:<code>HEAD^^^</code>或<code>HEAD^3</code> #: ''以此类推'' |- | '''<code>git revert</code>''' | 回退版本 | |- | '''<code>git mv</code>''' | 移动或重命名工作区文件 | # '''<code>git mv -f [file] [newfile]</code>''':强制执行; |- | '''<code>git rm</code>''' | 删除工作区文件 | # '''<code>git rm <file></code>''':将文件从暂存区和工作区中删除; # '''<code>git rm --cached <file></code>''':将文件从暂存删除,工作区保留(即停止追踪指定文件,通常是在文件曾经被git管理过,现在不需要被git接管的时候使用); # '''<code>git rm -r *</code>''':递归删除; # '''<code>git rm -f <file></code>''':强制删除; |} == 分支== {| class="wikitable" |- ! 命令 !! 说明 !! 备注 |- | '''<code>git branch</code>''' | 分支命令 | # '''<code>git branch</code>''':查看所有本地分支 # '''<code>git branch -r</code>''':查看所有远程分支 # '''<code>git branch -a</code>''':查看本地和远程的所有分支 # '''<code>git branch <branchname></code>''':创建<branchname>分支 # '''<code>git branch -d <branchname></code>''':删除<branchname>分支 # '''<code>git branch -D <branchname></code>''':强制删除一个未被合并过的分支<branchname> # '''<code>git branch --set-upstream <本地分支名> <远程主机名>/<远程分支名></code>''':手动在本地分支与远程分支间建立追踪关系; |- | '''<code>git checkout</code>''' | 分支切换 | # '''<code>git checkout <branchname></code>''':切换到<branchname>分支 # '''<code>git checkout -b <branchname></code>''':'''创建并切换'''到<branchname>分支 # '''<code>git checkout -b <本地新建分支> <远程主机名>/<远程分支></code>''':在远程分支基础上新建本地分支(先有远程主机信息<code>git fetch <远程主机名> <远程分支></code>) # '''<code>git checkout -- <fileName></code>''':“'''<code>--</code>'''”表示'''撤销工作区文件<fileName>的修改''' ## 工作区修改,但'''未add'''到暂存区:checkout重置到与'''版本库'''一致 ## 工作区修改并'''add后''',再次修改:checkout重置到与'''暂存区'''一致 : ''切换分支的时候,Git会用该分支最后提交的快照替换本地工作目录的内容。'' |- | '''<code>git merge</code>''' | 分支合并 | # '''<code>git merge <branch1></code>''':将<branch1>分支合并到当前分支; #: 将''branchB''和并到''branchA'':切换到''branchA''中执行“<code>git merge branchB</code>”。 # '''<code>git merge <branch1> <branch2> <branch3> <branch4></code>''':合并多个分支到当前分支; |- | '''<code>git rebase</code>''' | 分支合并 | |} 冲突合并:<br/> 冲突合并涉及文件添加、移除的操作,还包括文件修改内容的合并。 == 远程 == {| class="wikitable" |- ! style="width:8%"|命令 !! style="width:30%"|说明 !! 备注 |- | '''<code>git remote</code>''' | 管理远程仓库主机名 | # <code>git remote -v</code>:显示所有远程仓库 # <code>git remote show [remoteName]</code>:显示远程仓库的信息 #: 如:git remote show origin # <code>git remote add [remoteName] [url]</code>:添加远程版本库 #: 如:git remote add origin git@github.com:tianqixin/runoob-git-test.git # <code>git remote rm [remoteName]</code>:删除远程仓库 # <code>git remote rename [old_name] [new_name]</code>:修改远程仓库名 |- | '''<code>git pull</code>''' | 下载远程代码并合并:<br/> <code>git pull <远程主机名> <远程分支名>:<本地分支名></code> | Note1: # '''合并到当前分支''',可省略“'''<code>:<本地分支名></code>'''”; # 等效于<code>git fetch</code>+<code>git merge FETCH_HEAD</code>(概念上等效,实现上不一样???)。 Note2: # <code>git pull --rebase <远程主机名> <远程分支名>:<本地分支名></code>:合并需要采用'''rebase'''模式的pull; # <code>git pull -p</code>:若服务器删除了某分支,拉取远程分支的时候,同步删除本地的该分支; #: 等同于:<code>git fetch --prune <远程主机名></code> 、<code>git fetch -p</code>; Note3: # <code>git pull <远程主机名> <远程分支名></code> #: 拉取远程主机的分支,并与本地'''当前分支'''合并; # <code>git pull <远程主机名></code> #: 拉取远程主机的对应分支,并与本地'''当前分支'''合并(当前分支与远程分支必须存在追踪关系); # <code>git pull</code> #: 拉取远程主机的对应分支,并与本地'''当前分支'''合并(当前分支只有一个追踪分支); |- | '''<code>git push</code>''' | 上传远程代码并合并:<br/> <code>git push <远程主机名> <本地分支名>:<远程分支名></code> | Note1: # '''<code>git push --delete</code>''':删除远程主机的分支; # '''<code>git push --force</code>''':(慎用)强制推送,忽略远程分支版本比本地高(应先pull); # '''<code>git push --all</code>''':无论对应远程分支是否存在,将本地的所有分支都推送到远程主机; # '''<code>git push --tags</code>''':推送所有未推送过的本地标签; # '''<code>git push <tag></code>''':推送本地标签<tag>; Note2: # '''<code>git push -u <远程主机名> <本地分支名></code>''' #: 如果当前分支与多个主机存在追踪关系,则可以使用'''-u'''参数指定一个默认主机,这样后面就可以不加任何参数使用git push; # '''<code>git push <远程主机名> <本地分支名></code>''' #: 表示将本地分支推送到与之存在追踪关系的远程分支(通常两者同名),如果该远程分支不存在,则会被新建; # '''<code>git push <远程主机名> :<远程分支名></code>''' #: 表示删除对应的远程分支(推送空的本地分支到远程); # '''<code>git push <远程主机名></code>''' #: 表示将'''当前分支'''推送到远程主机的对应分支(当前分支与远程分支必须存在追踪关系); # '''<code>git push</code>''' #: 表示将'''当前分支'''推送到远程主机的对应分支(当前分支只有一个追踪分支); Note3:关于推送方式 # '''simple'''方式:不带任何参数的<code>git push</code>,默认只推送当前分支;(默认) # '''matching'''方式:推送所有有对应的远程分支的本地分支;(Git 2.0之前默认) # <code>git config</code>中的<code>push.default</code>可配置推送方式; |- | '''<code>git fetch</code>''' | 获取远程主机的版本库更新(commit):<code>git fetch [alias] [branch]</code><br/> <pre>Download objects and refs from another repository</pre> | Note1: # '''FETCH_HEAD''':是一个版本链接,记录在本地的文件中(.git/FETCH_HEAD),指向着目前已经从远程仓库取下来的分支的末端版本; # “git fetch”将: ## 更新“git remote”中所有远程仓库所包含分支的最新commit-id, 将其记录到“'''.git/FETCH_HEAD'''”文件中; ## 下载内容到“'''.git\objects'''”及“'''.git\refs'''”; # '''<code>git fetch</code>''' 与 '''<code>git pull</code>''': ## <code>git fetch</code>拉取更新信息(commitID),但不会合并到本地工作目录; ## <code>git pull</code>将本地库更新至远程库的最新状态; Note2: # '''<code>git fetch <远程主机名> <远程分支1> <远程分支2> <远程分支3></code>''':从远程仓库拉取多个分支; # '''<code>git fetch <远程主机名> <远程分支></code>''':拉取指定主机指定分支的更新; # '''<code>git fetch <远程主机名></code>''':拉取指定主机的更新(不指定分支时通常默认为master); # '''<code>git fetch </code>''':拉取所有分支(branch)的更新; Note3: # 拉取分支更新,并在本地新建分支: #* 不切换到该分支: ## '''<code>git fetch <远程主机名> <远程分支>:<本地新建分支></code>''' #* 并切换到该分支: ## (<code>git fetch <远程主机名> <远程分支></code>) ## '''<code>git checkout -b <本地新建分支> <远程主机名>/<远程分支></code>''' # 拉取远程分支更新,并合并到本地分支(与<code>git merge</code>配合使用): ## <code>git fetch origin master</code>:从'''origin'''拉取分支'''master'''的更新; ## <code>git merge origin/master</code>:合并'''origin/master'''分支到'''当前分支'''; # 拉取多分支更新: ## 拉取多个分支:<code>git fetch origin master stable oldstable</code>,从远程仓库origin拉取master、stable、oldstable分支的更新; ## 合并多个分支:<code>git merge origin/master hotfix-2275 hotfix-2276 hotfix-2290</code>,合并origin/master、origin/stable、origin/oldstable到当前分支。 |} == 日志 == {| class="wikitable" |- ! 命令 !! 说明 !! 示例 |- | '''<code>git log</code>''' | 查看版本提交历史 | <syntaxhighlight lang="PowerShell"> Microsoft Windows [版本 10.0.19041.508] (c) 2019 Microsoft Corporation。保留所有权利。 D:\git\eijux>git log commit 5e26159ad738b08b9321eba9ecaeff39c8acc42f (HEAD -> master, eijux/master) Author: Eijux <chen@eijux.com> Date: Fri Nov 1 01:18:47 2019 +0800 idea commit test 01<E7><82><B9>18<E5><88><86> commit ffa14211ab7088d9782de019328238dcfe09a4bc Merge: 3fbc79a fc32a4a Author: Eijux <chen@eijux.com> Date: Thu Oct 31 16:03:24 2019 +0800 Merge branch 'master' of github.com:Eijux/eijux commit 3fbc79ae2c8b7199c22133d1f3e4115848dbe749 Author: Eijux <chen@eijux.com> Date: Thu Oct 31 04:19:09 2019 +0800 idea commit test (END) </syntaxhighlight> |- | '''<code>git reflog</code>''' | 查看版本命令历史 | <syntaxhighlight lang="PowerShell"> Microsoft Windows [版本 10.0.19041.508] (c) 2019 Microsoft Corporation。保留所有权利。 D:\git\eijux>git reflog 5e26159 (HEAD -> master, eijux/master) HEAD@{0}: pull eijux master: Fast-forward fc32a4a HEAD@{1}: commit: add gitignore file d07be0a HEAD@{2}: commit: branch management test 492be10 HEAD@{3}: merge dev: Merge made by the 'recursive' strategy. fc1038b HEAD@{4}: checkout: moving from dev to master 9d0f76e (dev) HEAD@{5}: commit: write a line on branch dev 7640602 (eijux/dev) HEAD@{6}: checkout: moving from master to dev fc1038b HEAD@{7}: reset: moving to HEAD fc1038b HEAD@{8}: commit: branch back to master 7640602 (eijux/dev) HEAD@{9}: merge dev: Fast-forward 63fa5ac HEAD@{10}: checkout: moving from dev to master 7640602 (eijux/dev) HEAD@{11}: commit: add dev branch 63fa5ac HEAD@{12}: checkout: moving from master to dev 63fa5ac HEAD@{13}: pull eijux master --allow-unrelated-histories: Merge made by the 'recursive' strategy. da60d32 HEAD@{14}: commit: add t5.txt e88cb69 HEAD@{15}: commit: delete rmtest.txt cc1e40c HEAD@{16}: commit: changed t4 and add t5 1707196 HEAD@{17}: commit: changed t4 378fb5a HEAD@{18}: commit: add t4.txt e3501ab HEAD@{19}: commit: add t4 ea463d0 HEAD@{20}: commit: add t2.txt 92ad079 HEAD@{21}: commit: git commit single file test 061529e HEAD@{22}: reset: moving to 061529 69e722f HEAD@{23}: reset: moving to head 69e722f HEAD@{24}: reset: moving to head^ 061529e HEAD@{25}: reset: moving to 061529 69e722f HEAD@{26}: reset: moving to 69e722 061529e HEAD@{27}: commit: git diff test 69e722f HEAD@{28}: commit: banben test fd1fd5f HEAD@{29}: commit (initial): git add test </syntaxhighlight> |} == 标签 == tag实际就是一个指向commit的指针,将一个有意义的tag名称与commit相关联。<br/> git标签有两种类型: # 轻量级的(lightweight):实际上它就是个指向特定提交对象的引用。 # 含附注的(annotated):<pre>实际上是存储在仓库中的一个独立对象,有自身的校验和信息,包含标签名称,电子邮件地址和日期,及标签说明,标签本身也允许使用 GNU Privacy Guard (GPG) 来签署或验证。</pre> {| class="wikitable" |- ! 命令 !! 说明 !! 备注 |- | '''<code>git tag</code>''' | git标签 | Note: # <code>git tag</code>:查看所有标签; # <code>git tag <tagname></code>:创建标签(默认指定到当前分支); # <code>git tag <tagname> <commitID></code>:创建标签,并关联到指定分支; # <code>git tag -a <tagname></code>:创建带注解的标签(Git会打开编辑器来编辑tag的注解信息); # <code>git tag -d <tagname></code>:删除标签; # <code>git show <tagname></code>:查看tag关联的commit信息 # <code>git tag -a <tagname> -m "标签信息"</code>:指定标签信息 # <code>git tag -s <tagname> -m "标签信息"</code>:PGP签名标签 如: <syntaxhighlight lang="PowerShell"> Microsoft Windows [版本 10.0.19041.508] (c) 2019 Microsoft Corporation。保留所有权利。 D:\git\eijux>git tag D:\git\eijux>git tag rc1.0 D:\git\eijux>git tag -a rc2.0 # Write a message for tag: # rc2.0 # Lines starting with '#' will be ignored. D:\git\eijux>git tag rc3.0 ffa1 D:\git\eijux>git log --decorate commit 5e26159ad738b08b9321eba9ecaeff39c8acc42f (HEAD -> master, tag: rc2.0, tag: rc1.0, eijux/master) Author: Eijux <chen@eijux.com> Date: Fri Nov 1 01:18:47 2019 +0800 idea commit test 01<E7><82><B9>18<E5><88><86> commit ffa14211ab7088d9782de019328238dcfe09a4bc (tag: rc3.0) Merge: 3fbc79a fc32a4a Author: Eijux <chen@eijux.com> Date: Thu Oct 31 16:03:24 2019 +0800 Merge branch 'master' of github.com:Eijux/eijux D:\git\eijux>git show rc3.0 commit ffa14211ab7088d9782de019328238dcfe09a4bc (tag: rc3.0) Merge: 3fbc79a fc32a4a Author: Eijux <chen@eijux.com> Date: Thu Oct 31 16:03:24 2019 +0800 Merge branch 'master' of github.com:Eijux/eijux D:\git\eijux> </syntaxhighlight> |} == 命令辨析 == === pull 与 fetch === <pre> More precisely, git pull runs git fetch with the given parameters and calls git merge to merge the retrieved branch heads into the current branch. With --rebase, it runs git rebase instead of git merge. </pre> 在默认模式下,<code>git pull</code>是<code>git fetch</code>后跟<code>git merge FETCH_HEAD</code>的缩写。<br/> 更准确地说,<code>git pull</code>使用给定的参数运行<code>git fetch</code>,并调用<code>git merge</code>将检索到的分支头合并到当前分支中。 '''使用<code>--rebase</code>,它运行<code>git rebase</code>而不是<code>git merge</code>。'''<br/> <br/> '''fetch理解:''' * fetch 是否有下载? *: 当然有下载,拉取的更新内容保存在“'''.git\objects'''”及“'''.git\refs'''”中,只是没有合并到当前的工作区,所以没有直观的改变;毕竟merge是不会有下载操作的。 * fetch 更新的是什么内容? *# fetch 会下载远程分支<code>orign/master</code>的文件存储对象到“<code>.git\objects</code>”; *# fetch 会更新<code>FETCH_HEAD</code>文件(如<syntaxhighlight lang="xml" inline>5e26159ad738b08b9321eba9ecaeff39c8acc42f branch 'master' of github.com:Eijux/eijux</syntaxhighlight>),其中commit_ID对应“<code>.git\objects</code>”的对象; * 本地文件与分支的关系? *: <syntaxhighlight lang="xml"> .git\refs\head\ [本地分支] .git\refs\remotes\ [正在跟踪的分支] </syntaxhighlight>
返回至“
Git:基本操作
”。
导航菜单
个人工具
登录
命名空间
页面
讨论
大陆简体
已展开
已折叠
查看
阅读
查看源代码
查看历史
更多
已展开
已折叠
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
笔记
服务器
数据库
后端
前端
工具
《To do list》
日常
阅读
电影
摄影
其他
Software
Windows
WIKIOE
所有分类
所有页面
侧边栏
站点日志
工具
链入页面
相关更改
特殊页面
页面信息