初始化本地库
git init
添加文件到暂存区
git add . / git add filename
提交文件 -m 表示对当前修改的描述
git commit -m "commit test"
为什么Git添加文件需要add,commit一共两步呢?因为commit可以一次提交很多文件,所以你可以多次add不同的文件,比如:
$ git add t1.md
$ git add t2.md
$ git commit -m "add two files"
修改一个文件,然后使用git diff filename查看文件修改了哪些内容
$ git diff README.md
diff --git a/README.md b/README.md
index e581649..d68176f 100644
--- a/README.md
+++ b/README.md
@@ -9,4 +9,6 @@ Springboot学习
#test1
-$test2
\ No newline at end of file
+$test2
+
+# fasdfasdf
\ No newline at end of file
PS D:\springboot>
修改一个文件(未提交),使用git status命令
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
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.md
no changes added to commit (use "git add" and/or "git commit -a")
Changes not staged for commit:git告诉你文件已经被修改,但是并没有提交(commit)
更新本地修改到远程仓库 ,需要输入github账号密码
$ git push origin -u master
Username for 'https://github.com': chenai0122@gmail.com
Password for 'https://chenai0122@gmail.com@github.com':
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 509 bytes | 0 bytes/s, done.
Total 6 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 1 local objects.
To https://www.github.com/sperains/Springboot.git
ce103d0..ef56a07 master -> master
Branch master set up to track remote branch master from origin.
新建分支并切换
$ git checkout -b dev
Switched to a new branch 'dev'
查看当前分支,git branch 会列出所有分支,*代表当前分支
$ git branch
* dev
master
切换到master分支
$ git checkout master
将分支dev合并到master
$ git merge dev
Updating d17efd8..fec145a
Fast-forward
readme.txt | 1 +
1 file changed,
1 insertion(+)
git merge 命令作用是将指定分支合并到当前分支。
合并之后就可以安全删除分支了
$ git branch -d dev
Deleted branch dev (was c4bed9b).
将分支提交到远程
$ git push origin dev
删除远程分支,原理是推一个空分支到远程。这样就相当于删除分支了
$ git push origin :dev
在本地添加sshkey
$ ssh-keygen -t rsa -C "username@gmail.com"
如果执行成功,返回
Generating public/private rsa key pair.Enter file
in which to save the key (/home/forwhat.cn/.ssh/id_rsa):
之后一直enter,把生成的id_rsa.pub文件内容添加到github账户设置中
查看本地git配置
$ git config --list
添加远程库
$ git remote add origin git@github.com:sperains/Springboot.git
删除远程库
$ git remote remove origin
添加或者删除命令中的 origin 为远程库的名称.
强行提交,添加-f参数
$ git push -f origin master
版本回退
git log [查看commit记录]//这个3628164 是一个commit版本号,可以指定回退到那个版本
git reset --hard 3628164