Git常用命令实践总结

来源:互联网 发布:阿里云centos图形界面 编辑:程序博客网 时间:2024/06/07 15:15

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Git仓库的创建

  • 在当前目录新建一个Git仓库
# 命令形式:git init$ cd TestGit$ git initInitialized empty Git repository in /Users/***/TeskGit/.git/
  • 在指定目录下新建一个Git仓库
# 命令形式:git init [Directory Name]$ git init TestGitInitialized 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 masterCloning into 'Refacor'...Saving password to keychain failedIdentity added: /Users/**/.ssh/id_rsa_github ((null))remote: Counting objects: 3, done.remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0Receiving objects: 100% (3/3), done.Checking connectivity... done.

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 remoteorigin$ git remote -vorigin  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 fetchremote: Counting objects: 3, done.remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0Unpacking 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)

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

  • 创建本地分支并切换到新创建的分支,-b代表新创建一个分支,-B用在本地分支已经存在的情况下,强行创建一个新分支并将原来的分支覆盖,checkout主要是分支切换,创建分支使用branch命令
# 命令形式:git checkout [-b or -B] [local branch name]$ git checkout -b aSwitched to a new branch 'a'$ git checkout -b afatal: A branch named 'a' already exists.$ git checkout -B aSwitched to and reset branch 'a'
  • 切换本地分支
# 命令形式:git checkout [local branch name]$ git checkout developSwitched 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 testDeleted branch test (was 0b05e43).
  • 创建本地分支并与远端分支关联
# 命令形式:git checkout -b [local branch name] origin/[remote branch name]$ git checkout -b develop origin/developBranch develop set up to track remote branch develop from origin.Switched to a new branch 'develop'$ git checkout -b master origin/masterBranch 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/developBranch 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 developTo git@github.com:***/Refacor.git - [deleted]         develop$ git branch -dr origin/developDeleted remote-tracking branch origin/develop (was d6813fd).

注:本地可以有多个分支,远程也可以有多个分支,本地多个分支可以关联远程多个分支,但是,本地分支最好与远程分支同名,以免出现问题。

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

向远程仓库提交内容之前,要理解三个概念:本地文件,缓冲区,本地仓库。平常修改的文件内容都是本地文件,在往远程仓库提交之前先要提交到缓冲区,再从缓冲区提交到本地仓库,然后本地仓库才能往远程仓库提交。本地的内容分为三大部分,它们是相互独立的,理解了这四个概念就明白为什么要执行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 statusOn branch developYour branch is up-to-date with 'origin/develop'.nothing to commit, working directory clean$ git fetch
  • 将从远程仓库取下的内容与本地仓库的内容进行合并,合并之前必须将本地的修改提交到本地仓库。
# 命令形式:git merge# 正常情况下$ git meregeAlready up-to-date.# 没有提交修改到本地仓库的情况下$ git mergeUpdating d6813fd..2444abcerror: Your local changes to the following files would be overwritten by merge:    README.mdPlease, commit your changes or stash them before you can merge.Aborting# 本地仓库与取下的远程仓库内容有冲突的情况下$ git mergeAuto-merging README.mdCONFLICT (content): Merge conflict in README.mdAutomatic merge failed; fix conflicts and then commit the result.
  • 解决冲突,冲突是使用git时常常会碰到的情况,冲突解决主要是在将本地仓库内容与远程仓库取下的内容merge后,进入merge后的文件进行修改,将冲突解决,然后重新add,commit,push即可。

冲突内容:

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

修改后的内容:

# Refacorbranch develop

处理过程:

$ vim README.md $ git add .$ git commit -m "feat: handle conflict"[develop d4c01a6] feat: handle conflict$ git pushCounting 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 pullremote: Counting objects: 3, done.remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0Unpacking objects: 100% (3/3), done.From github.com:***/Refacor   d4c01a6..afa74f2  develop    -> origin/developUpdating d4c01a6..afa74f2Fast-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 pushfatal: The upstream branch of your current branch does not matchthe name of your current branch.  To push to the upstream branchon the remote, use    git push origin HEAD:masterTo push to the branch of the same name on the remote, use    git push origin develop$ git push origin HEAD:masterCounting 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/developBranch develop set up to track remote branch develop from origin.$ git statusOn branch developYour 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 mergeAlready up-to-date.$ git pushTotal 0 (delta 0), reused 0 (delta 0)To git@github.com:***/Refacor.git   f7ea1a0..d6813fd  develop -> develop

