Git使用详解

文章内容参考:廖雪峰Git教程

Git命令:

git init  //把当前目录变成Git可以 管理的仓库
git add   //把文件添加到仓库
git commit -m  //带说明的提交,说明信息最好具有实际意义
git status  //随时掌握工作区的状态
git diff  //查看修改的内容
git log  //可以查看提交历史,以便确定要回退到哪个版本
git log --pretty=oneline  //查看提交历史的简要信息
git reset //回退版本 
git reset --hard HEAD^  //回退到上一个版本
git reset --hard HEAD^^  //回退到上上个版本
git reset --hard HEAD~100  //回退到前100个版本
git reset --hard 1094a  //回退到指定版本号
git reflog  //记录你的每一次命令,以便确定要回到未来的哪个版本
git checkout -- file  //丢弃工作区的修改
git reset HEAD   //可以把暂存区的修改撤销掉(unstage),重新放回工作区
git rm  //用于删除一个文件
git remote add origin  //关联一个远程库
git push -u origin master  //第一次推送master分支的所有内容
git push origin master  //推送最新修改
git clone <远程仓库地址>  //克隆一个库到本地库
git branch <分支名称>  //创建分支
git checkout <分支名称>  //切换分支
git checkout -b <分支名称>  //创建并切换分支
git branch  //查看当前分支
git merge  //合并指定分支到当前分支
git switch  //切换分支
git switch -c <分支名称>  //创建并切换分支
git branch -d <分支名称>  //删除分支
git log --graph  //查看分支合并图
git merge --no-ff -m <描述> <分支名称>  //合并分支,--no-ff参数,表示禁用Fast forward
git stash  //把当前工作现场“储藏”起来,等以后恢复现场后继续工作
git stash pop  //回到工作现场
git cherry-pick   //把bug提交的修改“复制”到当前分支,避免重复劳动
git branch -D <分支名称>  //丢弃一个没有被合并过的分支,强行删除
git remote  //查看远程库的信息
git remote -v  //显示更详细的信息
git push origin branch-name  //从本地推送分支
git pull  //抓取远程的新提交
git checkout -b branch-name origin/branch-name  //在本地创建和远程分支对应的分支,本地和远程分支的名称最好一致
git branch --set-upstream branch-name origin/branch-name  //建立本地分支和远程分支的关联
git rebase  //把分叉的提交历史“整理”成一条直线,看上去更直观
git tag   //就可以打一个新标签
git tag v0.9 f52c633  //要对某次add merge提交打标签,且它对应的commit id是f52c633
git show   //查看标签信息
git tag -a  -m "blablabla..."  //可以指定标签信息

Git 命令使用:

默认使用visual studio code

git clone --使用git clone从远程仓库拉取代码

git clone [email protected]:nangongque/doumijia-react-native.git
/** Cloning into 'doumijia-react-native'...
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
remote: Enumerating objects: 94, done.
remote: Counting objects: 100% (94/94), done.
remote: Compressing objects: 100% (78/78), done.
remote: Total 94 (delta 2), reused 91 (delta 2), pack-reused 0R
Receiving objects: 100% (94/94), 232.52 KiB | 163.00 KiB/s, done.
Resolving deltas: 100% (2/2), done.**/

git add --把文件添加到仓库

/** 修改README.md文件如下:
豆米家
2020.9.15更新,由于项目中使用的豆瓣api经常变动,导致接口有时不可用,有问题可以及时联系我的邮箱,谢谢。
**/
/**保存后进行添加**/
 git add README.md

git commit --提交

/** 针对add后修改的文件进行提交**/
git commit -m 'first change README.md'
/** 
[master 20e8cc3] first change README.md
 1 file changed, 2 insertions(+), 1 deletion(-)
**/

git status --随时掌握工作区的状态

/**时刻掌握仓库当前的状态 **/
git status
/**
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean
**/

git diff --查看修改的内容

