git同步

当同步本地仓库和远程仓库时,输入命令git push可能会发生下列报错:

1
2
3
4
5
6
7
! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/xxxx/xxxx.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.``

原因:远程仓库与本地仓库文件不一致造成的

因此下面阐述三种不同情况下的同步方式。

  1. 远程仓库发生改变,本地仓库没有改变

  2. 远程仓库没有改变,本地仓库发生改变

  3. 远程仓库,本地仓库都发生改变

这三种方式的情况下,如何做到没有Bug的合并代码。。。

远程仓库发生改变,本地仓库没有改变

  • 首先,查看远程仓库git remote -v

    1
    2
    3
    4
    ┌─[root@sun] - [/home/CSAPP] - [Thu Jan 20, 10:30]
    └─[$]> git remote -v
    origin git@github.com:tiketiskte/CSAPP.git (fetch)
    origin git@github.com:tiketiskte/CSAPP.git (push)
  • 把远程库更新到本地 git fetch origin master

    1
    2
    3
    4
    ┌─[root@sun] - [/home/CSAPP] - [Thu Jan 20, 10:31]
    └─[$]> git fetch origin master
    From github.com:tiketiskte/CSAPP
    * branch master -> FETCH_HEAD
  • 比较远程更新和本地版本库的差异 git log master.. origin/master

    1
    2
    3
    4
    5
    6
    $ git log master.. origin/master
    commit ce39f8b3eeee898a2a038444f897f2aef3673493
    Author: {User} <794870409@qq.com>
    Date: Fri Feb 26 14:14:39 2016 +0800

    {The context origin added ... }
  • 合并远程库 git merge origin/master

    • 有差异

      1
      2
      3
      4
      5
      6
      ┌─[root@sun] - [/home/CSAPP] - [Thu Jan 20, 10:35]
      └─[$]> git merge origin/master
      Updating eb32b20..ce39f8b
      Fast-forward
      README.md | 2 +-
      1 file changed, 1 insertion(+), 1 deletion(-)
    • 无差异

      1
      2
      3
      ┌─[root@sun] - [/home/CSAPP] - [Thu Jan 20, 10:36]
      └─[$]> git merge origin/master
      Already up-to-date.

远程仓库没有改变,本地仓库发生改变

常用命令

1
2
3
4
git status // 查看版本库的状态
git add .|[file you want add like README.md] // 添加修改的文件进入版本库
git commit -m "the content of your modify" // 提交版本库
git push -u origin master // 上传到远程版本库
  • 首先你的文件出现了修改 git status

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    ┌─[root@sun] - [/home/CSAPP] - [Thu Jan 20, 10:45]
    └─[$]> git status
    # On branch master
    # Your branch is ahead of 'origin/master' by 1 commit.
    # (use "git push" to publish your local commits)
    #
    # 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: datalab-handout/bits.c
    #
    no changes added to commit (use "git add" and/or "git commit -a")

你的文件被修改后就会出现这样的显示

  • 添加文件 git add .|[或者某个文件]|like: README.md

    1
    2
    3
    git add .

    git add xxx

然后查看状态git status

1
2
3
4
5
6
7
8
9
10
11
┌─[root@sun] - [/home/CSAPP] - [Thu Jan 20, 10:54]
└─[$]> git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
# (use "git push" to publish your local commits)
#
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: datalab-handout/bits.c
#

文件添加后,就会出现这种

  • 提交文件git commit -m "content"

    1
    2
    3
    4
    ┌─[root@sun] - [/home/CSAPP] - [Thu Jan 20, 10:56]
    └─[$]> git commit -m "update content"
    [master b9a152e] update content
    1 file changed, 1 insertion(+)

文件提交到你的版本库,这样本地版本就更新了

  • 和远程库进行同步 git push -u origin master

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ┌─[root@sun] - [/home/CSAPP] - [Thu Jan 20, 11:06]
    └─[$]> git push -u origin master
    Counting objects: 7, done.
    Compressing objects: 100% (4/4), done.
    Writing objects: 100% (4/4), 391 bytes | 0 bytes/s, done.
    Total 4 (delta 2), reused 0 (delta 0)
    remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
    To git@github.com:tiketiskte/CSAPP.git
    06be22d..b9a152e master -> master
    Branch master set up to track remote branch master from origin.

这样我们的版本库同步也就好了。

远程仓库发生改变,本地仓库发生改变

待更……

Git的一些命令

  1. git add .git add *的区别:git add .会把本地所有untrack的文件都加入暂存区,并且会根据.gitignore做过滤,但是git add *会忽略.gitignore把任何文件都加入
  2. git add时,可以采用git add [file1] [file2] ...将一个或多个文件添加到暂存区,也可以采用git add [dir]添加指定目录到暂存区(包括子目录),同样也可以采用git add .添加当前目录下的所有文件到暂存区