5. Git配置

  • 查看Git的所有配置信息
# 命令形式:git config --list$ git config --list
  • 查看特定的配置信息
# 命令形式:git config --get [variable]$ git config --get user.nametest$ git config --global --get user.emailglobal@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"

6. 查看提交记录

  • 查看提交记录,当前分支的版本历史
#命令形式:git log$ git logcommit d6813fdf94c37d51e952aee42155654fe333ef28Author: *** <***@gmail.com>Date:   Fri Oct 22 17:05:12 2016 +0800    feat: edit README.mdcommit f7ea1a04e423e135d92c4c93e4f188e7257b0cadAuthor: *** <***@gmail.com>Date:   Fri Oct 22 16:39:45 2016 +0800    feat:developcommit 68506c5f153fc303fa33ceb733bf15b6da690b00Merge: 9b7d7c8 f777a9bAuthor: *** <***@gmail.com>Date:   Fri Oct 22 16:25:40 2016 +0800    feat: testcommit 9b7d7c87c32e144ea1364c1a0aa0fe16f074c8d5Author: *** <***@gmail.com>Date:   Fri Oct 22 16:18:18 2016 +0800
  • 显示commit历史,以及每次commit发生变更的文件
# 命令形式:git log --stat$ git log --statcommit afa74f2de6caede85b6b4beeefc3ec338ed42986Author: *** <***@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 9b313361b73cd76505c05e6b6ad3b0c6204357ceAuthor: *** <***@gmail.com>Date:   Fri Oct 22 17:25:28 2016 +0800    feat: conflict README.md | 2 +-
  • 查看最近的操作信息
# 命令形式:git reflog$ git reflogafa74f2 HEAD@{0}: checkout: moving from master to developd6813fd HEAD@{1}: merge refs/remotes/origin/master: Fast-forwardf777a9b HEAD@{2}: checkout: moving from develop to masterafa74f2 HEAD@{3}: pull: Fast-forwardd4c01a6 HEAD@{4}: commit (merge): feat: handle conflict9b31336 HEAD@{5}: commit: feat: conflictd6813fd HEAD@{6}: commit: feat: edit README.mdf7ea1a0 HEAD@{7}: commit: feat:develop68506c5 HEAD@{8}: commit (merge): feat: test9b7d7c8 HEAD@{9}: commit: feat:branch test0b05e43 HEAD@{10}: checkout: moving from master to developf777a9b HEAD@{11}: pull: Fast-forward0b05e43 HEAD@{12}: checkout: moving from develop to master0b05e43 HEAD@{13}: checkout: moving from a to develop0b05e43 HEAD@{14}: checkout: moving from develop to a0b05e43 HEAD@{15}: checkout: moving from a to develop0b05e43 HEAD@{16}: checkout: moving from origin/develop to a0b05e43 HEAD@{17}: checkout: moving from 0b05e4388c3cb16d23a59f7238d59894c7f1fb8
  • 显示缓存区和工作区的差异
# 命令形式:git diff$ git diffdiff --git a/a.txt b/a.txtindex d00491f..cd6c7f8 100644--- a/a.txt+++ b/a.txt@@ -1 +1 @@-1+1dfa

7. 切换分支修改内容

有时候你正在某个分支上进行开发,突然来了一个任务要修改另一个分支上的内容,而此时你还不想将当前分支的内容提交,git stash(保存当前的工作状态)命令就有了用处。

# 查看当前工作状态$ git statusOn branch developYour 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.mdno 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 stashHEAD is now at d6813fd feat: edit README.md# 命令形式:git stash list,查看当前的$ git stash liststash@{0}: On develop: test git stash# 切换分支,查看状态,修改当前分支内容$ git checkout masterSwitched to branch 'master'Your branch is up-to-date with 'origin/master'.$ git statusOn branch masterYour 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 developYour 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.mdno changes added to commit (use "git add" and/or "git commit -a")Dropped stash@{0} (b30b83ef5fe3127052a992a6679379f78813208e)

备注:还有很多没讲到,例如tag,但我感觉现有的东西已经足够用了,以后需要再去查资料吧。

2 0
原创粉丝点击