git实验室

git clone一个项目

jiqing@jiqing-System-Product-Name:/home/wwwroot/default$ sudo git clone http://106.14.59.204/jiqing/testGit.git
正克隆到 'testGit'...
Username for 'http://106.14.59.204': jiqing@caomall.net
Password for 'http://jiqing@caomall.net@106.14.59.204': 
warning: 您似乎克隆了一个空仓库。
检查连接... 完成。

git status 查看状态

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git status
位于分支 master
初始提交
未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)
	1
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

git add 添加文件

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git add ./1

git config/git commit 提交

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git config --global user.email "jiqing@caomall.net"
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git config --global user.name "jiqing"jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git commit -m "1"
[master (根提交) eb3b1d1] 1
 1 file changed, 1 insertion(+)
 create mode 100755 1

git push 推送到分支中

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git push -u origin master
Username for 'http://106.14.59.204': jiqing@caomall.net
Password for 'http://jiqing@caomall.net@106.14.59.204': 
Counting objects: 3, done.
Writing objects: 100% (3/3), 196 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://106.14.59.204/jiqing/testGit.git
 * [new branch]      master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

同时添加多个文件

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git add ./
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git commit -m "2,3"
[master 5ba06bf] 2,3
 2 files changed, 2 insertions(+)
 create mode 100644 2
 create mode 100644 3
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git push -u origin master
Username for 'http://106.14.59.204': jiqing@caomall.net
Password for 'http://jiqing@caomall.net@106.14.59.204': 
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 296 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To http://106.14.59.204/jiqing/testGit.git
   eb3b1d1..5ba06bf  master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

创建分支,切换分支

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git branch dev
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git checkout dev
切换到分支 'dev'
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git status
位于分支 dev
无文件要提交,干净的工作区

git push origin dev将分支推送到远端

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git push origin dev
Username for 'http://106.14.59.204': jiqing
Password for 'http://jiqing@106.14.59.204': 
Total 0 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for dev, visit:
remote:   http://106.14.59.204/jiqing/testGit/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote: 
To http://106.14.59.204/jiqing/testGit.git
 * [new branch]      dev -> dev

在分支中添加代码

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git add ./4
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git commit -m "4"
[dev b735b44] 4
 1 file changed, 1 insertion(+)
 create mode 100644 4
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git status
位于分支 dev
无文件要提交,干净的工作区
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git push -u origin dev
Username for 'http://106.14.59.204': jiqing
Password for 'http://jiqing@106.14.59.204': 
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 251 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: 
remote: To create a merge request for dev, visit:
remote:   http://106.14.59.204/jiqing/testGit/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote: 
To http://106.14.59.204/jiqing/testGit.git
   5ba06bf..b735b44  dev -> dev
分支 dev 设置为跟踪来自 origin 的远程分支 dev。

这个时候dev上有4这个文件,而master上面没有。

删除本地分支

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git checkout dev
切换到分支 'dev'
您的分支与上游分支 'origin/dev' 一致。
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git branch -D devJi
已删除分支 devJi(曾为 b735b44)。

删除线上分支

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git push origin --delete devJi
Username for 'http://106.14.59.204': jiqing
Password for 'http://jiqing@106.14.59.204': 
To http://106.14.59.204/jiqing/testGit.git
 - [deleted]         devJi

从分支上合并代码

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git checkout master 
切换到分支 'master'
您的分支与上游分支 'origin/master' 一致。
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git merge devyuan
更新 5ba06bf..269885a
Fast-forward
 5 | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 5
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ ll
总用量 28
drwxrwxrwx 3 root   root   4096  4月  3 19:23 ./
drwxr-xr-x 7 www    www    4096  4月  3 16:57 ../
-rwxrwxrwx 1 root   root     20  4月  3 15:53 1*
-rw-rw-r-- 1 jiqing jiqing   14  4月  3 15:59 2
-rw-rw-r-- 1 jiqing jiqing   20  4月  3 16:07 3
-rw-rw-r-- 1 jiqing jiqing    7  4月  3 19:23 5
drwxrwxrwx 8 root   root   4096  4月  3 19:23 .git/

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git push -u origin master
Username for 'http://106.14.59.204': jiqing
Password for 'http://jiqing@106.14.59.204': 
Total 0 (delta 0), reused 0 (delta 0)
To http://106.14.59.204/jiqing/testGit.git
   5ba06bf..269885a  master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

