公司突然希望我们使用一款软件管理软件,就有同事推荐使用知名度比较高的Git管理软件,第一次使用有好多不习惯的直接上指令
1、安装完git后首先需要配置上传的用户名和邮箱(这个只是记录上传时的用户名信息)
$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"
初始化一个Git仓库,使用git init
命令。
添加文件到Git仓库,分两步:
<1>使用命令git add <file>
,注意,可反复多次使用,添加多个文件;
<2>使用命令git commit -m <message>
,完成。
2、需要使用 git init初始化git目录
作用是使本地文件夹作为成Git可以管理的仓库
$ git init
Initialized empty Git repository in H:/Git_GavinZhang/repository1/xxx/.git/
3、用命令git add
把文件添加到仓库
$ git add git_test.c
执行上面的命令,没有任何显示,说明添加成功。
4、用命令git commit
把文件提交到仓库
$ git commit -m "add file"
[master (root-commit) 79ce3f5] worte a readme file
1 file changed, 14 insertions(+)
create mode 100644 /Git_test.c
简单解释一下git commit
命令,-m
后面输入的是本次提交的说明,可以输入任意内容,当然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。
5、使用命令git remote add
把它和Gitee的远程库关联,
$ git remote add gitee https://gitee.com/xxx1/xxx2.git
在自己gitee上厂库网址复制到命令中,注意复制完后需要在最后加上 .git才可以。
大多数网上会使用这种方式添加,不过我没有添加成功。
$ git remote add gitee git@gitee.com:xxx1/xxx2.git
这说明本地库已经关联了一个名叫gitee
的远程库,此时,可以先用git remote -v
查看远程库信息:
$ git remote -v
$ gitee https://gitee.com/xxx1/xxx2.git (fetch)
$ gitee https://gitee.com/xxx1/xxx2.git (push)
6、我们可以删除已有的Gitee远程库:
$ git remote rm gitee
7、使用git push 推送到gitee厂库,第一次使用是需要加-u
$ git push -u gitee master
直接推送会出现报错,根据提示输入命令无效,报错! [rejected] master -> master (fetch first);原因是缺少文件,其实你自己创建了,再去上传还是错的。
$ git push -u gitee master
To https://gitee.com/xxx1/xxx2.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/xxx1/xxx2.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.
正确的解决方法就是将你的仓库和你的gitee合并了,用填充的方法,即
$ git pull --rebase gitee master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), 1.37 KiB | 6.00 KiB/s, done.
From https://gitee.com/xxx1/xxx2
* branch master -> FETCH_HEAD
* [new branch] master -> gitee/master
Successfully rebased and updated refs/heads/master.
输入上述命令,其中gitee代表你的仓库,后面的master表示将当前的提交到本地仓库的内容和远程仓库合并;
再输入如下命令,即可将本地仓库推送到远程仓库:
$ git push gitee master
Enumerating objects: 26, done.
Counting objects: 100% (26/26), done.
Delta compression using up to 12 threads
Compressing objects: 100% (24/24), done.
Writing objects: 100% (25/25), 12.06 KiB | 2.01 MiB/s, done.
Total 25 (delta 8), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.3]
To https://gitee.com/xxx1/repository1.git
fd9fdfb..615b7cb master -> master
8、经过上面几个步骤就可以在gitee厂库看到自己上传的文件了。