博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
git常用操作
阅读量:4583 次
发布时间:2019-06-09

本文共 9907 字,大约阅读时间需要 33 分钟。

这是一个极为简单的git笔记,只包含相关命令

1.git安装后自报家门,姓名和邮箱

$ git config --global user.name "Your Name"$ git config --global user.email "email@example.com"

2.创建工作区,初始化工作区

$ pwd/c/notos/code/gitpractice74565@jason MINGW64 /c/notos/code/gitpractice$ git initInitialized empty Git repository in C:/notos/code/gitpractice/.git/

 3添加新文件并添加到缓存区,并commit

$ git add readme.txt //为什么会有add commit两步骤呢,你可以添加很多次文件然后依次commitwarning: LF will be replaced by CRLF in readme.txt.The file will have its original line endings in your working directory.74565@jason MINGW64 /c/notos/code/gitpractice (master)$ git commit -m "create readme.txt" // -m 是对本次提交的描述,最好是有意义的[master (root-commit) 33dd7af] create readme.txtwarning: LF will be replaced by CRLF in readme.txt.The file will have its original line endings in your working directory.1 file changed, 2 insertions(+)create mode 100644 readme.txt

 4.查看git 状态

刚提交完时查询$ git statusOn branch masternothing to commit, working directory clean修改readme文件后$ git statusOn branch masterChanges not staged for commit:  (use "git add 
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) //撤回修改,其实使用版本库中的版本替换工作区版本 modified: readme.txtno changes added to commit (use "git add" and/or "git commit -a")add文件后$ git statuswarning: LF will be replaced by CRLF in readme.txt.The file will have its original line endings in your working directory.On branch masterChanges to be committed: (use "git reset HEAD
..." to unstage)//将提交到暂存区的file 撤回,相当于没有add modified: readme.txt

 5.比较工作区修改前后文件,修改未add

$ git diff readme.txtdiff --git a/readme.txt b/readme.txtindex 46d49bf..9247db6 100644--- a/readme.txt+++ b/readme.txt@@ -1,2 +1,2 @@-Git is a version control system.+Git is a distributed version control system.Git is free software.warning: LF will be replaced by CRLF in readme.txt.The file will have its original line endings in your working directory.

 6.git查看log

$ git refloge47dc70 HEAD@{
0}: commit: append GPLe3c958d HEAD@{
1}: commit: add distributed33dd7af HEAD@{
2}: commit (initial): create readme.txt----------------------------------------------------------------------------$ git logcommit e47dc706520adc55fd9de97bb911dbd8a406ab19 //commitid 每个人的都不一样Author: jasondong <745650624@qq.com>Date: Mon Apr 2 10:25:45 2018 +0800 append GPL commit e3c958de9f539e0caad6e8a2a845c60defff2940Author: jasondong <745650624@qq.com>Date: Mon Apr 2 10:20:28 2018 +0800 add distributed commit 33dd7afe64ff9e73688de94a1fc61f1b8966207dAuthor: jasondong <745650624@qq.com>Date: Mon Apr 2 09:58:34 2018 +0800 create readme.txt

 7.git版本回退

$ git reflog //版本回退需要知道commitid的前几位(前七位),所以需要先查看commitide47dc70 HEAD@{
0}: reset: moving to e47dc7033dd7af HEAD@{
1}: reset: moving to 33dd7afe47dc70 HEAD@{
2}: commit: append GPLe3c958d HEAD@{
3}: commit: add distributed33dd7af HEAD@{
4}: commit (initial): create readme.txt74565@jason MINGW64 /c/notos/code/gitpractice (master)$ git reset --hard e3c958d //假如要会退到第二版本HEAD is now at e3c958d add distributed //git 只需简单的吧指针指向老的版本即可,所以速度很快

 8. 取消工作区修改,实际上使用版本库中文件替换 当前文件内容

在readme 中添加如下内容Git is a distributed version control system.Git is free software.+My stupid boss still prefer to svn. //新添加内容-----------------------------------------------------$ git statusOn branch masterChanges not staged for commit:  (use "git add 
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) // 取消修改的命令 modified: readme.txt no changes added to commit (use "git add" and/or "git commit -a")$ git checkout -- readme.txt 74565@jason MINGW64 /c/notos/code/gitpractice (master)$ cat readme.txtGit is a distributed version control system. Git is free software.

 9.取消暂存区文件的修改

