设置SSH Key密匙 以后push不用输入账户密码
-ssh-keygen -t rsa -C "账户邮箱" $ ssh-keygen -t rsa -C "771058736@qq.com" Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/ Administrator/.ssh/id_rsa): /c/Users/Administrator/.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/ Administrator/.ssh/id_rsa. //私匙 Your public key has been saved in /c/Users/ Administrator/.ssh/id_rsa.pub. //公匙 The key fingerprint is: SHA256:gG1231A4C6nBA2323213f1t7od3U/131 771058736@qq.com The key‘s randomart image is: +---[RSA 2048]----+ |=o oo. | |+.. o . o o. | |*.+.. * . =.+.| |=+.+.+ o . . oO o| |.oo=. . S ...o.+o| |..+ . . E. .+| | . . o.+| | oo| | | +----[SHA256]-----+ -$ ssh -T git@github.com //测试是否认证成功 Enter passphrase for key ‘/c/Users/Administrator/.ssh/ id_rsa‘: Hi zhangzn3! You‘ve successfully authenticated, but GitHub does not provide shell access.
git clone <版本库的地址>
$ git clone https://github.com/daerduomao/test.git Cloning into ‘test‘... warning: You appear to have cloned an empty repository.
git clone <版本库的地址> <本地目录名>
$ git clone https://github.com/daerduomao/test.git halo Cloning into ‘halo‘... warning: You appear to have cloned an empty repository.
git add <需要添加的文件> 将文件添加到暂存区域(tracked area)
-git add path 添加指定的目录文件,path为"."则表示当前目录文件 -git add -u 只将tracked文件中被修改过或已删除文件的信息添加到索引库。它不会处理untracted的文件
git commit -m "描述信息" 将索引内容提交到本地仓库中
-git commit -m "描述信息" 一般要先git add,然后再git commit, -git commmit -a -m "描述信息" 如果只是提交被修改或者被删除的且已经被git管理(就是已经加入到索引的)文件,可以不用先git add,直接使用 -a参数进行commit,相当于合并了 git add $ git commit -a -m‘modify sample.txt‘ [master bcecc6f] modify sample.txt 1 file changed, 1 insertion(+)
git branch 分支操作
-git branch 查看本地分支,当前分支的前面加“*”号标记 -git branch -r 查看远程分支 -git branch -a 查看本地及远程分支 -git branch -d local-branch 删除本地分支 $ git branch -d origin Deleted branch origin (was 714e9b8).
git push 推送操作
-git push <远程主机名> <本地分支名>:<远程分支名> $ git push origin b2:b2 推送本地b2分支到origin主机的b2分支 Username for ‘https://github.com‘: zhangzn3 Total 0 (delta 0), reused 0 (delta 0) To https://github.com/zhangzn3/test.git * [new branch] b2 -> b2 -git push origin local-branch 这里省略远程分支名,表示推送本地分支local-branch到origin主机同名分支,如果没有同名的远程分支,则新建一个分支 -git push origin :remote/branch 这里省略本地分支名,表示推送一个空分支到远程分支,相当于删除该远程分支 $ git push origin :b1 Username for ‘https://github.com‘: zhangzn3 To https://github.com/zhangzn3/test.git - [deleted] b1 -git push origin --all 推送本地所有分支到远程主机 $ git push origin --all Username for ‘https://github.com‘: zhangzn3 Total 0 (delta 0), reused 0 (delta 0) To https://github.com/zhangzn3/test.git * [new branch] b1 -> b1 * [new branch] b2 -> b2 * [new branch] b5 -> b5
git pull 更新操作
-git pull <远程主机名> <远程分支名>:<本地分支名> 注:跟git push 远程分支:本地分支参数位置相反 $ git pull origin b2:b2 Already up-to-date. -git pull origin remote-branch 这里省略本地分支,表示从远程分支更新数据到当前分支 $ git pull origin b2 remote: Counting objects: 3, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/zhangzn3/test * branch b2 -> FETCH_HEAD a7dc974..6082709 b2 -> origin/b2 Updating a7dc974..6082709 Fast-forward sample2.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
git checkout 检出操作
-git checkout branch-name 检出(切换)分支 -git checkout -b branch-name 新建分支并切换到该分支 $ git checkout -b b5 Switched to a new branch ‘b5‘ $ git checkout - //返回上一个分支 Switched to branch ‘b1‘ Your branch is ahead of ‘origin/b1‘ by 9 commits. (use "git push" to publish your local commits)
手动建立跟踪关系(--set-upstream),这样可以在push pull时省略本地分支和远程分支参数
-git branch --set-upstream local-branch remote-branch 关联本地分支和远程分支 $ git branch --set-upstream b2 origin/b2 The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to Branch b2 set up to track remote branch b2 from origin. $ git push Username for ‘https://github.com‘: daerduomao Counting objects: 2, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 227 bytes | 0 bytes/s, done. Total 2 (delta 0), reused 0 (delta 0) To https://github.com/zhangzn3/test.git d600893..ff30992 b2 -> b2
git stash 储藏操作
-git stash 储藏当前修改 -git stash list 查看储藏的列表信息 -git stash pop 或者 git stash apply 还原上一次的储藏 -git stash pop | apply stash@{idx} 还原到指定的储藏 -git stash clear 清空储藏列表 当前分支内容发生修改,这时候需要切换到其他分支工作,git会提示切换之前有两个选择:先commit或者stash当前分支修改的内容,如果不想commit,可以使用stash将修改储藏起来,等再切换回来时可以再还原回来以前修改的内容. $ git checkout b1 切换时出现错误提示 error: Your local changes to the following files would be overwritten by checkout:3.txt Please commit your changes or stash them before you switch branches. Aborting $ git stash 执行储藏操作 Saved working directory and index state WIP on b2: ff30992 3 HEAD is now at ff30992 3 $ git checkout b1 这个时候可以切换成功了 Switched to branch ‘b1‘ Your branch is up-to-date with ‘origin/b1‘. $ git stash apply 再切换回到b2时,可以还原到以前储藏的内容 On branch b2 Your branch is up-to-date with ‘origin/b2‘. Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: 3.txt no changes added to commit (use "git add" and/or "git commit -a")
git status 查看仓库的状态
-git status 查看状态信息 有以下几种状态 $ git status On branch b1 Your branch and ‘origin/b1‘ have diverged, and have 8 and 1 different commits each, respectively. (use "git pull" to merge the remote branch into yours) You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Changes to be committed: //新提交的文件 new file: b4.txt Unmerged paths: //未合并的文件 (use "git add ..." to mark resolution) both modified: b1.txt //有修改, 但是没有被添加到暂存区的文件 Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: b2.txt Untracked files: //没有加入版本控制的文件 没有add的文件 (use "git add ..." to include in what will be committed) b3.txt
git log 查看提交日志
-git log $ git log commit 5747b585fda93a0d1cc84e0f9c6b3f5924b78973 Author: zhangzn3 <619025283@qq.com> Date: Wed Oct 25 14:41:29 2017 +0800 $ git log --pretty=oneline 显示简述信息 5747b585fda93a0d1cc84e0f9c6b3f5924b78973 111 f091b8f2f2cec797e3d2c40cbfcf3f14a589b34c 12121212 073a0a22d7a4cc57ff0e459753ce81a8a62dc124 Merge branch ‘b2‘ into b1 -git log xx.txt 只显示指定文件的日志 -git log -p 显示文件的改动 -git log -p b2.txt 显示指定文件的改动 $ git log -p b2.txt commit 8d7c3e6be5a7a46cc3b598c81bb53c95acce825a Author: zhangzn3 <619025283@qq.com> Date: Wed Oct 25 10:38:21 2017 +0800 b2 diff --git a/b2.txt b/b2.txt index 76fd2b8..7c229bf 100644 --- a/b2.txt +++ b/b2.txt @@ -1 +1 @@ -b2 \ No newline at end of file +b2b2b2 \ No newline at end of file -git log --graph 以图表形式查看分支
git diff 查看工作树和暂存区前后的差别
$ git diff diff --cc b1.txt index 9db4b82,93ee6ac..0000000 deleted file mode 100644,100644 --- a/b1.txt +++ /dev/null diff --git a/33/66.txt b/33/66.txt deleted file mode 100644 index e69de29..0000000 diff --git a/b2.txt b/b2.txt deleted file mode 100644 index 7c229bf..0000000 --- a/b2.txt +++ /dev/null @@ -1 +0,0 @@ -b2b2b2 \ No newline at end of file diff --git a/b4.txt b/b4.txt deleted file mode 100644 index e69de29..0000000
git diff 查看工作树和最新提交前后的差别 HEAD指向当前分支中最新一次提交,最好每次commit之前先执行该命令查 看本次提交和上次提交有什么差别 git reset 回溯历史版本
$ git log --pretty=oneline 先查看提交日志 5747b585fda93a0d1cc84e0f9c6b3f5924b78973 111 f091b8f2f2cec797e3d2c40cbfcf3f14a589b34c 12121212 073a0a22d7a4cc57ff0e459753ce81a8a62dc124 Merge branch ‘b2‘ into b1 0a6eb13c1a429f1de55229bd31461d8056a022cd 1212 $ git reset --hard 5747b5 版本hash //回溯到某个版本 HEAD is now at 5747b58 111
conflict冲突解决
$ 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 https://github.com/zhangzn3/test 4319f32..89ab451 b1 -> origin/b1 Auto-merging b1.txt CONFLICT (content): Merge conflict in b1.txt //出现冲突 Automatic merge failed; fix conflicts and then commit the result. //自动合并失败 //打开有冲突的文件 <<<<<<< HEAD //当前的内容 我是哈哈哈 ======= 12121214234234234212 //要合并的内容 >>>>>>> 89ab451b10d42ed559dba38950b2665ccc2eb64b //解决冲突后进行add和commit解决结果 $ git commit -a -m‘resolve conflict‘ [b1 9c4d232] resolve conflict