git diff 
/**
diff --git a/README.md b/README.md
index 552a71e..139e58e 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,3 @@
 豆米家
-2020.9.15更新,由于项目中使用的豆瓣api经常变动,导致接口有时不可用,有问题可以及时联系我的邮箱,谢谢。
\ No newline at end of file
+2020.9.15更新,由于项目中使用的豆瓣api经常变动,导致接口有时不可用,有问题可以及时联系我的邮箱,谢谢。
+米西米西,画布垃圾
\ No newline at end of file
**/

git log --查看提交历史,以便确定要回退到哪个版本

git log
/**
commit 20e8cc32eba612e88c1209c8206af90d236b9d3b (HEAD -> master, origin/master, origin/HEAD)
Author: murong-yuxuan <350****[email protected]>
Date:   Tue Sep 15 22:11:49 2020 +0800

    first change README.md

commit 429d3cee52b38ec8846299704f7a108f4348a36f
Author: 李*坡 <189****[email protected]>
Date:   Tue Sep 15 12:39:50 2020 +0800

    Initial commit

commit 1c33f7c2f470cc5bbb7060b237379b6882e663ff
Author: nangongque <[email protected]>
Date:   Tue Sep 15 11:47:56 2020 +0800

    Initial commit
**/
git log --pretty=onelin //查看提交历史的简要信息
/**
20e8cc32eba612e88c1209c8206af90d236b9d3b (HEAD -> master, origin/master, origin/HEAD) first change README.md
429d3cee52b38ec8846299704f7a108f4348a36f Initial commit
1c33f7c2f470cc5bbb7060b237379b6882e663ff Initial commit
**/

git push origin master --推送最新修改

git push origin master
/**
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 314 bytes | 314.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:nangongque/doumijia-react-native.git
   20e8cc3..a145af8  master -> master
**/

git reset --hard HEAD^ --回退到上一个版本

git reset --hard HEAD^
/** README.md文件被还原到上一个版本的内容**/

git reflog -- 记录你的每一次命令,以便确定要回到未来的哪个版本

git reflog
/**
20e8cc3 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
a145af8 (origin/master, origin/HEAD) HEAD@{1}: commit: first change commit
20e8cc3 (HEAD -> master) HEAD@{2}: commit: first change README.md
429d3ce HEAD@{3}: clone: from [email protected]:nangongque/doumijia-react-native.git
**/

git checkout -- file --丢弃工作区的修改

git checkout -- .\README.md
/** 上一次修改的代码将被撤销**/

git branch --创建分支

git branch test1 

git checkout --切换分支

git checkout test1
/**Switched to branch 'test1'**/

git push origin --推送分支到远程仓库

git push origin test1:test1
/**
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'test1' on GitHub by visiting:
remote:      https://github.com/nangongque/doumijia-react-native/pull/new/test1
remote:
To github.com:nangongque/doumijia-react-native.git
 * [new branch]      test1 -> test1
**/

git switch --切换分支

git branch
/**
  master
* test1
**/
 git switch master
/**
Switched to branch 'master'
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)
**/
git branch
/**
* master
  test1
**/

git stash --把当前工作现场“储藏”起来,等以后恢复现场后继续工作

git stash
/**
Saved working directory and index state WIP on master: 20e8cc3 first change README.md
**/
/**会把工作区中修改的代码暂时隐藏起来**/

git stash pop --回到工作现场

git stash pop
/**
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (f09780a198fda8a2f4b36d8d0498c52daef5eab1)
**/
/**先前隐藏的代码又出来了**/

git tag --打一个新标签

git show --查看标签信息

git tag v0.1
git show v0.1
/**
Author: murong-yuxuan <350****[email protected]>
Date:   Tue Sep 15 22:11:49 2020 +0800

    first change README.md

diff --git a/README.md b/README.md
index 49e1184..552a71e 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
-# doumijia-react-native
\ No newline at end of file
+豆米家
+2020.9.15更新,由于项目中使用的豆瓣api经常变动,导致接口有时不可用,有问题可以及时联系我的邮箱,谢谢。
\ No newline at end of file
**/

未完待续....

其他开发工具下使用的git命令后续补上

你可能感兴趣的:(Git使用详解)