GIT神速入门,Git常用命令总括

git-scm

小说小编:Tyan
博客:noahsnail.com | CSDN | 简书

# 右键--Git Bash Heregit config --global user.name "gdxieyue"git config --global user.email "gdxieyue@gmail.com"ssh-keygen -t rsa -C "gdxieyue@gmail.com"clip < ~/.ssh/id_rsa.pub# windows路径为C:Usersyour_username.sshid_rsa.pub

1. Git酒店的制造

  • 在当前目录新建三个Git货仓
# 命令形式:git init

$ cd TestGit
$ git init
Initialized empty Git repository in /Users/***/TeskGit/.git/
  • 在钦命目录下新建贰个Git货仓
# 命令形式:git init [Directory Name]

$ git init TestGit
Initialized empty Git repository in /Users/***/TeskGit/.git/
  • 从Github等地点克隆三个旅馆到当前目录(恐怕供给输入密码,以ssh方式克隆),假使不钦命分支,则私下认可从Refactor商旅的Default branch(平时为master)克隆
# 命令形式:git clone [url] -b [branch name]

$ git clone ssh://git@github.com/**/Refacor -b master
Cloning into 'Refacor'...
Saving password to keychain failed
Identity added: /Users/**/.ssh/id_rsa_github ((null))
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
Checking connectivity... done.

图片 1git公钥配置.png

2. 关联远程旅舍

  • 将本地酒店与长途酒店关联,首先要先河化一个地点酒馆,url能够在github的库房获得,origin为远程饭馆在地头的小名,即origin=git@github.com:***/Refacor.git
# 命令形式:git remote add [remote repository aliase] [url]

$ git remote add origin git@github.com:***/Refacor.git
  • 翻开当地旅舍关联的长距离酒店
# 命令形式:git remote [-v]

$ git remote
origin
$ git remote -v
origin  git@github.com:***/Refacor.git (fetch)
origin  git@github.com:***/Refacor.git (push)
  • 删去本地货仓关联的长途仓库
# 命令形式:git remote rm [remote repository aliase]

$ git remote rm origin
$ git remote
  • 涉嫌远程宾馆后,从远程客栈取内容,并基于远端饭店在当地创制了四个支行,master和develop
# 命令形式:git fetch

$ git fetch
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:***/Refacor
* [new branch]      develop    -> origin/develop
* [new branch]      master     -> origin/master
  • 显示远程客栈的新闻
# 命令形式:git remote show [remote repository name]

$ git remote show origin
* remote origin
  Fetch URL: git@github.com:***/Refacor.git
  Push  URL: git@github.com:***/Refacor.git
  HEAD branch: master
  Remote branches:
    develop tracked
    master  tracked
  Local branches configured for 'git pull':
    develop merges with remote develop
    master  merges with remote master
  Local refs configured for 'git push':
    develop pushes to develop (up to date)
    master  pushes to master  (local out of date)

传送门: Windows下Git多账号配置,同一Computer多少个ssh-key的管理

3. Git分支的始建、切换、删除、关联

  • 成立本地分支并切换来新创造的支行,-b代表新创立二个分段,-B用在本地分支已经存在的动静下,强行创立多个新支行并将原本的分支覆盖,checkout首假使分段切换,创建分指派用branch命令
# 命令形式:git checkout [-b or -B] [local branch name]

$ git checkout -b a
Switched to a new branch 'a'
$ git checkout -b a
fatal: A branch named 'a' already exists.
$ git checkout -B a
Switched to and reset branch 'a'
  • 切换本地分支
# 命令形式:git checkout [local branch name]

$ git checkout develop
Switched to branch 'develop'
  • 查阅当前所在的地面分支,创立本地分支,local branch name不设有时是翻开当前所在的本地分支,存在时是成立一个新的本土分支
# 命令形式:git branch [local branch name]

$ git branch
* a
  develop
  master

$ git branch b
  • 翻开远程分支和具有支行,-r是翻开远程分支,-a是翻开全体支行
# 命令形式:git branch [-r] [-a]

$ git branch -r
  origin/develop
  origin/master
$ git branch -a
  develop
* master
  remotes/origin/develop
  remotes/origin/master
  • 剔除本地分支
# 命令形式:git branch -d [local branch name]

