git 两个用户实现在服务器上的交互

这篇博客详细介绍了如何在服务器上设置Git仓库,并演示了两个用户如何交互操作同一项目。用户1创建并初始化项目,用户2克隆并进行编辑,通过建立追踪关系和执行`git pull`和`git push`命令,实现代码的同步更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

服务器端
➜  ~  mkdir share.git
➜  ~  share.git
➜  share.git  git init --bare
Initialized empty Git repository in /Users/qingyun/share.git/
➜  share.git git:(master)

用户1
➜  1222   mkdir Share1
➜  1222  Share1
➜  Share1  git clone ~/share.git    将服务器上的裸库克隆过来
Cloning into 'share'...
warning: You appear to have cloned an empty repository.
done.
➜  Share1  ls
share
➜  Share1  share
➜  share git:(master) echo "main”>main.c  创建文件main.c
➜  share git:(master) ✗ mkdir resource  创建文件夹
➜  share git:(master) ✗ ls
main.c   resource
➜  share git:(master) ✗ git add *
➜  share git:(master) ✗ git ci -a -m"user1启动项目"
[master (root-commit) dc83633] user1启动项目
 1 file changed, 1 insertion(+)
 create mode 100644 main.c
➜  share git:(master) git push origin
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 3, done.
Writing objects: 100% (3/3), 226 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /Users/qingyun/share.git
 * [new branch]      master -> master
➜  share git:(master) cd resource
➜  resource git:(master) echo "student">student.c
➜  resource git:(master) ✗ git add *
➜  resource git:(master) ✗ git ci -a -m"create student.c"
[master d4502ad] create student.c
 1 file changed, 1 insertion(+)
 create mode 100644 resource/student.c
➜  resource git:(master) git push origin master:master
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 325 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To /Users/qingyun/share.git
   dc83633..d4502ad  master -> master
➜  resource git:(master) git br test
➜  resource git:(master) git co test
Switched to branch 'test'
➜  resource git:(test) cd ..
➜  share git:(test) echo "int main">>main.c
➜  share git:(test) ✗ git add *
➜  share git:(test) ✗ git commit -a -m"edit main at test by user1"
[test d34fe17] edit main at test by user1
 1 file changed, 1 insertion(+)
➜  share git:(test) git push origin test:test
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 288 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /Users/qingyun/share.git
 * [new branch]      test -> test
➜  share git:(test) git co master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
➜  share git:(master) fit br -a
zsh: command not found: fit
➜  share git:(master) git br -a
* master
  test
  remotes/origin/master
  remotes/origin/test
➜  share git:(master) ls
main.c   resource
➜  share git:(master) git getch
git: 'getch' is not a git command. See 'git --help'.

Did you mean this?
    fetch
➜  share git:(master) git fetch
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 8 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (8/8), done.
From /Users/qingyun/share
   d34fe17..2edf846  test       -> origin/test
➜  share git:(master) git co test
Switched to branch 'test'
➜  share git:(test) git fetch
➜  share git:(test) ls
main.c   resource
➜  share git:(test) git merge origin/test
Updating d34fe17..2edf846
Fast-forward
 docs/readme.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 docs/readme.md
➜  share git:(test) ls
docs     main.c   resource
➜  share git:(test)  git pull origin
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.
From /Users/qingyun/share
   2edf846..378f603  test       -> origin/test
You asked to pull from the remote 'origin', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.
➜  share git:(test) ls
docs     main.c   resource
➜  share git:(test) git merge origin/test
Updating 2edf846..378f603
Fast-forward
 study.c | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 study.c
➜  share git:(test) git branch --set-upstream-to origin/test   用户1的分支test与服务器端的test建立追踪关系
Branch test set up to track remote branch test from origin.
➜  share git:(test) ls
docs     main.c   resource study.c
➜  share git:(test) cat sutdy
cat: sutdy: No such file or directory
➜  share git:(test) cat sutdy.c
cat: sutdy.c: No such file or directory
➜  share git:(test) cat study.c
tingyuxuan
➜  share git:(test) git oull origin
git: 'oull' is not a git command. See 'git --help'.

