本地库上传到远程库
创建本地库
$ git init
Initialized empty Git repository in H:/Admin桌面}/临时}Git/config-repo-demo/.git/
新增文件后查看状态
hu@hu MINGW64 /h/Admin桌面}/临时}Git/config-repo-demo (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
config-client-dev.yml
config-client.yml
nothing added to commit but untracked files present (use "git add" to track)
将当前目录所有文件提交本地git仓库
hu@hu MINGW64 /h/Admin桌面}/临时}Git/config-repo-demo (master)
$ git add .
提交版本信息到本地库
hu@hu MINGW64 /h/Admin桌面}/临时}Git/config-repo-demo (master)
$ git commit -m "添加配置信息}"
[master (root-commit) b19476a] 添加配置信息
2 files changed, 4 insertions(+)
create mode 100644 config-client-dev.yml
create mode 100644 config-client.yml
将本地的仓库关联到GitHub
hu@hu MINGW64 /h/Admin桌面}/临时}Git/config-repo-demo (master)
$ git remote add origin https://github.com/huingsn/config-repo-demo.git
将远程库下载到本地
hu@hu MINGW64 /h/Admin桌面}/临时}Git/config-repo-demo (master)
$ git clone https://github.com/huingsn/config-repo-demo.git
Cloning into 'config-repo-demo'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
上传github之前pull一下
hu@hu MINGW64 /h/Admin桌面}/临时}Git/config-repo-demo (master)
$ git pull --rebase origin master
warning: no common commits
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/huingsn/config-repo-demo
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
First, rewinding head to replay your work on top of it...
Applying: 添加配置信息
上传代码到GitHub远程仓库
hu@hu MINGW64 /h/Admin桌面}/临时}Git/config-repo-demo (master)
$ git push -u origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 341 bytes | 341.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/huingsn/config-repo-demo.git
b67a80d..4191bba master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
完毕!
上面是本地和远程都新建–关联–同步
如果是已经同步好的库,本地做了修改,需要同步,只需要这几个步骤:
1、查看当前的git仓库状态
git status
2、更新全部
git add *
3、接着输入git commit -m "更新说明"
git commit -m "更新说明"
4、先git pull,拉取当前分支最新代码
git pull
5、push到远程master分支上
git push origin master:master
master为我自己的分支的名称,实际应用中,你要改成自己的分支的名称
打开GitHub已经同步了
切换分支
1、查看远程分支
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/config-label-test
remotes/origin/master
可以看到,我们现在在master分支下
2、查看本地分支
$ git branch
* master
3、切换分支
$ git checkout -b config-label-test
Switched to a new branch 'config-label-test'
$ git branch
* config-label-test
master
4、切换回master分支
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
git status 查看分支状态
回退版本
$ git reset --hard HEAD
git reset的作用是修改HEAD的位置,即将HEAD指向的位置改变为之前存在的某个版本
git revert的作用通过反做创建一个新的版本,这个版本的内容与我们要回退到的目标版本一样,但是HEAD指针是指向这个新生成的版本,而不是目标版本。
$ git fetch 相当于是从远程获取最新到本地,不会自动merge
git pull:相当于是从远程获取最新版本并merge到本地,在实际使用中,git fetch更安全一些
git reflog查看所有历史提交记录其中包括已用reset命令删除的提交
在vs中每次更新代码都会要输入账号密码,方便起见,可以配置一下让GIT记住密码账号。
git config –global credential.helper store //在Git Bash输入这个命令就可以了
git修改commit时间
git commit –amend –date=”Thu Aug 9 23:04:57 2018 -0700”