第一篇:Git基本命令的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36279318/article/details/81048343

(一)基本命令的使用

注:参考Pro Git简体中文版
注:参考Git官方文档
注:参考Git中文文档
注:参考Git官方文档
注:参考易百Git教程
注:廖雪峰的官方网站

这里写图片描述

1.创建本地仓库的操作步骤如下:
在D:\Gitlearn目录下创建工作区并初始化本地仓库
lenovo@Lenovo-PC MINGW64 /d/Gitlearn
$ mkdir respository //1.创建工作区目录

lenovo@Lenovo-PC MINGW64 /d/Gitlearn
$ cd respository/  //2.进入工作区

lenovo@Lenovo-PC MINGW64 /d/Gitlearn/respository
$ git init  //3.执行完此命令后,初始化本地仓库已经完成
Initialized empty Git repository in D:/Gitlearn/respository/.git/

lenovo@Lenovo-PC MINGW64 /d/Gitlearn/respository (master)
$ ls -a  //4.查看本地仓库(.git目录位于respository根目录下)
./  ../  .git/

2.使用 git add +文件名把文件添加到Stage(暂存区),再使用git commit -m"描述信息"命令将文件提交到git仓库
lenovo@Lenovo-PC MINGW64 /d/Gitlearn/repository (master)
$ git add demo.txt //1.把文件添加到暂存区

lenovo@Lenovo-PC MINGW64 /d/Gitlearn/repository (master)
$ git status  //2.查看状态
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   demo.txt
        new file:   test.txt


lenovo@Lenovo-PC MINGW64 /d/Gitlearn/repository (master)
$ git commit  -m"测试用例"  //3.把暂存区的文件添加到git仓库
[master 9327845] 测试用例
 2 files changed, 2 insertions(+)
 create mode 100644 demo.txt
 create mode 100644 test.txt

lenovo@Lenovo-PC MINGW64 /d/Gitlearn/repository (master)
$ git ls-files  //4.查看暂存区是否有文件提交

demo.txt
readme.txt
test.txt

 


3.使用git rm+文件名删除当前工作空间中和索引中的文件
lenovo@Lenovo-PC MINGW64 /d/Gitlearn/repository (master)
$ ls  //1.工作区中的文件
demo.txt  readme.txt  test.txt

lenovo@Lenovo-PC MINGW64 /d/Gitlearn/repository (master)
$ git rm readme.txt  //2.删除git仓库中的文件(删除多个文件空格隔开)
rm 'readme.txt'

lenovo@Lenovo-PC MINGW64 /d/Gitlearn/repository (master)
$ git status  //3.查看删除状态
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    readme.txt  //删除文件的名称
        
lenovo@Lenovo-PC MINGW64 /d/Gitlearn/repository (master)
$ git ls-files  //4.查看删除后git仓库中的文件
demo.txt
test.txt



4.使用git log命令查看历史日志

参考:git log命令全解析,打log还能这么随心所欲!

  1. git log 查看提交历史记录

  2. git log - -oneline 或者 git log - -pretty=oneline 以精简模式显示

  3. git log - -graph 以图形模式显示

  4. git log - -stat 显示文件更改列表

  5. git log - -author= ‘name’ 显示某个作者的日志

  6. git log -p filepath 查看某个文件的详细修改

  7. git log -L start,end:filepath 查看某个文件某几行范围内的修改记录

  8. git log - -stat commitId 或者 git show - -stat commitId 查看某一次提交的文件修改列表


lenovo@Lenovo-PC MINGW64 /d/Gitlearn/repository (master)
$ git log  // 1.使用此命令查看日志
commit 9327845036d7f9e0203abbb299b330aee208c49f (HEAD -> master)
Author: Kaina <[email protected]>
Date:   Sat Jul 14 22:08:21 2018 +0800

    测试用例

commit 97c34a16cf22d3725f5c1e07af1df3877d7e29da
Author: Kaina <[email protected]>
Date:   Sat Jul 14 21:48:17 2018 +0800

    测试添加文件到仓库中

