git经典常用操作

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

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

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

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

$ pwd
/c/notos/code/gitpractice
74565@jason MINGW64 /c/notos/code/gitpractice
$ git init
Initialized empty Git repository in C:/notos/code/gitpractice/.git/

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

$ git add readme.txt //为什么会有add commit两步骤呢,你可以添加很多次文件然后依次commit warning: 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.txt warning: 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 status
On branch master
nothing to commit, working directory clean
修改readme文件后 git status
On branch master
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.txt
no changes added to commit (use "git add" and/or "git commit -a")
add文件后
$ git status
warning: LF will be replaced by CRLF in readme.txt.
The file will have its original line endings in your working directory.
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)//将提交到暂存区的file 撤回,相当于没有add
 
        modified:   readme.txt

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

$ git diff readme.txt
diff --git a/readme.txt b/readme.txt
index 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 reflog
e47dc70 HEAD@{0}: commit: append GPL
e3c958d HEAD@{1}: commit: add distributed
33dd7af HEAD@{2}: commit (initial): create readme.txt
----------------------------------------------------------------------------
$ git log
commit e47dc706520adc55fd9de97bb911dbd8a406ab19  //commitid 每个人的都不一样
Author: jasondong <745650624@qq.com>
Date:   Mon Apr 2 10:25:45 2018 +0800
 
    append GPL
 
commit e3c958de9f539e0caad6e8a2a845c60defff2940
Author: jasondong <745650624@qq.com>
Date:   Mon Apr 2 10:20:28 2018 +0800
 
    add distributed
 
commit 33dd7afe64ff9e73688de94a1fc61f1b8966207d
Author: jasondong <745650624@qq.com>
Date:   Mon Apr 2 09:58:34 2018 +0800
 
    create readme.txt

 7.git版本回退

$ git reflog //版本回退需要知道commitid的前几位(前七位),所以需要先查看commitid
e47dc70 HEAD@{0}: reset: moving to e47dc70
33dd7af HEAD@{1}: reset: moving to 33dd7af
e47dc70 HEAD@{2}: commit: append GPL
e3c958d HEAD@{3}: commit: add distributed
33dd7af HEAD@{4}: commit (initial): create readme.txt
74565@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 status
On branch master
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.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.txt
Git is a distributed version control system.
 
Git is free software.

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

现在修改了文件,而且进行了 git add
git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage) // 用这个命令即可从暂存区撤销add
        modified:   readme.txt git reset HEAD readme.txt
Unstaged changes after reset:
 
M       readme.txt

 10.删除版本库中文件

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

git rm --cached从索引中删除文件。但是本地文件还存在, 只是不希望这个文件被版本控制。

 11.添加远程仓库并push

$ git remote add origin git@github.com:jasondong-1/gitpractice.git // 添加远程仓库,一般给远程仓库命名为origin
74565@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)? yes
Warning: 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 -> master
Branch 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
  master

git checkout -b dev 这条命令可以用一下两条命令替代
$ git branch dev //创建分支
$ git checkout dev //切换分支

 14.合并分支

 
//在新分支上修改文件并提交 git add readme.txt 74565@jason MINGW64 /c/notos/code/gitpractice (dev) git commit -m "branch test" [dev 84a8f86] branch test 1 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..84a8f86 Fast-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 feature1 Auto-merging readme.txt CONFLICT (content): Merge conflict in readme.txt Automatic 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

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 stash Saved working directory and index state WIP on dev: b5e0aa9 new file hello.txt HEAD is now at b5e0aa9 new file hello.txt 74565@jason MINGW64 /c/notos/code/gitpractice (dev) $ git stash list stash@{0}: WIP on dev: b5e0aa9 new file hello.txt 74565@jason MINGW64 /c/notos/code/gitpractice (dev) $ git stash pop On branch dev 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: 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 remote
origin
74565@jason MINGW64 /c/notos/code/gitpractice (dev)
$ git remote -v
origin  git@github.com:jasondong-1/gitpractice.git (fetch)
origin  git@github.com:jasondong-1/gitpractice.git (push)

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

语法:$ git push <远程主机名> <本地分支名>:<远程分支名> 若省略远程分支,代表将本地当前分支推送到远端对应分支
git push origin master // 推送到远端的master git 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 do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.// git建议push之前先pull
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

$ git pull
There 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 <remote> <branch> //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/<branch> 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 <tagname>可以推送一个本地标签;
git push origin --tags可以推送全部未推送过的本地标签;
git tag -d <tagname>可以删除一个本地标签;
git push origin :refs/tags/<tagname>可以删除一个远程标签。

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

# Python:
*.py[cod]
*.so
*.egg
*.egg-info
dist
build

28.git删除本地仓库

打开gitbash查看项目目录:

Arenas@Leon-T460s MINGW64 ~/Documents/Python Projects
ls
 LeonDjango/   Netmiko/  'Python Code'/   README.md

Arenas@Leon-T460s MINGW64 ~/Documents/Python Projects cd LeonDjango/

Arenas@Leon-T460s MINGW64 ~/Documents/Python Projects/LeonDjango (master)
$ 

删除本地仓库:

Arenas@Leon-T460s MINGW64 ~/Documents/Python Projects/LeonDjango (master)
$ rm .git -rf

已删除:

Arenas@Leon-T460s MINGW64 ~/Documents/Python Projects/LeonDjango
$

Git快速克隆项目

 git config --global user.name "Leon.Zhang"
 git config --global user.email "37988538@qq.com"
 git clone https://gitee.com/ccieleon/{project}.git
 git config --global credential.helper store
 git remote add origin https://gitee.com/ccieleon/{project}.git
 git remote -v
 git push -u origin master
 git pull

最终效果:
 [root@NTOP-NG project]# git config --list
 user.name=Leon.Zhang
 user.email=ccieleon@qq.com
 credential.helper=store
 core.repositoryformatversion=0
 core.filemode=true
 core.bare=false
 core.logallrefupdates=true
 remote.origin.url=https://gitee.com/ccieleon/{project}.git
 remote.origin.fetch=+refs/heads/:refs/remotes/origin/
 branch.master.remote=origin
 branch.master.merge=refs/heads/master

发表回复