Git(读音为/gɪt/。)是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。 [1] Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
命令:
git init git add * git commit -m "Initial commit" git remote add origin https://github.com/testaccount/demo.git git pull origin master --allow-unrelated-histories git push -u origin master
1. 进入根目录
$ cd /Users/admin/Documents/work/demo
2. 初始化本地仓库
$ git init
输出如下:
Initialized empty Git repository in /Users/admin/Documents/work/demo/.git/
即创建了一个空的本地Git仓库。
3. 将代码添加到本地仓库
$ git add .
将根目录下的所有文件添加到本地仓库中,git add .
或者 git add --all 或者 git add *
如果只添加单个文件,只需把.换成你要添加的文件名即可,如git add README.md;
4. 把代码提交到本地仓库
git commit -m “添加你的注释,一般是一些更改信息”
$ git commit -m "Initial commit"
输出:
[master (root-commit) a77a31a] Initial commit 47 files changed, 959 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 demo/.classpath create mode 100644 demo/.project create mode 100644 demo/.settings/org.eclipse.core.resources.prefs create mode 100644 demo/.settings/org.eclipse.jdt.core.prefs create mode 100644 demo/.settings/org.eclipse.m2e.core.prefs create mode 100644 dem/pom.xml ......
5. 将本地的库链接到远程服务器
$ git remote add origin https://github.com/testaccount/demo.git
如果执行git remote add xxxx时报错fatal: remote origin already exists,解决办法:git remote rm origin,删除远程仓库的origin,然后再重新执行add命令。
6. 推送前先合并
这时候还是不可以push的,可能会提示:
To https://github.com/testaccount/demo.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'https://github.com/testaccount/demo.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
意思是在push之前,需要先pull一下,将远程的主分支down下来,可以合并本地内容。
$ git pull origin master
如果报fatal:refusing to merge unrelated histories错误,意思是拒绝合并不相关的历史,确实本地是新加的内容还重未和线上连接过,或者线上手动添加了一个文件,未曾和本地代码仓库同步。解决办法:可以添加–allow-unrelated-histories参数,意思告诉系统我允许合并不相关历史的内容。
$ git pull origin master --allow-unrelated-histories
输出:
remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (4/4), done. remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (4/4), done. From https://github.com/testaccount/demo * branch master -> FETCH_HEAD * [new branch] master -> origin/master Merge made by the 'recursive' strategy. .gitignore | 23 +++++++++++++++++++++++
合并成功,如果不存在不相关历史的内容,可略过此步。
7. 推送到远程服务器
$ git push -u origin master
输出:
Counting objects: 135, done. Delta compression using up to 4 threads. Compressing objects: 100% (63/63), done. Writing objects: 100% (135/135), 12.75 KiB | 768.00 KiB/s, done. Total 135 (delta 16), reused 0 (delta 0) remote: Resolving deltas: 100% (16/16), done. To https://github.com/testaccount/demo.git e465e22..35e0d1e master -> master Branch 'master' set up to track remote branch 'master' from 'origin'.
进入远程仓库个人控制台,刷新页面,会发现代码已经同步成功了。