git深入理解(八):其他操作

gitlab包含本地仓克和远程仓库,例如 github,gitee,也可以自己搭建gitlab服务作为公司代码库。

git库所在的文件夹中的文件大致有4种状态
Untracked

未跟踪,此文件在文件夹中,但并没有加入到git库,不参与版本控制。通过 git add 将状态变为 Staged。

Modified

文件已修改,仅仅是修改,并没有进行其他的操作。这个文件也有两个去处,通过
git add filename 可进入暂存 Staged 状态,
git checkout – filename 则丢弃修改,返回到 Unmodify 状态,也就是从库中取出文件,覆盖当前修改。

Staged

暂存状态。
可以执行 git reset HEAD filename 取消暂存,文件状态为 Untracked 状态。
可以执行 git commit 将修改同步到库中,这时库中的文件和本地文件又变为一致,文件为 Unmodify 状态。

Unmodify

文件已经入库,未修改,即版本库中的文件快照内容与文件夹中完全一致,这种类型的文件有两种去处。
如果它被修改而变为 Modified;
可以使用 git rm filename 移出版本库,并删除文件,但是还没提交!!
可以使用 git rm --cached filename 移出版本库,成为 Untracked,但是还没提交!!

Git 状态 untracked 和 not staged 的区别
1)untrack 表示是新文件,没有被add过,是未跟踪的意思。
2)not staged 表示add过的文件,即跟踪文件,再次修改没有add,就是没有暂存的意思。

有四个阶段

working directory --> index(Stage) --> local repository --> remote repository

查看帮助

git --help

usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introduced a bug
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout    Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Reapply commits on top of another base tip
   tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository or a local branch
   push       Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.

查看某个命令的帮助

git help init 会弹出网页。
git init -h 打印帮助信息。
初始化本地仓库
进入要初始化的目录。
git init

git clone
clone下来的repo会以url最后一个斜线后面的名称命名,创建一个文件夹,如果想要指定特定的名称,可以 git clone url newname 指定。

当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin。
clone 下来的项目包含完整 .git 目录,所以你能查看完整的版本信息。

下载指定分支的代码,有的仓库分支很多,但是只想要某个分支的。
git clone -b release-branch.go1.15 https://github.com/phprao/go.git
提交缓冲区的所有修改到本地仓库

git commit -m “提交的说明”
git commit -a 会先把所有已经track的文件的改动add进来,然后提交(有点像svn的一次提交,不用先暂存),对于没有track的文件,还是需要git add一下。

查看仓库的操作历史

git reflog

为本地仓库创建tag

git tag v1.0
会在当前分支的最新提交上建立永久性的书签,通常是发布一个release版本或者ship了什么东西之后加tag。

git tag -a v1.0 -m "some comment"
-a 参数会允许你添加一些信息,-m 设置备注

git tag v0.9 commitID
为指定的commit版本号打tag

git tag
查看标签列表,按字母顺序列出的,不是按时间

git show tagname
查看标签详情

git tag -d v0.1
删除本地标签

如果标签已经推送到远程,要删除远程标签就麻烦一点,先从本地删除,然后,从远程删除。删除命令也是push,但是格式如下git push origin :refs/tags/v0.9要看看是否真的从远程库删除了标签,可以登陆GitHub查看

标签总是和某个commit挂钩。如果这个commit既出现在master分支,又出现在dev分支,那么在这两个分支上都可以看到这个标签

push的时候是不包含tag的,如果想包含,可以在push时加上--tags参数。

如果要推送某个标签到远程,使用命令git push origin tagname

fetch的时候,branch HEAD可以reach的tags是自动被fetch下来的, tags that aren’t reachable from branch heads will be skipped.如果想确保所有的tags都被包含进来,需要加上–tags选项

git remote

list, add and delete remote repository aliases。
因为不需要每次都用完整的url,所以Git为每一个remote repo的url都建立一个别名,然后用git remote来管理这个list。

git remote
列出remote aliases。
如果你clone一个project,Git会自动将原来的url添加进来,别名就叫做:origin。

git remote -v
可以看见每一个别名对应的实际url

$ git remote -v
origin  http://219.140.188.146:52133/test.git (fetch)
origin  http://219.140.188.146:52133/test.git (push)

上面显示了可以抓取和推送的origin的地址。如果没有推送权限,就看不到push的地址。

git remote add [alias] [url]
添加一个新的remote repo,一般一个本地仓库对应一个远程仓库,并且使用 origin 作为名称,即
git remote add origin url

如果别名alias已经存在了则会报错,可以先删除再来加,也可以直接修改url。
远程仓库是存储在 .git/refs/remotes 目录下。

git remote rm [alias]
删除一个存在的remote alias。

git remote rename [old-alias] [new-alias]
重命名

git remote set-url [alias] [url]
更新url. 可以加上—push和fetch参数,为同一个别名set不同的存取地址

git fetch

download new branches and data from a remote repository.

拉取指定远程仓库上的指定分支的代码,比如 origin master。

首先会对比本地的 .git/refs/remotes/origin/master 文件中保存的 commitID 和线上的 master 分支的最新的 commitID 对比,如果不一致,则将这些文件下载下来。下载下来的文件依然放在 .git/objects 下面,然后更新 .git/refs/remotes/origin/master 文件到最新的 commitID。

可以git fetch [alias]取某一个远程repo,也可以git fetch --all取到全部repo。
它们和本地分支一样(可以看diff,log等,也可以merge到其他分支),但是Git不允许你checkout到它们。

git pull

fetch from a remote repo and try to merge into the current branch.

pull == fetch + merge FETCH_HEAD。

git pull会首先执行git fetch,然后执行git merge,把取来的分支的head merge到当前分支.这个merge操作会产生一个新的commit。
如果使用–rebase参数,它会执行git rebase来取代原来的git merge。

git push

push your new branches and data to a remote repository.

git push [alias] [branch]
将会把当前分支merge到alias上的[branch]分支.如果分支已经存在,将会更新,如果不存在,将会添加这个分支.
如果有多个人向同一个remote repo push代码, Git会首先在你试图push的分支上运行git log,检查它的历史中是否能看到server上的branch现在的tip,如果本地历史中不能看到server的tip,说明本地的代码不是最新的,Git会拒绝你的push,让你先fetch,merge,之后再push,这样就保证了所有人的改动都会被考虑进来。

第一次push的时候一般加上 -u 参数,这样push成功后,Git会在你本地将当前提交的分支和 [alias] [branch] 做一个绑定,这样以后的 push, pull, status 操作就默认指向了 [alias] [branch],而不用每次都去指定。

git branch

git branch可以用来列出分支,创建分支和删除分支.
git branch -v可以看见每一个分支的最后一次提交.
git branch: 列出本地所有分支,当前分支会被星号标示出.
git branch (branchname): 创建一个新的分支(当你用这种方式创建分支的时候,分支是基于你的上一次提交建立的).
git branch -d (branchname): 删除一个分支.
删除remote的分支:
git push (remote-name) :(branch-name): delete a remote branch.
这个是因为完整的命令形式是:
git push remote-name local-branch:remote-branch
而这里local-branch的部分为空,就意味着删除了remote-branch

git merge

把一个分支merge进当前的分支.
git merge [alias] [branch]

如果出现冲突,需要手动修改,可以用git mergetool.
解决冲突的时候可以用到git diff,解决完之后用git add添加,即表示冲突已经被resolved

git rebase

把本地未push的分叉提交历史整理成直线。
rebase的目的是使得我们在查看历史提交的变化时更容易,因为分叉的提交需要三方对比。
https://www.liaoxuefeng.com/wiki/896043488029600/1216289527823648

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值