5.使用git push把本地仓库中的代码提交到远程版本仓库中

注:1.如何使用Git建立本地仓库并上传代码到GitHub

lenovo@Lenovo-PC MINGW64 /d/Gitlearn/repository (master)
$ git config --global user.name"Kaina"  //1,账号

lenovo@Lenovo-PC MINGW64 /d/Gitlearn/repository (master)
$ git config --global user.email "邮箱" //2.邮箱

lenovo@Lenovo-PC MINGW64 /d/Gitlearn/repository (master)
$ git remote add origin https://github.com/666666666/Test.git //3.添加到远程仓库

lenovo@Lenovo-PC MINGW64 /d/Gitlearn/repository (master)
$ git push -u origin master  //4.推送到主分支
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 554 bytes | 277.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To https://github.com/666666666/Test.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.



2.git 提示fatal: remote origin already exists 解决办法
1.先删除远程 Git 仓
$ git remote rm origin
2.再添加远程 Git 仓库
$ git remote add origin 仓库地址
3.如果执行 git remote rm origin 依然报错,可以手动修改gitconfig文件的内容

$ vi .git/config //把config文件的 [remote “origin”] 一行删掉
lenovo@Lenovo-PC MINGW64 ~/Desktop/项目/ssm-crud (master)
$ git remote add origin https://github.com/666666666/Demo1.git
fatal: remote origin already exists. //1.显示文件存在

lenovo@Lenovo-PC MINGW64 ~/Desktop/项目/ssm-crud (master)
$ git remote rm origin  //2.删除此文件

lenovo@Lenovo-PC MINGW64 ~/Desktop/项目/ssm-crud (master)
//3.将已有的文件添加到仓库
$ git remote add origin https://github.com/666666666/Demo1.git  

lenovo@Lenovo-PC MINGW64 ~/Desktop/项目/ssm-crud (master)
$ git push -u origin master //4.将项目文件推到master分支
Enumerating objects: 81, done.
Counting objects: 100% (81/81), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (69/69), done.
Writing objects: 100% (81/81), 342.71 KiB | 2.21 MiB/s, done.
Total 81 (delta 10), reused 0 (delta 0)
remote: Resolving deltas: 100% (10/10), done.
To https://github.com/666666666/Demo1.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

6.git clone +url克隆远程版本库
lenovo@Lenovo-PC MINGW64 /d/Gitlearn/TestCloneRepository (master)
#1.把远程仓库克隆到本地仓库的libgit2
$ git clone https://github.com/libgit2/libgit2  
Cloning into 'libgit2'...
remote: Counting objects: 82264, done.
remote: Compressing objects: 100% (23369/23369), done.
remote: Total 82264 (delta 57440), reused 82254 (delta 57430), pack-reused 0
Receiving objects: 100% (82264/82264), 37.70 MiB | 1.04 MiB/s, done.
Resolving deltas: 100% (57440/57440), done.
Checking out files: 100% (5653/5653), done.

7.git branch命令使用详解
操作 命令
1.查看当前有哪些分支 $ git branch
2.新建一个分支 $ git branch dev2
3.切换到指定分支 $ git checkout dev2
4. 查看本地和远程分支 $ git branch -a
5.将更改添加到新建分支上 $ git push origin dev2
6.修改分支的名字 $ git branch -m dev2 version.2
7.删除远程分支 $ git push origin --delete dev2
8.合并分支 $ git merge version.2
8.git checkout命令的作用:在不同的分支之间进行切换

注:参考git checkout命令详解

lenovo@Lenovo-PC MINGW64 /d/Gitlearn/TestCloneRepository (master) #默认分支
$ git branch  #1.查看git仓库的分支
  dev1
  dev2
* master

lenovo@Lenovo-PC MINGW64 /d/Gitlearn/TestCloneRepository (master)
$ git checkout  dev1  #2.切换到dev1分支
Switched to branch 'dev1'