强制更新和强制推送

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git pull origin master:devji
Username for 'http://106.14.59.204': jiqing
Password for 'http://jiqing@106.14.59.204': 
来自 http://106.14.59.204/jiqing/testGit
 ! [已拒绝]          master     -> devji  (非快进式)
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ ll
总用量 28
drwxrwxrwx 3 root   root   4096  4月  3 19:26 ./
drwxr-xr-x 7 www    www    4096  4月  3 16:57 ../
-rwxrwxrwx 1 root   root     20  4月  3 15:53 1*
-rw-rw-r-- 1 jiqing jiqing   14  4月  3 15:59 2
-rw-rw-r-- 1 jiqing jiqing   20  4月  3 16:07 3
-rw-rw-r-- 1 jiqing jiqing   37  4月  3 19:26 4
drwxrwxrwx 8 root   root   4096  4月  3 19:32 .git/
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git pull origin master:devji -f
Username for 'http://106.14.59.204': jiqing
Password for 'http://jiqing@106.14.59.204': 
来自 http://106.14.59.204/jiqing/testGit
 + b735b44...269885a master     -> devji  (强制更新)
警告:fetch 更新了当前的分支。您的工作区
警告:从原提交 b735b444332bede7e6dcc828ab5dd7e3dd3029e4 快进。
Already up-to-date.
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ ll
总用量 28
drwxrwxrwx 3 root   root   4096  4月  3 19:33 ./
drwxr-xr-x 7 www    www    4096  4月  3 16:57 ../
-rwxrwxrwx 1 root   root     20  4月  3 15:53 1*
-rw-rw-r-- 1 jiqing jiqing   14  4月  3 15:59 2
-rw-rw-r-- 1 jiqing jiqing   20  4月  3 16:07 3
-rw-rw-r-- 1 jiqing jiqing    7  4月  3 19:33 5
drwxrwxrwx 8 root   root   4096  4月  3 19:33 .git/
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git push -u origin devji
Username for 'http://106.14.59.204': jiqing
Password for 'http://jiqing@106.14.59.204': 
To http://106.14.59.204/jiqing/testGit.git
 ! [rejected]        devji -> devji (non-fast-forward)
error: 无法推送一些引用到 'http://106.14.59.204/jiqing/testGit.git'
提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支。
提示:再次推送前,先与远程变更合并(如 'git pull ...')。详见
提示:'git push --help' 中的 'Note about fast-forwards' 小节。
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git push -u origin devji -f
Username for 'http://106.14.59.204': jiqing
Password for 'http://jiqing@106.14.59.204': 
Total 0 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for devji, visit:
remote:   http://106.14.59.204/jiqing/testGit/merge_requests/new?merge_request%5Bsource_branch%5D=devji
remote: 
To http://106.14.59.204/jiqing/testGit.git
 + b735b44...269885a devji -> devji (forced update)
分支 devji 设置为跟踪来自 origin 的远程分支 devji。

如果两个人同时操作一个分支,这个就有点像svn了。先拉取,再提交。

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/test/testGit$ git add ./6
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/test/testGit$ git commit -m "6 from test/testGit"
[dev ae59a51] 6 from test/testGit
 1 file changed, 1 insertion(+)
 create mode 100644 6
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/test/testGit$ git push -u origin dev
Username for 'http://106.14.59.204': jiqing
Password for 'http://jiqing@106.14.59.204': 
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 265 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: 
remote: To create a merge request for dev, visit:
remote:   http://106.14.59.204/jiqing/testGit/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote: 
To http://106.14.59.204/jiqing/testGit.git
   b735b44..ae59a51  dev -> dev
分支 dev 设置为跟踪来自 origin 的远程分支 dev。

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git pull origin dev
Username for 'http://106.14.59.204': jiqing
Password for 'http://jiqing@106.14.59.204': 
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
来自 http://106.14.59.204/jiqing/testGit
 * branch            dev        -> FETCH_HEAD
   b735b44..ae59a51  dev        -> origin/dev
更新 b735b44..ae59a51
Fast-forward
 6 | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 6
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ ll
总用量 32
drwxrwxrwx 3 root   root   4096  4月  4 09:15 ./
drwxr-xr-x 7 www    www    4096  4月  3 16:57 ../
-rwxrwxrwx 1 root   root     20  4月  3 15:53 1*
-rw-rw-r-- 1 jiqing jiqing   14  4月  3 15:59 2
-rw-rw-r-- 1 jiqing jiqing   20  4月  3 16:07 3
-rw-rw-r-- 1 jiqing jiqing   37  4月  4 09:10 4
-rw-rw-r-- 1 jiqing jiqing   49  4月  4 09:15 6
drwxrwxrwx 8 root   root   4096  4月  4 09:15 .git/
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git status
位于分支 dev
您的分支与上游分支 'origin/dev' 一致。

无文件要提交,干净的工作区