Did you mean this?
    pull
➜  share git:(test) git pull origin
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.
From /Users/qingyun/share
   378f603..4d04938  test       -> origin/test
Updating 378f603..4d04938
Fast-forward
 study.c | 1 +
 1 file changed, 1 insertion(+)
➜  share git:(test) ls
docs     main.c   resource study.c
➜  share git:(test) cat study
cat: study: No such file or directory
➜  share git:(test) cat study.c
tingyuxuan
studytoo
➜  share git:(test)

用户2
➜  1222  Share1
➜  Share1  mkdir Share2
  1 readme...............
➜  Share1  Share2
➜  Share2  git clone ~/share.git
Cloning into 'share'...
done.
➜  Share2  cd share
➜  share git:(master) ls
main.c   resource
➜  share git:(master) cat main.c
main
➜  share git:(master) git br test
➜  share git:(master) git br
* master
  test
➜  share git:(master) git br -a
* master
  test
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/test
➜  share git:(master) git checkout
Your branch is up-to-date with 'origin/master'.
➜  share git:(master) git checkout test
Switched to branch 'test'
➜  share git:(test) git br
  master
* test
➜  share git:(test) git merge origin/test
Updating d4502ad..d34fe17
Fast-forward
 main.c | 1 +
 1 file changed, 1 insertion(+)
➜  share git:(test) ls
main.c   resource
➜  share git:(test) cat main.c
main
int main
➜  share git:(test) mkdir docs
➜  share git:(test) cd docs
➜  docs git:(test) touch readme.md
➜  docs git:(test) ✗ cd ..
➜  share git:(test) ✗ git add *
➜  share git:(test) ✗ git ci -m"create docs by user2"
[test 8bf144c] create docs by user2
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 docs/readme.md
➜  share git:(test) git push origin test:test
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 351 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To /Users/qingyun/share.git
   d34fe17..8bf144c  test -> test
➜  share git:(test) vi docs/readme.md
➜  share git:(test) ✗ git ci -a -m "read...........by user2"
[test 2edf846] read...........by user2
 1 file changed, 1 insertion(+)
➜  share git:(test) ✗ git branch --set-upstream test origin/test 用户2 的test分支和服务器上的分支test建立追踪关系
The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
Branch test set up to track remote branch test from origin.
➜  share git:(test) ✗ git branch --set-upstream-to origin/test
Branch test set up to track remote branch test from origin.
➜  share git:(test) ✗ git push origin
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 360 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To /Users/qingyun/share.git
   8bf144c..2edf846  test -> test
➜  share git:(test) ✗ ls
docs     main.c   resource
➜  share git:(test) ✗ git st
On branch test
Your branch is up-to-date with 'origin/test'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    docs/readme.md~

nothing added to commit but untracked files present (use "git add" to track)
➜  share git:(test) ✗ rm -f docs/readme.md~
➜  share git:(test) ls
docs     main.c   resource
➜  share git:(test)
➜  share git:(test) ls
docs     main.c   resource
➜  share git:(test) echo "tingyuxuan">study.c
➜  share git:(test) ✗ git add *
➜  share git:(test) ✗ git ci -m"create study.c at user2"
[test 378f603] create study.c at user2
 1 file changed, 1 insertion(+)
 create mode 100644 study.c
➜  share git:(test) git push origin
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 278 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To /Users/qingyun/share.git
   2edf846..378f603  test -> test
➜  share git:(test) echo "studytoo">> study.c
➜  share git:(test) ✗ git ci -a -m"edit study.c at user2"
[test 4d04938] edit study.c at user2
 1 file changed, 1 insertion(+)
➜  share git:(test) git push origin
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 273 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To /Users/qingyun/share.git
   378f603..4d04938  test -> test
➜  share git:(test)


当用户1,用户2,和服务器上对应的分支建立追踪关系的时候就方便以后两个用户的使用
比如用户1 在对应的分支上修改了文件之后执行 git push origin命令(向服务器直接推送)之后
用户2 在对应的分支上执行git pull origin 命令就可以直接看到对应文件修改之后的结果 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值