$ git branch -d test
Deleted branch test (was 0b05e43).
  • 开创本地分支并与远端分支关联
# 命令形式:git checkout -b [local branch name] origin/[remote branch name]

$ git checkout -b develop origin/develop
Branch develop set up to track remote branch develop from origin.
Switched to a new branch 'develop'

$ git checkout -b master origin/master
Branch master set up to track remote branch master from origin.
Switched to a new branch 'master'
  • 关联远程分支
# 命令形式:git branch --set-upstream-to [remote repository name/remote branch]

$ git branch --set-upstream-to origin/develop
Branch develop set up to track remote branch develop from origin.
  • 去除远程分支,git push origin --delete [remote branch name]是一向删除掉远程宾馆的分支,git branch -dr [remote/branch]是删除本地分支与长途分支的关联关系。
# 命令形式:git push origin --delete [remote branch name] or git branch -dr [remote/branch]

$ git push origin --delete develop
To git@github.com:***/Refacor.git
 - [deleted]         develop

$ git branch -dr origin/develop
Deleted remote-tracking branch origin/develop (was d6813fd).

注:本地能够有多个支行,远程也足以有三个分支,本地八个分支能够提到远程五个支行,可是,本地分支最棒与长途分支同名,以防出现难点。

# 配置giteeHost gitee.com HostName gitee.com IdentityFile /home/gdtel/.ssh/gitee PreferredAuthentications publickey User gdxieyue # 配置办公室 132.97.8.10Host 132.97.8.10 HostName 132.97.8.10 IdentityFile /home/gdtel/.ssh/id_rsa PreferredAuthentications publickey User gdxieyue

4.从远程仓库取内容,向远程仓库提交内容

向远程仓库提交内容前边,要明白多少个概念:当半夏件,缓冲区,本地旅馆。平时修改的公文内容都是当和姑件,在往远程客栈提交此前先要提交到缓冲区,再从缓冲区交付到地头仓库,然后本地仓库技术往远程宾馆提交。当地的开始和结果分为三超过50%,它们是相互独立的,掌握了那三个概念就了然为啥要推行git add,git commit,git push命令了。

  • 从远程仓库取内容,git fetch默许情状下是前段时间的本地分支从其关联的长途分支上取内容,能够利用git branch先查看当前的地面分支,使用git status(包蕴地面分支和长距离分支都能看见)查看其涉及的远程分支。从上面包车型大巴例子中得以看看地点分支为develop,远程分支也为develop
# 命令形式:git fetch,git merge,git pull

$ git branch
* develop
  master
$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
nothing to commit, working directory clean
$ git fetch
  • 将从远程仓库取下的内容与地面商旅的剧情开展统一,合併在此以前必须将本地的修改提交到本地仓库。
# 命令形式:git merge

# 正常情况下
$ git merege
Already up-to-date.

# 没有提交修改到本地仓库的情况下
$ git merge
Updating d6813fd..2444abc
error: Your local changes to the following files would be overwritten by merge:
    README.md
Please, commit your changes or stash them before you can merge.
Aborting

# 本地仓库与取下的远程仓库内容有冲突的情况下
$ git merge
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
  • 消除争辨,冲突是选拔git时平日会遇见的事态,冲突消除重视是在将地面栈室内容与长途旅社取下的内容merge后,踏入merge后的文件进行修改,将冲突化解,然后重新add,commit,push就能够。

争辨内容:

# Refacor
<<<<<<< HEAD
branch develop
=======
branch
>>>>>>> refs/remotes/origin/develop

修改后的剧情:

# Refacor
branch develop

管理进程:

$ vim README.md 
$ git add .
$ git commit -m "feat: handle conflict"
[develop d4c01a6] feat: handle conflict
$ git push
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 416 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To git@github.com:***/Refacor.git
   2444abc..d4c01a6  develop -> develop
  • 一贯从远程酒店取内容并统一
# 命令形式:git pull
$ git pull
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:***/Refacor
   d4c01a6..afa74f2  develop    -> origin/develop
Updating d4c01a6..afa74f2
Fast-forward
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