现在修改了文件,而且进行了 git add$ git statusOn branch masterChanges to be committed:  (use "git reset HEAD 
..." to unstage) // 用这个命令即可从暂存区撤销add modified: readme.txt$ git reset HEAD readme.txtUnstaged changes after reset: M readme.txt

 10.删除版本库中文件

git rm test.txt  //git rm 和git add 其实是等效的,都是对暂存区的操作git commit -m "rm test.txt"

 11.添加远程仓库并push

$ git remote add origin git@github.com:jasondong-1/gitpractice.git // 添加远程仓库,一般给远程仓库命名为origin74565@jason MINGW64 /c/notos/code/gitpractice (master)$ git push -u origin master//第一次向远程仓库提交加 -u参数The authenticity of host 'github.com (13.250.177.223)' can't be established.RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.Counting objects: 10, done.Delta compression using up to 4 threads.Compressing objects: 100% (7/7), done.Writing objects: 100% (10/10), 870 bytes | 0 bytes/s, done.Total 10 (delta 1), reused 0 (delta 0)remote: Resolving deltas: 100% (1/1), done.To git@github.com:jasondong-1/gitpractice.git* [new branch]      master -> masterBranch master set up to track remote branch master from origin.

 12.从远程仓库克隆

$ git clone git@github.com:jasondong-1/gitpractice.git // 后面也可再跟一个名字,即重命名git库

 13.创建新的分支

$ git checkout -b dev // Switched to a new branch 'dev'74565@jason MINGW64 /c/notos/code/gitpractice (dev) $ git branch //查看git下所有分支 ,*表示当前分支 * dev  mastergit checkout -b dev 这条命令可以用一下两条命令替代$ git branch dev //创建分支$ git checkout dev //切换分支

 14.合并分支

//在新分支上修改文件并提交$ git add readme.txt74565@jason MINGW64 /c/notos/code/gitpractice (dev)$ git commit -m "branch test"[dev 84a8f86] branch test1 file changed, 1 insertion(+)$ git checkout master //首先切换回master分支Already on 'master'Your branch is up-to-date with 'origin/master'. 74565@jason MINGW64 /c/notos/code/gitpractice (master)$ git branch  dev* master 74565@jason MINGW64 /c/notos/code/gitpractice (master)$ git merge dev // 合并得知 分支到当前分支    git merge --no-ff -m "merge with no-ff" dev  采用非fast-forward方式合并分支Updating 4b00677..84a8f86Fast-forward readme.txt | 1 +  1 file changed, 1 insertion(+)

 15.删除分支

$ git branch -d dev //删除分支Deleted branch dev (was 84a8f86).74565@jason MINGW64 /c/notos/code/gitpractice (master)$ git branch* master

 16.解决冲突

假如在一个新的分支上修改了readme并进行提交,master上也对readme进行了修改,而且修改的是同一行,也commit了,那么合并新分支到master时会报错$ git merge feature1Auto-merging readme.txtCONFLICT (content): Merge conflict in readme.txtAutomatic merge failed; fix conflicts and then commit the result.//自动合并失败,手动解决冲突后,重新提交手动解决冲突航班重新提交 git add readme.txt 74565@jason MINGW64 /c/notos/code/gitpractice (master|MERGING)$ git commit -m "fixed conflict"[master 4cdbdc4] fixed conflict

 17.查看分支合并图

$ git log --graph

 18.分支策略

分支策略在实际开发中,我们应该按照几个基本原则进行分支管理:首先,master分支应该是非常稳定的,也就是仅用来发布新版本,平时不能在上面干活;那在哪干活呢?干活都在dev分支上,也就是说,dev分支是不稳定的,到某个时候,比如1.0版本发布时,再把dev分支合并到master上,在master分支发布1.0版本;你和你的小伙伴们每个人都在dev分支上干活,每个人都有自己的分支,时不时地往dev分支上合并就可以了。

 19.git stash 隐藏,必须把修改的文件进行 git add 后 才可以 gits tash

$ git stashSaved working directory and index state WIP on dev: b5e0aa9 new file hello.txtHEAD is now at b5e0aa9 new file hello.txt 74565@jason MINGW64 /c/notos/code/gitpractice (dev)$ git stash liststash@{
0}: WIP on dev: b5e0aa9 new file hello.txt 74565@jason MINGW64 /c/notos/code/gitpractice (dev)$ git stash popOn branch devChanges not staged for commit: (use "git add
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: hello.txt no changes added to commit (use "git add" and/or "git commit -a")Dropped refs/stash@{
0} (a3e21e6f0042b48f4160e886cf6f79f346f15543) 74565@jason MINGW64 /c/notos/code/gitpractice (dev)$ git stash list 74565@jason MINGW64 /c/notos/code/gitpractice (dev)

 20.git 查看远程仓库信息

$ git remoteorigin74565@jason MINGW64 /c/notos/code/gitpractice (dev)$ git remote -vorigin  git@github.com:jasondong-1/gitpractice.git (fetch)origin  git@github.com:jasondong-1/gitpractice.git (push)

 21.推送分支(会推送到远端对应的分支上)

语法:$ git push 
<远程主机名>
<本地分支名>
:
<远程分支名>
若省略远程分支,代表将本地当前分支推送到远端对应分支 git push origin master // 推送到远端的mastergit push origin dev //推送到远端的dev什么样的分支需要推送到远端?我认为凡是需要大家共享的分支就需要push到远端

 22. 从远程仓库克隆

$ git clone git@github.com:jasondong-1/gitpractice.git //git clone 默认只会把远端的master克隆下来$ git branch* master

 23.获取远端其他分支

$ git checkout -b dev origin/dev //现在有了对应的dev分支$ git branch* dev  master

 24.两人同时修改了同一份文件向远端push冲突的解决

$ git push origin dev //git不知道该用哪一份文件To git@github.com:jasondong-1/gitpractice.git! [rejected]        dev -> dev (fetch first)error: failed to push some refs to 'git@github.com:jasondong-1/gitpractice.git'hint: Updates were rejected because the remote contains work that you dohint: not have locally. This is usually caused by another repository pushinghint: to the same ref. You may want to first integrate the remote changeshint: (e.g., 'git pull ...') before pushing again.// git建议push之前先pullhint: See the 'Note about fast-forwards' in 'git push --help' for details.$ git pullThere is no tracking information for the current branch.Please specify which branch you want to merge with.See git-pull(1) for details.     git pull 
//git pull 时报告没有tracking information,可以用这句pull If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=origin/
dev$ git pull origin dev解决冲突,并重新add commit

 25.git打标签(人类可以识别的标志)

git tag v0.1 33dd7af //33dd7af是commit-id或$ git tag -a v0.2 -m "stable edition" e47dc70查看 tag git tag

 26.操作标签

git push origin 
可以推送一个本地标签;git push origin --tags可以推送全部未推送过的本地标签;git tag -d
可以删除一个本地标签;git push origin :refs/tags/
可以删除一个远程标签。

 27..gitignore 可以忽略.gitignore 文件中的文件

# Python:*.py[cod]*.so*.egg*.egg-infodistbuild

 

转载于:https://www.cnblogs.com/jason-dong/p/8848350.html

你可能感兴趣的文章
写log
查看>>
Python基础 ----- 流程控制
查看>>
选择语言之switch case
查看>>
实现斐波那契神兔
查看>>
Understanding Paxos Algorithm
查看>>
springboot+Zookeeper+Dubbo入门
查看>>
【linux就该这么学】-08
查看>>
JavaScript基础知识汇总
查看>>
PyQt4网格布局
查看>>
PHP学习笔记 - 进阶篇(3)
查看>>
极角排序那些事
查看>>
Ganglia+nagios 监控hadoop资源与报警
查看>>
asterisk事件监控
查看>>
博客园主题样式修改教程
查看>>
apache commons-email1.3使用
查看>>
Linux学习 - 压缩解压命令
查看>>
Objective-C和C++的区别
查看>>
C#基础-第6章:类型和成员基础
查看>>
TextView实现多个TextView对象的走马灯效果
查看>>
感悟成功
查看>>