git config - - global user.name ‘NetworkCavalry’
git config - - global user.email ‘[email protected]’
git config - - list - - local | global | systemW
cd 项目代码所在文件夹
git init
cd 某个文件夹
git init your_project #会在当前路径下创建和项目名称同名的文件夹
cd your_project
1.首先需要将该文件加入到Git的暂存区中,不然无法提交
git add README.md
git add -help 查看参数详解
git status 查看当前项目的状态
git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: README.md
Untracked files:
(use "git add ..." to include in what will be committed)
a.txt
git commit -m ‘add’ 输入提交消息
git commit -a 省略 git add fileName的操作,一步到位W
git log 查看提交的记录
git log
commit d659e047f41fc22fd2b94e693dd579ca461aae98 (HEAD -> master)
Author: lwj <[email protected]>
Date: Sat Aug 24 01:26:06 2019 +0800
'add'
git commit - -amend 修改最近一次commit的message
git rebase -i 639d308226fb9 修改639d308226fb9的下一个版本
$ git log
commit 1cf43df41d4e377bef8b117688235856437402d3 (HEAD -> master)
Author: lwj <[email protected]>
Date: Sat Aug 24 15:22:31 2019 +0800
second
commit 639d308226fb93e8d32bdff898d3b7c12d9a9c80
Author: lwj <[email protected]>
Date: Sat Aug 24 11:46:45 2019 +0800
edit file name
$ git rebase -i 639d308226fb9
$ git log
commit 118e3a111d270e23662940a28fca0104714c303b (HEAD -> master)
Author: lwj <[email protected]>
Date: Sat Aug 24 15:22:31 2019 +0800
second
third
commit 639d308226fb93e8d32bdff898d3b7c12d9a9c80
Author: lwj <[email protected]>
Date: Sat Aug 24 11:46:45 2019 +0800
edit file name
git rebase -i 639d308226fb9 修改639d308226fb9的下一个版本
git rebase - -continue 完成间隔commmit的合并
git rebase - -abort 如果发生错误,使用该命令可以回到合并之前
git stash 将当前所做操作临时保存
git stash list 查看临时保存的集合
git stash apply 只取出list中保存的,不删除
git stash pop 取出并删除list中的保存的
git mv fileName newFileName
$ git status
On branch master
Changes to be committed:
(use "git restore --staged ..." to unstage)
renamed: first.txt -> second.txt
git rm fileName
git branch 查看分支
git branch -v ,—verbose 显示分支的详细信息
git branch -a ,—all 列出远程跟踪和本地分支
git checkout 分支名 切换分支
git branch -D 分支名 删除分支
git checkout 版本号 detached head 分离头指针,做临时开发,切换分支后需要手动保存,不然会被git清除
$ git status
HEAD detached at ae1c274 头指针分离于 ae1c274版本
Untracked files:
(use "git add ..." to include in what will be committed)
detachedHeadTest.txt
nothing added to commit but untracked files present (use "git add" to track)
$ git commit -m'detachedHeadTest'
$ git log
commit 0e12fac304482a99b5e8b4b8955325dbb359a168 (HEAD)
之前都是 HEAD -> master/branchName
Author: networkcavalry <[email protected]>
Date: Sat Aug 24 18:15:13 2019 +0800
detachedHeadTest
commit ae1c27434fe48fd319ec2183ee717485c816656a (tmp)
Author: networkcavalry <[email protected]>
Date: Sat Aug 24 17:11:20 2019 +0800
treetest
...
$ git checkout master
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
0e12fac detachedHeadTest
If you want to keep it by creating a new branch, this may be a good time
to do so with:
git branch 0e12fac
Switched to branch 'master'
git checkout -b 分支名 版本号 从指定版本创建一个分支
$ git checkout -b temp 639d308
Switched to a new branch 'temp'
git merge branchName 将当前分支与该分支合并
git merge —allow-unrelated-histories origin/master 将两个无关的分支进行合并
git reset 将暂存区的操作撤销回工作区
git reset - -hard 撤销暂存区和工作区的所有操作
git reset - -hard 版本号 删除该版本号之后的所有commit
git reset head 撤销暂存区操作,并恢复到head版本
git reset head — fileName 撤销部分暂存区操作,并恢复到head版本
git checkout — fileName 放弃工作区内的修改,恢复到暂存区的对应的文件
git log 查看历史版本(默认当前所在分支)
git log branch_name 查看指定分支的版本信息
git log - -oneline 查看历史版本的简要信息
$ git log --oneline
639d308 (HEAD -> master) edit file name
85d4d31 prepare edit file name
403f3fc do delete
ee81cb3 first commit
d659e04 'add'
git log -n2 - -online 查看最新的两条简要信息
$ git log -n2 --oneline
639d308 (HEAD -> master) edit file name
85d4d31 prepare edit file name
git log - -all 查看所有分支的信息
git log - -all - -graph 图形化查看分支继承 信息
gitk 通过GUI查看分支信息
$ git log --all --graph
* commit f0d85b1409bf3cee16d2ede5ef3bf449cd098a5d (HEAD -> tmp)
| Author: lwj <[email protected]>
| Date: Sat Aug 24 15:34:49 2019 +0800
|
| third
|
| * commit 4a678df5d0d4427d616d886d8e81c9354264551c (master)
| | Author: lwj <[email protected]>
| | Date: Sat Aug 24 15:22:31 2019 +0800
| |
| | second
| |
| * commit 639d308226fb93e8d32bdff898d3b7c12d9a9c80
| | Author: lwj <[email protected]>
| | Date: Sat Aug 24 11:46:45 2019 +0800
| |
| | edit file name
| |
| * commit 85d4d318e9a9dc4552e1b518735eae7800184b72
| | Author: lwj <[email protected]>
| | Date: Sat Aug 24 11:41:13 2019 +0800
| |
| | prepare edit file name
| |
| * commit 403f3fc02c68813e4f2220fb65ea1df661873d93
|/ Author: lwj <[email protected]>
| Date: Sat Aug 24 11:23:54 2019 +0800
|
| do delete
|
* commit ee81cb311618bbe81c60ef9f3019765091c64418
| Author: lwj <[email protected]>
| Date: Sat Aug 24 09:34:51 2019 +0800
|
| first commit
|
* commit d659e047f41fc22fd2b94e693dd579ca461aae98
Author: lwj <[email protected]>
Date: Sat Aug 24 01:26:06 2019 +0800
'add'
git diff 403f3fc02c6 d659e047fdfa 比较两个版本的变化
git diff head head~1 比较当前Head和Head的父亲的变化
git diff - -cached 比较暂存区和head之间的变化
git diff 比较工作区和暂存区的区别
git diff - - 文件名 只看该文件
git cat-file -p 403f3fc02c6 查看版本文件内容
定义忽略文件
将git仓库备份到本地
git clone —bare /e/gittest1/.git ya.git —bare 建立裸仓,不含工作区的 使用哑协议
$ git clone --bare /e/gittest1/.git ya.git
Cloning into bare repository 'ya.git'...
done.
git clone —bare file://e/gittest1/.git zhineng.git 使用智能协议,可以看进度
$ git clone --bare file:///e/gittest1/.git zhineng.git
Cloning into bare repository 'zhineng.git'...
remote: Enumerating objects: 24, done.
remote: Counting objects: 100% (24/24), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 24 (delta 4), reused 0 (delta 0)
Receiving objects: 100% (24/24), done.
Resolving deltas: 100% (4/4), done.
创建查看remote
git remote add zhineng file:///e/gittest/zhineng.git
git remote
$ git remote add github [email protected]:networkcavalry/git_study.git 使用ssh协议创建remote
git push zhineng
git push —set-upstream zhineng newBranch
git push -f origin master !!!强制将当前分支覆盖远端,慎用
git fetch 只拉取远端的分支
git pull 拉取远端到本地后对相同的分支进行merge