备注:git pull = git fetch + git merge,git pull功效很有力,能活动合併冲突,合併后须求手动化解争辩,最棒不要向来利用git pull,因为不菲细节都看不到。

  • 将本麻芋果件修改提交到缓冲区,git add filename是将单个文件提交到缓冲区,能够付出四个公文,中间用空格分开,也可交付目录,git add . 是将持有修改内容提交到缓冲区
# 命令形式:git add [filename1] [filename2] or [directoryname] or [.]
$ git add README.md
$ git add README.md a.txt
$ git add . 
  • 将缓冲区内容交给到本地旅馆
# 命令形式:git commit [file1] [file2] -m [comment]
$ git commit -m "feat: edit README.md"
[develop d6813fd] feat: edit README.md
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git commit README.md a.txt -m "feat: commit test"
  • 将当地旅社内容提交到长途仓库,暗中认可情形下,借使您的地方分支与长途分支同名且关联,git push就能够,但倘诺不是你须求加多更加多的东西。小编本地货仓有master和develop分支,远程仓库有master和develop分支,小编本地客栈的develop分支与长途旅舍的master分支关联。所以直接git push是无法的。最棒不要这么干,小编只是测验一下。上边包车型大巴另三个例子是自己将地方酒店的develop分支与长途仓库的develop分支关联并提交修改到长途仓库。
# 命令形式:git push [remote repository name] [remote branch]
git push
fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use

    git push origin HEAD:master

To push to the branch of the same name on the remote, use

    git push origin develop

$ git push origin HEAD:master
Counting objects: 3, done.
Writing objects: 100% (3/3), 296 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:***/Refacor.git
   f7ea1a0..d6813fd  HEAD -> master

$ git branch --set-upstream-to origin/develop
Branch develop set up to track remote branch develop from origin.
$ git status
On branch develop
Your branch is ahead of 'origin/develop' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean
$ git fetch
$ git merge
Already up-to-date.
$ git push
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:***/Refacor.git
   f7ea1a0..d6813fd  develop -> develop

master主分支,develop为付出分支。develop测验牢固后,合併到master分支。

5. Git配置

  • 查阅Git的有所配置音信
# 命令形式:git config --list
$ git config --list
  • 翻开特定的配备消息
# 命令形式:git config --get [variable]

$ git config --get user.name
test

$ git config --global --get user.email
global@test.com
  • 布局客商名和邮件,加上--global是安顿全局的,不然是安插当前仓库的,当前酒店的配置会覆盖全局配置
# 命令形式:git config [--global] [user.name or user.email]

$ git config --global user.name "test global"

$ git config user.name "test"

$ git config --global user.email "global@test.com"

$ git config user.email "local@test.com"

查看全体支行

git branch -a

6. 翻看提交记录

  • 查看提交记录,当前支行的版本历史
#命令形式:git log

$ git log
commit d6813fdf94c37d51e952aee42155654fe333ef28
Author: *** <***@gmail.com>
Date:   Fri Oct 22 17:05:12 2016 +0800

    feat: edit README.md

commit f7ea1a04e423e135d92c4c93e4f188e7257b0cad
Author: *** <***@gmail.com>
Date:   Fri Oct 22 16:39:45 2016 +0800

    feat:develop

commit 68506c5f153fc303fa33ceb733bf15b6da690b00
Merge: 9b7d7c8 f777a9b
Author: *** <***@gmail.com>
Date:   Fri Oct 22 16:25:40 2016 +0800

    feat: test

commit 9b7d7c87c32e144ea1364c1a0aa0fe16f074c8d5
Author: *** <***@gmail.com>
Date:   Fri Oct 22 16:18:18 2016 +0800
  • 显示commit历史,以及历次commit产生更换的文书
# 命令形式:git log --stat

$ git log --stat
commit afa74f2de6caede85b6b4beeefc3ec338ed42986
Author: *** <***@gmail.com>
Date:   Fri Oct 22 17:35:32 2016 +0800

    Update README.md

 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)


commit 9b313361b73cd76505c05e6b6ad3b0c6204357ce
Author: *** <***@gmail.com>
Date:   Fri Oct 22 17:25:28 2016 +0800

    feat: conflict

 README.md | 2 +-
  • 翻看近期的操作新闻
# 命令形式:git reflog