lenovo@Lenovo-PC MINGW64 /d/Gitlearn/TestCloneRepository (dev1) #切换到dev1分支
$


9.git stash命令:将当前未提交的工作存入Git工作栈中,时机成熟的时候在应用回来

注:参考git stash命令详解
注:易百教程git stash命令

10.git pull命令:把远程仓库pull到本地仓库
11.配置SSH,生成公钥和私钥

lenovo@Lenovo-PC MINGW64 ~
$  git config --global user.name kaina  //1.配置账号

lenovo@Lenovo-PC MINGW64 ~
$  git config --global user.email "自己的邮箱" //2.配置邮箱

lenovo@Lenovo-PC MINGW64 ~
$  ssh-keygen -t rsa -C "自己的邮箱" //3.生成SSH
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/lenovo/.ssh/id_rsa):
/c/Users/lenovo/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/lenovo/.ssh/id_rsa. //4.生成公钥文件
Your public key has been saved in /c/Users/lenovo/.ssh/id_rsa.pub. //5.生成私钥文件
The key fingerprint is:
SHA256:1CffJc6gg+ELZvNMU8SJRNS7FOvqRo50CujdaVqd9/E [email protected]
The key's randomart image is:  //6.表示SSH公钥私钥生成成功
+---[RSA 2048]----+
|       +++..     |
|        .o=      |
|        o ++o . .|
|       o ++= = o |
|   .  = Soo.. +  |
|  . .o.Bo+o.     |
| . . +.B*.. .    |
|  . ..* +. . o   |
|    .o o.   . E  |
+----[SHA256]-----+


12.将本地磁盘保存的项目上传到Github
lenovo@Lenovo-PC MINGW64 ~/Desktop/项目/SSM项目/AJax实现分页功能/LearnProgram (master)
$ git init   // 1.初始化仓库
Reinitialized existing Git repository in C:/Users/lenovo/Desktop/项目/SSM项目/AJax实现分页功能/LearnProgram/.git/

lenovo@Lenovo-PC MINGW64 ~/Desktop/项目/SSM项目/AJax实现分页功能/LearnProgram (master)
$ git add .   //2.把所有项目文件添加到仓库
warning: LF will be replaced by CRLF in src/main/resources/spring-bean.xml.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in src/main/resources/springmvc.xml.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in src/main/webapp/static/bootstrap-3.3.7-dist/css/bootstrap-theme.css.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in src/main/webapp/static/bootstrap-3.3.7-dist/css/bootstrap-theme.min.css.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in src/main/webapp/static/bootstrap-3.3.7-dist/css/bootstrap.css.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in src/main/webapp/static/bootstrap-3.3.7-dist/css/bootstrap.min.css.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in src/main/webapp/static/bootstrap-3.3.7-dist/fonts/glyphicons-halflings-regular.svg.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in src/main/webapp/static/bootstrap-3.3.7-dist/js/bootstrap.js.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in src/main/webapp/static/bootstrap-3.3.7-dist/js/bootstrap.min.js.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in src/main/webapp/static/bootstrap-3.3.7-dist/js/npm.js.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in target/classes/spring-bean.xml.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in target/classes/springmvc.xml.
The file will have its original line endings in your working directory.

