git、github常用操作

1、将github项目拷贝到本地

$ git clone https://github.com/jim2500/miaosha_project.git

2、修改本地项目上传到github

T470s@shijun MINGW64 /d/ideaproject/miaosha_project/miaosha_project (master)
$ git add .

T470s@shijun MINGW64 /d/ideaproject/miaosha_project/miaosha_project (master)
$ git commit -m "第一次修改"
[master 067d3e3] 第一次修改
 4 files changed, 85 insertions(+), 104 deletions(-)

T470s@shijun MINGW64 /d/ideaproject/miaosha_project/miaosha_project (master)
$ git push
Enumerating objects: 27, done.
Counting objects: 100% (27/27), done.
Delta compression using up to 4 threads
Compressing objects: 100% (12/12), done.
Writing objects: 100% (14/14), 2.30 KiB | 588.00 KiB/s, done.
Total 14 (delta 6), reused 0 (delta 0)
remote: Resolving deltas: 100% (6/6), completed with 6 local objects.
To https://github.com/jim2500/miaosha_project.git
   21b664f..067d3e3  master -> master

 

3、更新github别人修改的部分

转自:https://blog.csdn.net/weixin_43147867/article/details/90764364

场景: 我在本地创建了一个分支 A ,开发了几天,在我要提交的时候,发现远程的master 版本更新了,这时候我应如何从远程拉取最新的代码,并且与本地分支合并呢
前提:切入A分支
第一步、 查看远程仓库
git remote -v
第二步 我们已经查看到远程有一个叫origin 的仓库了,使用命令获取最新的代码,并且创建新分支 new
git fetch origin master:new
第三步
查看new 分支和咱们的本地分支有啥不同呢
git diff new
在这一步一定要慎重查看,是对自己代码的一次检查(查看一下,新代码有没有跟自己本地的开发有重合的部分,了解变化,合并不慌)
第四步
将new分支和本地分支合并,我们的代码成为最新
git merge new
第五步:删除掉我们为了达到目地创建的分支new. 有种卸磨杀驴的感觉呢。
git branch -d new

如果本地合并分支后后悔了怎么办呢,我们可以回退版本
第一步:git log 查看之前的commit的id,找到想要还原的版本
第二步:git reset --hard <想要还原的版本的commit id> 还原到之前的某个版本

 

eq:

T470s@shijun MINGW64 /d/ideaproject/miaosha_project/miaosha_project (dev)
$ git fetch origin dev:new
From https://github.com/jim2500/miaosha_project
 * [new branch]      dev        -> new

T470s@shijun MINGW64 /d/ideaproject/miaosha_project/miaosha_project (dev)
$ git diff

T470s@shijun MINGW64 /d/ideaproject/miaosha_project/miaosha_project (dev)
$ git diff new
diff --git a/miaosha/src/main/java/com/miaoshaproject/validator/ValidatorImpl.java b/miaosha/src/main/java/com/miaoshaproject/validator/ValidatorImpl.java
index 574f12d..5eea7bf 100644
--- a/miaosha/src/main/java/com/miaoshaproject/validator/ValidatorImpl.java
+++ b/miaosha/src/main/java/com/miaoshaproject/validator/ValidatorImpl.java
@@ -22,6 +22,6 @@ public class ValidatorImpl implements InitializingBean{
     @Override
     public void afterPropertiesSet() throws Exception {
         //将hibernate validator通过工厂的初始化方式使其实例化
-        this.validator = Validation.buildDefaultValidatorFactory().getValidator();
+
     }
 }

T470s@shijun MINGW64 /d/ideaproject/miaosha_project/miaosha_project (dev)
$ git merge new
Updating 542f12e..9152f01
Fast-forward
 miaosha/src/main/java/com/miaoshaproject/validator/ValidatorImpl.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

 

 

4、创建分支、拷贝github分支到本地分支、修改本地分支上传到github分支

  创建分支: git branch 分支名

  切换分支:git checkout 目标分支名

  注:切换到那个分支,本地文件夹的文件就是该分支的内容

  创建分支+切换+绑定远程分支: git checkout -b dev origin/dev

  具体解释:创建一个dev分支(-b),并把远程dev分支(origin/dev)的内容放在该分支内。接着切换到该分支(checkout)

  查看本地分支:git branch

  查看远程分支:git branch -r

  查看全部分支:git branch -a

  删除本地分支:git branch -d 分支名

  删除远程分支:git push origin:分支名

  注:分之前冒号代表删除

  提交本地分支到远程:

T470s@shijun MINGW64 /d/ideaproject/miaosha_project/miaosha_project (dev)
$ git add .

T470s@shijun MINGW64 /d/ideaproject/miaosha_project/miaosha_project (dev)
$ git commit -m "第一次修改分支dev"
[dev decc133] 第一次修改分支dev
 4 files changed, 85 insertions(+), 104 deletions(-)

T470s@shijun MINGW64 /d/ideaproject/miaosha_project/miaosha_project (dev)
$ git push
Enumerating objects: 27, done.
Counting objects: 100% (27/27), done.
Delta compression using up to 4 threads
Compressing objects: 100% (12/12), done.
Writing objects: 100% (14/14), 2.31 KiB | 473.00 KiB/s, done.
Total 14 (delta 6), reused 0 (delta 0)
remote: Resolving deltas: 100% (6/6), completed with 6 local objects.
To https://github.com/jim2500/miaosha_project.git
   21b664f..decc133  dev -> dev

  

5、master和其他分支合并

$ git merge new
上一篇:小知识-newline = ‘ ‘.join(list(str))的作用


下一篇:java初学