$ git reflog
afa74f2 HEAD@{0}: checkout: moving from master to develop
d6813fd HEAD@{1}: merge refs/remotes/origin/master: Fast-forward
f777a9b HEAD@{2}: checkout: moving from develop to master
afa74f2 HEAD@{3}: pull: Fast-forward
d4c01a6 HEAD@{4}: commit (merge): feat: handle conflict
9b31336 HEAD@{5}: commit: feat: conflict
d6813fd HEAD@{6}: commit: feat: edit README.md
f7ea1a0 HEAD@{7}: commit: feat:develop
68506c5 HEAD@{8}: commit (merge): feat: test
9b7d7c8 HEAD@{9}: commit: feat:branch test
0b05e43 HEAD@{10}: checkout: moving from master to develop
f777a9b HEAD@{11}: pull: Fast-forward
0b05e43 HEAD@{12}: checkout: moving from develop to master
0b05e43 HEAD@{13}: checkout: moving from a to develop
0b05e43 HEAD@{14}: checkout: moving from develop to a
0b05e43 HEAD@{15}: checkout: moving from a to develop
0b05e43 HEAD@{16}: checkout: moving from origin/develop to a
0b05e43 HEAD@{17}: checkout: moving from 0b05e4388c3cb16d23a59f7238d59894c7f1fb8
  • 呈现缓存区和专门的工作区的差别
# 命令形式:git diff

$ git diff
diff --git a/a.txt b/a.txt
index d00491f..cd6c7f8 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1 @@
-1
+1dfa

下载远程分支到本地

#git上已经有master分支 和develop分支#在本地,新建并切换到本地develop分支git checkout -b develop #本地分支与远程分支相关联git pull origin develop#简单命令git checkout -b develop origin/develop

7. 切换分支修改内容

不时你正在某些分支上进打开荒,突然来了两个任务要修改另叁个分段上的剧情,而那时你还不想将眼下支行的原委交给,git stash(保存当前的专门的职业状态)命令就有了用处。

# 查看当前工作状态
$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

  modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

# 命令形式:git stash/git stash save "message",git stash是保存当前的工作进度。会分别对暂存区和工作区的状态进行保存,save "message"是添加的备注信息,可以没有。
$ git stash save "test git stash"
Saved working directory and index state On develop: test git stash
HEAD is now at d6813fd feat: edit README.md

# 命令形式:git stash list,查看当前的
$ git stash list
stash@{0}: On develop: test git stash

# 切换分支,查看状态,修改当前分支内容
$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

# 完成master分支的修改后,恢复stash中的内容,然后继续开发develop
# 命令形式:git stash pop stash@{0},git stash pop是恢复stash中的第一个的内容,有多个时也可以添加索引,形式为stash@{index}
$ git stash pop stash@{0}
On branch develop
Your branch is up-to-date with 'origin/develop'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

  modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
Dropped stash@{0} (b30b83ef5fe3127052a992a6679379f78813208e)

备考:还会有为数不菲没讲到,举例tag,但自个儿备感现成的事物已经丰裕用了,以往必要再去查资料吧。

新建本地分支

git checkout -b myBranch

剔除本地分支

git branch -d localBranch

提交本地分支到长途

git checkout myBranchgit push origin myBranch

切换分支

  • 切换分支时,倘使有未成功的专门的职业,会带到切换的分层。所以切换分支援前线,注意先 git status 查看境况,git stash 缓存,再切换过去。
git status#如果有未commit的操作,先git commit,或者git stash 暂存起来,再切换分支git stashgit checkout master

将Develop分支发表到Master分支的通令

# 切换到Master分支git checkout master# 对Develop分支进行合并git merge --no-ff develop

git pull时提示争持

#先暂存当前修改的代码git stash#拉服务端代码git pull# 放出之前的暂存的代码git stash pop# 进行合并和提交

重命名本地分支

git branch -m oldName newName
  • Git分支管理攻略
  • git之分支切换时互相影响的主题素材
  • AMDliJ IDEA 下的版本调节介绍
  • Idea中Git的施用和三种等级次序的争持消除
  • Eclipse 中Git的采用及怎么着缓和冲突
  • eclipse中git消除争执

本文由365bet体育在线官网发布于网络工程,转载请注明出处:GIT神速入门,Git常用命令总括

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。