lenovo@Lenovo-PC MINGW64 ~/Desktop/项目/SSM项目/AJax实现分页功能/LearnProgram (master)
$ git commit -m "提交SSM项目" //3.提交项目
[master (root-commit) 5d8cf7c] 提交SSM项目
 79 files changed, 13281 insertions(+)
 create mode 100644 .classpath
 create mode 100644 .project
 create mode 100644 .settings/.jsdtscope
 create mode 100644 .settings/org.eclipse.jdt.core.prefs
 create mode 100644 .settings/org.eclipse.m2e.core.prefs
 create mode 100644 .settings/org.eclipse.wst.common.component
 create mode 100644 .settings/org.eclipse.wst.common.project.facet.core.xml
 create mode 100644 .settings/org.eclipse.wst.jsdt.ui.superType.container
 create mode 100644 .settings/org.eclipse.wst.jsdt.ui.superType.name
 create mode 100644 .settings/org.eclipse.wst.validation.prefs
 create mode 100644 mbg.xml
 create mode 100644 pom.xml
 create mode 100644 src/main/java/com/wang/ssm/bean/Department.java
 create mode 100644 src/main/java/com/wang/ssm/bean/DepartmentExample.java
 create mode 100644 src/main/java/com/wang/ssm/bean/Employee.java
 create mode 100644 src/main/java/com/wang/ssm/bean/EmployeeExample.java
 create mode 100644 src/main/java/com/wang/ssm/bean/Msg.java
 create mode 100644 src/main/java/com/wang/ssm/controller/EmployeeControlller.java
 create mode 100644 src/main/java/com/wang/ssm/dao/DepartmentMapper.java
 create mode 100644 src/main/java/com/wang/ssm/dao/EmployeeMapper.java
 create mode 100644 src/main/java/com/wang/ssm/servicce/EmployeeService.java
 create mode 100644 src/main/java/com/wang/ssm/test/MBGTest.java
 create mode 100644 src/main/java/com/wang/ssm/test/MapperTest.java
 create mode 100644 src/main/java/com/wang/ssm/test/SpringMVCTest.java
 create mode 100644 src/main/resources/dbconfig.properties
 create mode 100644 src/main/resources/mapper/DepartmentMapper.xml
 create mode 100644 src/main/resources/mapper/EmployeeMapper.xml
 create mode 100644 src/main/resources/mybatis-config.xml
 create mode 100644 src/main/resources/spring-bean.xml
 create mode 100644 src/main/resources/springmvc.xml
 create mode 100644 src/main/webapp/META-INF/MANIFEST.MF
 create mode 100644 src/main/webapp/WEB-INF/web.xml
 create mode 100644 src/main/webapp/index.jsp
 create mode 100644 src/main/webapp/static/bootstrap-3.3.7-dist/css/bootstrap-theme.css
 create mode 100644 src/main/webapp/static/bootstrap-3.3.7-dist/css/bootstrap-theme.css.map
 create mode 100644 src/main/webapp/static/bootstrap-3.3.7-dist/css/bootstrap-theme.min.css
 create mode 100644 src/main/webapp/static/bootstrap-3.3.7-dist/css/bootstrap-theme.min.css.map
 create mode 100644 src/main/webapp/static/bootstrap-3.3.7-dist/css/bootstrap.css
 create mode 100644 src/main/webapp/static/bootstrap-3.3.7-dist/css/bootstrap.css.map
 create mode 100644 src/main/webapp/static/bootstrap-3.3.7-dist/css/bootstrap.min.css
 create mode 100644 src/main/webapp/static/bootstrap-3.3.7-dist/css/bootstrap.min.css.map
 create mode 100644 src/main/webapp/static/bootstrap-3.3.7-dist/fonts/glyphicons-halflings-regular.eot
 create mode 100644 src/main/webapp/static/bootstrap-3.3.7-dist/fonts/glyphicons-halflings-regular.svg
 create mode 100644 src/main/webapp/static/bootstrap-3.3.7-dist/fonts/glyphicons-halflings-regular.ttf
 create mode 100644 src/main/webapp/static/bootstrap-3.3.7-dist/fonts/glyphicons-halflings-regular.woff
 create mode 100644 src/main/webapp/static/bootstrap-3.3.7-dist/fonts/glyphicons-halflings-regular.woff2
 create mode 100644 src/main/webapp/static/bootstrap-3.3.7-dist/js/bootstrap.js
 create mode 100644 src/main/webapp/static/bootstrap-3.3.7-dist/js/bootstrap.min.js
 create mode 100644 src/main/webapp/static/bootstrap-3.3.7-dist/js/npm.js
 create mode 100644 src/main/webapp/static/js/jquery-1.12.4.min.js
 create mode 100644 target/classes/com/wang/ssm/bean/Department.class
 create mode 100644 target/classes/com/wang/ssm/bean/DepartmentExample$Criteria.class
 create mode 100644 target/classes/com/wang/ssm/bean/DepartmentExample$Criterion.class
 create mode 100644 target/classes/com/wang/ssm/bean/DepartmentExample$GeneratedCriteria.class
 create mode 100644 target/classes/com/wang/ssm/bean/DepartmentExample.class
 create mode 100644 target/classes/com/wang/ssm/bean/Employee.class
 create mode 100644 target/classes/com/wang/ssm/bean/EmployeeExample$Criteria.class
 create mode 100644 target/classes/com/wang/ssm/bean/EmployeeExample$Criterion.class
 create mode 100644 target/classes/com/wang/ssm/bean/EmployeeExample$GeneratedCriteria.class
 create mode 100644 target/classes/com/wang/ssm/bean/EmployeeExample.class
 create mode 100644 target/classes/com/wang/ssm/bean/Msg.class
 create mode 100644 target/classes/com/wang/ssm/controller/EmployeeControlller.class
 create mode 100644 target/classes/com/wang/ssm/dao/DepartmentMapper.class
 create mode 100644 target/classes/com/wang/ssm/dao/EmployeeMapper.class
 create mode 100644 target/classes/com/wang/ssm/servicce/EmployeeService.class
 create mode 100644 target/classes/com/wang/ssm/test/MBGTest.class
 create mode 100644 target/classes/com/wang/ssm/test/MapperTest.class
 create mode 100644 target/classes/com/wang/ssm/test/SpringMVCTest.class
 create mode 100644 target/classes/dbconfig.properties
 create mode 100644 target/classes/mapper/DepartmentMapper.xml
 create mode 100644 target/classes/mapper/EmployeeMapper.xml
 create mode 100644 target/classes/mybatis-config.xml
 create mode 100644 target/classes/spring-bean.xml
 create mode 100644 target/classes/springmvc.xml
 create mode 100644 target/m2e-wtp/web-resources/META-INF/MANIFEST.MF
 create mode 100644 target/m2e-wtp/web-resources/META-INF/maven/com.gaolei.ssm/ssm-crud/pom.properties
 create mode 100644 target/m2e-wtp/web-resources/META-INF/maven/com.gaolei.ssm/ssm-crud/pom.xml
 create mode 100644 target/m2e-wtp/web-resources/META-INF/maven/com.wang.ssm/ssm-crud/pom.properties
 create mode 100644 target/m2e-wtp/web-resources/META-INF/maven/com.wang.ssm/ssm-crud/pom.xml

