对了,这里要提一下,Git 和 Linux 是同一个爸爸,所以 Linux 系统上能跑的原生指令 Git 上面也都可以,反之也一样。暂存区。进仓库前暂时存放区域,未对本地仓库生效。对暂存区文件修改,显蓝色。第一次创建并add到暂存区的文件,由于远程仓库没同步,显绿色。(注:存放在 “.git/index目录” 。)本地仓库。暂存区commit代码放入本地仓库,通过 push 推送到远程仓库。(注:存在“.git”目录。)远程仓库。在前面的话,仓库里都有些什么东西只有你自己知道,但是一旦把本地仓库推到远程仓库之后,仓库里有什么东西大家就都知道了。开头两个命令是用来设置用户签名的,这个一般就用一次就完事儿了,因为你要提交代码的时候是需要责任跟踪的。这个 name,email 啥的,人家也不会去认证,不过组里面肯定是会有规范的,也不会让你想起啥名就随便起吧。新手一般就老老实实 git init 起手嘛,昨天我也是这么问我导师的,他说不用,直接 git clone 就好了。我想想也是,团队开发嘛。$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use 'git add' to track)
当我们新增了一个文件之后,再查看状态, 会检测到未追踪的文件:$ git status
On branch master
No commits yet
Untracked files:
(use 'git add <file>...' to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use 'git add'
to track)
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working
directory.
$ git status
On branch master
No commits yet
Changes to be committed:
(use 'git rm --cached <file>...' to unstage)
new file: hello.txt
$ git commit -m 'my first commit' hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working
directory.
[master (root-commit) 86366fa] my first commit
1 file changed, 16 insertions(+)
create mode 100644 hello.txt
$ git status
On branch master
nothing to commit, working tree clean
$ git status
On branch master
Changes not staged for commit:
(use 'git add <file>...' to update what will be committed)
(use 'git checkout -- <file>...' to discard changes in working
directory)
modified: hello.txt
no changes added to commit (use 'git add' and/or 'git commit -a')
那么接下来就需要再次将修改的文件提交到暂存区,然后提交到本地仓库。git reflog 查看版本信息
git log 查看版本详细信息
如果是想快速浏览版本信息,可以使用 reflog 即可,毕竟公司项目版本迭代信息可能已经很多了,想看详细日志也得看你有没有那个精力了。$ git reflog
087a1a7 (HEAD -> master) HEAD@{0}: commit: my third commit
ca8ded6 HEAD@{1}: commit: my second commit
86366fa HEAD@{2}: commit (initial): my first commit
$ git reflog
087a1a7 (HEAD -> master) HEAD@{0}: commit: my third commit
ca8ded6 HEAD@{1}: commit: my second commit
86366fa HEAD@{2}: commit (initial): my first commit
--切换到 86366fa 版本,也就是我们第一次提交的版本
$ git reset --hard 86366fa
HEAD is now at 86366fa my first commit
--切换完毕之后再查看历史记录,当前成功切换到了 86366fa 版本
$ git reflog
86366fa (HEAD -> master) HEAD@{0}: reset: moving to 86366fa
087a1a7 HEAD@{1}: commit: my third commit
ca8ded6 HEAD@{2}: commit: my second commit
86366fa (HEAD -> master) HEAD@{3}: commit (initial): my first commit
--然后查看文件 hello.txt,发现文件内容已经变化
Git 切换版本,底层其实是移动的 HEAD 指针。在版本控制过程中,同时推进多个任务,为每个任务,我们就可以创建每个任务的单独分支。使用分支意味着程序员可以把自己的工作从开发主线上分离开来,开发自己分支的时候,不会影响主线分支的运行。对于初学者而言,分支可以简单理解为副本,一个分支就是一个单独的副本。(分支底层其实也是指针的引用。)小伙子,不要一上来就想着创建分支嘛。先看看前辈们已经达到了哪个高度了嘛。$ git branch -v
* master 087a1a7 my third commit (*代表当前所在的分区)
$ git branch hot-fix
$ git branch -v
hot-fix 087a1a7 my third commit (刚创建的新的分支,并将主分支 master
的内容复制了一份)
* master 087a1a7 my third commit
接下来就可以在分支上进行开发了,开发完之后记得将代码提交到本地仓库。$ git checkout hot-fix
Switched to branch 'hot-fix'
--发现当先分支已由 master 改为 hot-fix
这里有个要注意的点:不仅是可以把旁支合并到主支上,主支也可以被合并到分支上,分支之间也可以互相合并。反正你爱怎么玩怎么玩,都是在你本地仓库上,又没有推送到远程仓库。这样就万事大吉了吗?不一定的。脑子清醒的话很容易想到一个问题:主支修改了一个文件,分支也修改了同一个文件,现在以谁的为准?脑子更清醒的人就要说了:你傻呀,我 clone 下来就切到分支去操作了,在我本地仓库我又不会去动主支的。脑子再清醒一点的人已经懒的跟他说了:大哥,咱这是团队开发,咱的本地仓库早晚是都要合并到主线上的。hello git! hello atguigu!
<<<<<<< HEAD
hello git! hello atguigu! master test
hello git! hello atguigu!
=======
hello git! hello atguigu!
hello git! hello atguigu! hot-fix test
>>>>>>> hot-fix
HEAD 到 === 的部分是主支的,后面是 hot-fix 的合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。Git 无法替我们决定使用哪一个。必须人为决定新代码内容。1、删掉冲突中不要的部分,留下最终的部分
2、git add 添加到暂存区
3、 git commit -m 注意,此时的 commit 后面不能带文件名。带文件名是几个意思呢?到底用哪个呢?
然后就会发现合并成功了。
这里不说 github,因为还有 gitlab,公司内部代码怎么能放到广域网上呢!!!为什么要别名呢?其实起不起别名都无所谓,主要是有个别名方便操作。你说是记一个网址容易还是记一个自己起的名字容易嘞?$ git remote -v
$ git remote add ori https://github.com/xxx.git
$ git remote -v
ori https://github.com/xxx.git (fetch)
ori https://github.com/xxx.git (push)
当文件还在本地仓库的时候,咱怎么玩都行。但是一旦推送上去了,那就产生历史版本了,大家都看得到了。不过这个推送啊,也不是咱想推送就能推送的,还得管理员给咱授权,这个不难理解吧。$ git push ori master
Logon failed, use ctrl+c to cancel basic credential prompt.
Username for 'https://github.com': atguiguyueyue
Counting objects: 3, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes | 276.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/atguiguyueyue/git-shTest.git
* [new branch] master -> master
这不用说吧,这个很简单。咱虽然说之前不会推送,但是拉取还是没有问题的。会创建别名,你 clone 之后用 git remote -v 看一下,一般是 origin。这个有两个办法,昨天我用的是先 clone,在 checkout 的方式选择我要的分支。$ git pull ori master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/atguiguyueyue/git-shTest
* branch master -> FETCH_HEAD
7cb4d02..5dabe6b master -> ori/master
Updating 7cb4d02..5dabe6b
Fast-forward
hello.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧ END ‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