比较本地与线上的差别

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git fetch origin
Username for 'http://106.14.59.204': jiqing
Password for 'http://jiqing@106.14.59.204': 
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
来自 http://106.14.59.204/jiqing/testGit
   25016f9..03dcb0d  dev        -> origin/dev
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/testGit$ git diff dev origin/dev
diff --git a/8 b/8
new file mode 100644
index 0000000..901184c
--- /dev/null
+++ b/8
@@ -0,0 +1 @@
+88888888

git stash ,git stash pop

jiqing@ubuntu:/home/wwwroot/default/siemens/new_hotel$ git stash
Saved working directory and index state WIP on siemens: ba1cc89 修改
HEAD 现在位于 ba1cc89 修改
jiqing@ubuntu:/home/wwwroot/default/siemens/new_hotel$ git pull origin siemens 
Username for 'http://106.14.59.204': jiqing
Password for 'http://jiqing@106.14.59.204': 
来自 http://106.14.59.204/daijiawei/new_hotel
 * branch            siemens    -> FETCH_HEAD
Already up-to-date.
jiqing@ubuntu:/home/wwwroot/default/siemens/new_hotel$ git stash pop
位于分支 siemens
您的分支与上游分支 'origin/siemens' 一致。

尚未暂存以备提交的变更:
  (使用 "git add <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	修改:         Index/Lib/Action/SiemensAction.class.php

未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	Index/Tpl/Siemens/share.html

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
丢弃了 refs/stash@{0} (5841615b40ac0332cf4dc3c3a3499e58a57c73ad)
jiqing@ubuntu:/home/wwwroot/default/siemens/new_hotel$ git add ./*
jiqing@ubuntu:/home/wwwroot/default/siemens/new_hotel$ git commit -m "提交分享页面"
[siemens 0b3a6db] 提交分享页面
 2 files changed, 15 insertions(+)
 create mode 100644 Index/Tpl/Siemens/share.html
jiqing@ubuntu:/home/wwwroot/default/siemens/new_hotel$ git push origin siemens
Username for 'http://106.14.59.204': jiqing
Password for 'http://jiqing@106.14.59.204': 
Counting objects: 21, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (9/9), 821 bytes | 0 bytes/s, done.
Total 9 (delta 4), reused 0 (delta 0)
remote: Checking connectivity: 9, done.
remote: 
remote: To create a merge request for siemens, visit:
remote:   http://106.14.59.204/daijiawei/new_hotel/merge_requests/new?merge_request%5Bsource_branch%5D=siemens
remote: 
To http://106.14.59.204/daijiawei/new_hotel.git
   ba1cc89..0b3a6db  siemens -> siemens


查看线上和本地所有分支git branch -a

jiqing@ubuntu:/home/wwwroot/default/5hao/lion$ git branch -a
* 5hao
  master
  remotes/origin/5hao
  remotes/origin/HEAD -> origin/master
  remotes/origin/develop
  remotes/origin/giftcard
  remotes/origin/master
  remotes/origin/ssy
jiqing@ubuntu:/home/wwwroot/default/5hao/lion$ git branch
* 5hao
  master

切换分支,从分支上拉数据

jiqing@jiqing-System-Product-Name:/home/wwwroot/default/5hao/mouse$ git status
位于分支 5hao
无文件要提交,干净的工作区
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/5hao/mouse$ git branch
* 5hao
  master
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/5hao/mouse$ git checkout -b 5hao
fatal: 一个分支名 '5hao' 已经存在。
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/5hao/mouse$ git pull origin 5hao 
Username for 'http://106.14.59.204': jiqing
Password for 'http://jiqing@106.14.59.204': 
来自 http://106.14.59.204/kala/mouse
 * branch            5hao       -> FETCH_HEAD
Already up-to-date.
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/5hao/mouse$ git checkout master
切换到分支 'master'
您的分支与上游分支 'origin/master' 一致。
jiqing@jiqing-System-Product-Name:/home/wwwroot/default/5hao/mouse$ git checkout 5hao
切换到分支 '5hao'

分支未合并完成处理

[root@iZuf6bmpnhekewcpbaogo9Z new_hotel]# git pull origin siemens
您尚未结束您的合并(存在 MERGE_HEAD)。
请在合并前先提交您的修改。
[root@iZuf6bmpnhekewcpbaogo9Z new_hotel]# rm -rf .git/MERGE*
[root@iZuf6bmpnhekewcpbaogo9Z new_hotel]# git pull origin siemens
来自 http://106.14.59.204/daijiawei/new_hotel
 * branch            siemens    -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 Index/Lib/Action/GetDataAction.class.php | 2 ++
 1 file changed, 2 insertions(+)

原文地址:https://www.cnblogs.com/jiqing9006/p/8710677.html