Git:基本操作

来自Wikioe
Eijux讨论 | 贡献2020年9月19日 (六) 02:35的版本 →‎分支
跳到导航 跳到搜索


仓库

命令 说明
git init 在当前目录初始化Git仓库
git init <newrepo> 在指定目录(<newrepo>)初始化Git仓库
git clone <repo> 从现有Git仓库(<repo>)中拷贝项目到当前目录
git clone <repo> <directory> 从现有Git仓库(<repo>)中拷贝项目到指定目录(<directory>

修改

命令 说明 备注
git add 将工作区新增或修改的文件添加到暂存区
  1. 添加一个或多个文件到暂存区:git add [file1] [file2]
  2. 添加指定目录到暂存区(包括子目录):git add [dir]
  3. 添加当前目录下的所有文件到暂存区:git add .
git commit 提交暂存区到本地仓库
  1. 提交暂存区到本地仓库:git commit -m [message]
  2. 提交暂存区的指定文件:git commit [file1] [file2] -m [message]
  3. -a参数设置直接提交(跳过git add):git commit ([file1] [file2]) -am [message]
git status 查看仓库当前的状态,显示有变更的文件
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
  1. Changed not staged for commit:修改文件,未add
  2. Changes to be committed:修改并add,未commit
  3. Untracked files:新增文件,从未add
git diff 比较文件的不同,即暂存区和工作区的差异
  1. 查看尚未缓存(未add)的改动:git diff ([file])
  2. 查看已缓存(已add)的改动: git diff --cached ([file])(或git diff --staged ([file])
  3. 查看已缓存与未缓存所有改动:git diff HEAD
  4. 显示摘要而非整个diff:git diff --stat
  5. 查看两次提交之间的差异:git diff [first-branch]...[second-branch]
git reset 回退版本

git reset [--soft | --mixed | --hard] [HEAD]
关于参数:

  1. --mixed:(默认)重置缓存区未指定commit(工作区不变)
  2. --soft:将HEAD指向指定commit(工作区与缓存区不变)
  3. --hard:重置工作区、缓存区未指定commit

关于[HEAD]:

  1. 当前版本:HEAD^HEAD~0
  2. 上一个版本:HEAD^HEAD~1
  3. 上上一个版本:HEAD^^HEAD^2
  4. 上上上一个版本:HEAD^^^HEAD^3
    以此类推
git revert 回退版本
git mv 移动或重命名工作区文件
  1. -f强制执行:git mv -f [file] [newfile]
git rm 删除工作区文件
  1. 将文件从暂存区和工作区中删除:git rm <file>
  2. 将文件从暂存删除(工作区保留):git rm --cached <file>
  3. -r递归删除:git rm -r *
  4. -f强制删除:git rm -f <file>

分支

命令 说明 备注
git branch 分支命令
  1. git branch:列出所有本地分支
  2. git branch <branchname>:创建<branchname>分支
  3. git branch -d <branchname>:删除<branchname>分支
git checkout 分支切换
  1. git checkout <branchname>:切换到<branchname>分支
  2. git checkout -b <branchname>创建并切换到<branchname>分支
  3. git checkout -- <fileName>:“--”表示撤销工作区文件<fileName>的修改
    1. 工作区修改,但未add到暂存区:checkout重置到与版本库一致
    2. 工作区修改并add后,再次修改:checkout重置到与暂存区一致
切换分支的时候,Git会用该分支最后提交的快照替换本地工作目录的内容。
git merge 分支合并
  1. git merge hotfix:将hotfix分支合并到当前分支
  2. branchB和并到branchA:切换到branchA中执行“git merge branchB
git rebase 分支合并

冲突合并:
冲突合并涉及文件添加、移除的操作,还包括文件修改内容的合并。

远程

命令 说明 备注
git remote 操作远程仓库
  1. git remote -v:显示所有远程仓库
  2. git remote show [remote]:显示远程仓库的信息([remote]远程仓库)
    如:git remote show https://github.com/tianqixin/runoob-git-test
  3. git remote add [shortname] [url]:添加远程版本库([shortname]本地版本库,[url]远程仓库)
    如:git remote add origin git@github.com:tianqixin/runoob-git-test.git
  4. git remote rm [name]:删除远程仓库
  5. git remote rename [old_name] [new_name]:修改远程仓库名

git fetch
git merge

从远程获取代码库:git fetch [alias] [branch]
合并远程数据到当前分支:git merge [alias]/[branch]

Note:

  1. 拉取多个分支:git fetch origin master stable oldstable,从远程仓库origin拉取masterstableoldstable分支;
  2. 合并多个分支:git merge origin/master hotfix-2275 hotfix-2276 hotfix-2290,合并origin/masterorigin/stableorigin/oldstable到当前分支。

拉取远程分支,并合并到本地分支,如:

  1. git fetch origin master:从名为origin的远程上拉取名为master的分支到本地分支origin/master中;
  2. git merge origin/master:合并origin/master分支到当前分支
    origin为配置的远程仓库别名([alias])
git pull

从远程获取代码并合并本地的版本:
git pull <远程主机名> <远程分支名>:<本地分支名>

Note:

  1. 合并到当前分支,可省略“:<本地分支名>”;
  2. 等效于git fetch+git merge FETCH_HEAD

如:

  1. git pull
  2. git pull origin
  3. git pull origin master:拉取远程主机originmaster分支,并与本地当前分支合并;
  4. git pull origin master:brantest:拉取远程主机originmaster分支,并与本地的brantest分支合并;
git push

从将本地的分支版本上传到远程并合并:
git push <远程主机名> <本地分支名>:<远程分支名>

  1. 本地分支名与远程分支名相同,可省略“:<远程分支名>”;
  2. --force强制推送:git push --force <远程主机名> <本地分支名>:<远程分支名>
  3. --delete删除远程主机的分支:git push <远程主机名> --delete <远程分支名>

日志

命令 说明 示例
git log 查看版本提交历史
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)
git reflog 查看版本命令历史
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

标签

tag实际就是一个指向commit的指针,将一个有意义的tag名称与commit相关联。
git标签有两种类型:

  1. 轻量级的(lightweight):实际上它就是个指向特定提交对象的引用。
  2. 含附注的(annotated):
    实际上是存储在仓库中的一个独立对象,有自身的校验和信息,包含标签名称,电子邮件地址和日期,及标签说明,标签本身也允许使用 GNU Privacy Guard (GPG) 来签署或验证。
命令 说明 备注
git tag git标签

Note:

  1. git tag:查看所有标签;
  2. git tag <tagname>:创建标签(默认指定到当前分支);
  3. git tag <tagname> <commitID>:创建标签,并关联到指定分支;
  4. git tag -a <tagname>:创建带注解的标签(Git会打开编辑器来编辑tag的注解信息);
  5. git tag -d <tagname>:删除标签;
  6. git show <tagname>:查看tag关联的commit信息
  7. git tag -a <tagname> -m "标签信息":指定标签信息
  8. git tag -s <tagname> -m "标签信息":PGP签名标签

如:

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>