lenovo@Lenovo-PC MINGW64 ~/Desktop/项目/SSM项目/AJax实现分页功能/LearnProgram (master)
$ git remote add origin https://github.com/666666666/JavaProject.git 
fatal: remote origin already exists. //4.将项目添加到github仓库,报此错误

lenovo@Lenovo-PC MINGW64 ~/Desktop/项目/SSM项目/AJax实现分页功能/LearnProgram (master)
$ git remote rm origin //5.报以上错误使用此命令

lenovo@Lenovo-PC MINGW64 ~/Desktop/项目/SSM项目/AJax实现分页功能/LearnProgram (master)
$ git remote add origin https://github.com/666666666/JavaProject.git //6.push到远程仓库

lenovo@Lenovo-PC MINGW64 ~/Desktop/项目/SSM项目/AJax实现分页功能/LearnProgram (master)
$ git push -u origin master //7.将项目源码添加到master分支
Enumerating objects: 115, done.
Counting objects: 100% (115/115), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (96/96), done.
Writing objects: 100% (115/115), 357.86 KiB | 1.54 MiB/s, done.
Total 115 (delta 17), reused 0 (delta 0)
remote: Resolving deltas: 100% (17/17), done.
To https://github.com/666666666/JavaProject.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

猜你喜欢

转载自blog.csdn.net/weixin_36279318/article/details/81048343