Git
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
Init
Create maven project
Use your idea to create a maven project. May like this:
D:\CODE\git-demo>tree /f
文件夹 PATH 列表
卷序列号为 00000200 3E45:BF16
D:.
│ git-demo.iml
│ pom.xml
│
├─.idea
│ │ compiler.xml
│ │ misc.xml
│ │ modules.xml
│ │ workspace.xml
│ │
│ └─copyright
│ profiles_settings.xml
│
└─src
├─main
│ ├─java
│ └─resources
└─test
└─java
.gitignore
This file can config what type of files you do not want to commit.
# maven ignore
target/
*.jar
*.war
*.zip
*.tar
*.tar.gz
# eclipse ignore
.settings/
.project
.classpath
# idea ignore
.idea/
*.ipr
*.iml
*.iws
git init & import to Github
1、you can use this way if you use IntelliJ IDEA
VCS -> Import into Version Control -> Share Project on Github
2、common ways
- create a new repository on the command line
echo "# git-demo" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/houbb/git-demo.git
git push -u origin master
- push an existing repository from the command line
git remote add origin https://github.com/houbb/git-demo.git
git push -u origin master
Share to Github
Or create a new repository on the command line
echo "# quartz-book" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/houbb/quartz-book.git
git push -u origin master
Or push an existing repository from the command line
git remote add origin https://github.com/houbb/quartz-book.git
git push -u origin master
Base usage
- git status
show the git files status;
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
pom.xml
nothing added to commit but untracked files present (use "git add" to track)
- git add
add files into local Repository
$ git add .gitignore
- git commit
commit files into local Repository
$ git commit -m "first commit"
[master (root-commit) 965cf5d] first commit
1 file changed, 6 insertions(+)
create mode 100644 .gitignore
- git push
push files into remote Repository
$ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
$ git push --set-upstream origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 258 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/houbb/git-demo.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
In this way, we can add another file README.md
into Repository for test, content is:
> Hello Git
Now, we edit the file README.md
into this:
> Hello Git
- git diff
- git diff
see the difference between local and Repository
$ git diff README.md
diff --git a/README.md b/README.md
index adb219f..e5a680e 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
-> Hello Git
\ No newline at end of file
- git log
show the log of git
$ git log
commit cd84e273e5ac1d42d08090d013696a237b120751
Author: houbinbin <1060732496@qq.com>
Date: Mon Oct 10 18:43:33 2016 +0800
git diff
commit 9f18a0cd10ac0eb661ab1a9bf056398c5e6fe6f3
Author: houbinbin <1060732496@qq.com>
Date: Mon Oct 10 18:14:38 2016 +0800
add readme
the cd84e273e5ac1d42d08090d013696a237b120751
is commit_id
, the uk of commit.
Time data back
The version recall
In Git, HEAD
stands for current version, in our demo is cd84e273e5ac1d42d08090d013696a237b120751
.
HEAD^
stands for last version, the version before last is HEAD^^
,
the last 100 version can write as HEAD~100
- git reset
If we want to recall version into 9f18a0cd10ac0eb661ab1a9bf056398c5e6fe6f3
(add readme)
$ git reset --hard 9f18a0cd10ac0eb661ab1a9bf056398c5e6fe6f3
and then, the content of README.md
is:
> Hello Git
- git reflog
now, how can we recall to version of cd84e273e5ac1d42d08090d013696a237b120751
(git diff)?
use git reflog
can get all commits you have done.
$ git reflog
9f18a0c HEAD@{0}: reset: moving to 9f18a0cd10ac0eb661ab1a9bf056398c5e6fe6f3
cd84e27 HEAD@{1}: merge origin/master: Fast-forward
9f18a0c HEAD@{2}: reset: moving to 9f18a0cd10ac0eb661ab1a9bf056398c5e6fe6f3
cd84e27 HEAD@{3}: merge origin/master: Fast-forward
9f18a0c HEAD@{4}: reset: moving to 9f18a0cd10ac0eb661ab1a9bf056398c5e6fe6f3
cd84e27 HEAD@{5}: commit: git diff
9f18a0c HEAD@{6}: commit: add readme
965cf5d HEAD@{7}: commit (initial): first commit
then
$ git reset --hard cd84e27
HEAD is now at cd84e27 git diff
and now, the content of README.md
is:
> Hello Git
- git diff
Working Directory & Stage
- Working Directory
the maven project can seem as a Working Directory
- Repository
the files of our project, you can see the .git
dir, it includes stage
、and the first branch master
$ dir /a
2016/10/10 18:53 <DIR> .
2016/10/10 18:53 <DIR> ..
2016/10/10 18:54 <DIR> .git
2016/08/03 17:16 56 .gitignore
2016/10/10 19:06 <DIR> .idea
2016/10/10 18:48 0 git
2016/10/10 16:53 862 git-demo.iml
2016/10/10 16:52 419 pom.xml
2016/10/10 18:53 23 README.md
2016/10/10 16:52 <DIR> src
- git add
add files into stage
- git commit
commit all files in stage into current branch, now is master
Discard Changes
if you add error content in README.md
by mistake, like this:
> Hello Git
- git diff
error--!
How to discard the changes?
$ git status
On branch master
Your branch is up-to-date with 'origin/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.md
As you can see, the command git checkout -- <file>...
can discard changes in working directory
$ git checkout -- README.md
then, the content of README.md
will be:
> Hello Git
- git diff
Remove Files
- git rm
Now, we add temp.md
for test, add it into stage
$ git add temp.md
$ git status
new file: temp.md
$ git rm temp.md
error: the following file has changes staged in the index:
temp.md
(use --cached to keep the file, or -f to force removal)
use git rm --cached
will retains the file in your working dir and remove file from stage
$ git rm --cached temp.md
rm 'temp.md'
$ git status
Untracked files:
(use "git add <file>..." to include in what will be committed)
temp.md
Now, we commit the temp.md
into branch and the use git rm
, the file will be removed from working dir
$ git add temp.md
$ git commit -m "add temp.md"
[master d36026f] add temp.md
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 temp.md
$ git rm temp.md
rm 'temp.md'
$ git status
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: temp.md
1、If you really want to delete temp.md
, commit it.
$ git commit -m "remove temp.md"
[master d33d131] remove temp.md
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 temp.md
2、if you do not want delete temp.md
, the file will be back ^_^
$ git checkout -- temp.md
Branch Manage
- View branch
local
$ git branch
remote
$ git branch -r
all
$ git branch -a
- Create & Checkout
houbinbindeMacBook-Pro:git-demo houbinbin$ git branch git_demo_1.1
houbinbindeMacBook-Pro:git-demo houbinbin$ git branch
git_demo_1.1
* master
houbinbindeMacBook-Pro:git-demo houbinbin$ git checkout git_demo_1.1
Switched to branch 'git_demo_1.1'
houbinbindeMacBook-Pro:git-demo houbinbin$
is the same as
$ git checkout -b <name>
- Push Branch into Github
$ git push origin [branch name]
- Remove Branch
local
$ git branch -d [branch name]
github remote
$ git push origin --delete <branchName>
$ git checkout master
$ git pull
$ git checkout branch
$ git rebase master (用rebase合并主干的修改,如果有冲突在此时解决)
$ git checkout master
$ git merge branch
$ git push
- push
Attention, After merge it’s has no stage, so, commit will tell you nothing to change.
You should use git push origin master
$ git push //push into current branch
$ git push origin master //push into master
- branch merge tips
Usually, git will use Fast Forward
mode when merge, we can use --no-ff
to merge.
houbinbindeMacBook-Pro:git-demo houbinbin$ git checkout -b git_demo_1.2
Switched to a new branch 'git_demo_1.2'
houbinbindeMacBook-Pro:git-demo houbinbin$ git add README.md
houbinbindeMacBook-Pro:git-demo houbinbin$ git commit -m "change git_demo_1.2"
[git_demo_1.2 8983fd1] change git_demo_1.2
1 file changed, 2 insertions(+), 1 deletion(-)
houbinbindeMacBook-Pro:git-demo houbinbin$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
houbinbindeMacBook-Pro:git-demo houbinbin$ git merge --no-ff -m "merge with --no-ff" git_demo_1.2
Merge made by the 'recursive' strategy.
README.md | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
Now, see the log
houbinbindeMacBook-Pro:git-demo houbinbin$ git log --graph --pretty=oneline --abbrev-commit
* 0d899d1 merge with --no-ff
|\
| * 8983fd1 change git_demo_1.2
|/
* d696904 add pom.xml
* cd84e27 git diff
* 9f18a0c add readme
* 965cf5d first commit
- remove local branch
$ git branch -d xxxxx
Bug branch
If on the half of your work, you must to solve other things.
For example, you write sth on branch 1.2
, but there has a bug to fix, how should we do?
- git stash
this command can save current work conditions.
houbinbindeMacBook-Pro:git-demo houbinbin$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(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")
houbinbindeMacBook-Pro:git-demo houbinbin$ git stash
Saved working directory and index state WIP on master: 0d899d1 merge with --no-ff
HEAD is now at 0d899d1 merge with --no-ff
now, we can checkout into master and fix the bug.
houbinbindeMacBook-Pro:git-demo houbinbin$ git checkout master
Already on 'master'
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
houbinbindeMacBook-Pro:git-demo houbinbin$ git checkout -b git_demo_bug_001
Switched to a new branch 'git_demo_bug_001'
houbinbindeMacBook-Pro:git-demo houbinbin$ git add README.md
houbinbindeMacBook-Pro:git-demo houbinbin$ git commit -m "fix the bug"
[git_demo_bug_001 4069a0c] fix the bug
1 file changed, 2 insertions(+), 1 deletion(-)
houbinbindeMacBook-Pro:git-demo houbinbin$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
houbinbindeMacBook-Pro:git-demo houbinbin$ git merge git_demo_bug_001
Updating 0d899d1..4069a0c
Fast-forward
README.md | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
after fix the bug, we can checkout to the 1.2
branch, continue to work
houbinbindeMacBook-Pro:git-demo houbinbin$ git stash list
stash@{0}: WIP on master: 0d899d1 merge with --no-ff
houbinbindeMacBook-Pro:git-demo houbinbin$ git stash pop
On branch git_demo_1.2
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")
Dropped refs/stash@{0} (9cd4d92f41db2cdb1b8e15ffde7c73c4fd6ef83c)
houbinbindeMacBook-Pro:git-demo houbinbin$ git stash list
Tag
- create tag
git tag <tagname> [commit_id]
- show tags
git tag
- show tag detail info
git show <tagname>
houbinbindeMacBook-Pro:git-demo houbinbin$ git branch
git_demo_1.1
git_demo_1.2
git_demo_bug_001
* master
houbinbindeMacBook-Pro:git-demo houbinbin$ git tag v1.0
houbinbindeMacBook-Pro:git-demo houbinbin$ git tag
v1.0
houbinbindeMacBook-Pro:git-demo houbinbin$ git show v1.0
commit 4069a0cc54ee97993850419a09b62023ee3fa711
Author: “houbb” <“1060732496@qq.com”>
Date: Mon Oct 10 22:48:07 2016 +0800
fix the bug
diff --git a/README.md b/README.md
index 5664a33..98be30e 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,4 @@
> Hello Git
- git diff
-- git_demo_1.2
\ No newline at end of file
+- git_demo_1.2
+fix the bug
\ No newline at end of file
houbinbindeMacBook-Pro:git-demo houbinbin$ git log --pretty=oneline --abbrev-commit
4069a0c fix the bug
0d899d1 merge with --no-ff
8983fd1 change git_demo_1.2
d696904 add pom.xml
cd84e27 git diff
9f18a0c add readme
965cf5d first commit
houbinbindeMacBook-Pro:git-demo houbinbin$ git tag v0.9 0d899d1
houbinbindeMacBook-Pro:git-demo houbinbin$ git tag
v0.9
v1.0
houbinbindeMacBook-Pro:git-demo houbinbin$ git show tag v0.9
fatal: ambiguous argument 'tag': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
houbinbindeMacBook-Pro:git-demo houbinbin$ git show v0.9
commit 0d899d1e9fa22bc3d8092d4a709abc0174186f79
Merge: d696904 8983fd1
Author: “houbb” <“1060732496@qq.com”>
Date: Mon Oct 10 22:29:49 2016 +0800
merge with --no-ff
- add tag with info
git tag -a <tagname> -m "info"
houbinbindeMacBook-Pro:git-demo houbinbin$ git tag -a v0.1 -m "first-commit-tag" 965cf5d
houbinbindeMacBook-Pro:git-demo houbinbin$ git show v0.1
tag v0.1
Tagger: “houbb” <“1060732496@qq.com”>
Date: Mon Oct 10 23:11:05 2016 +0800
first-commit-tag
commit 965cf5db0b2ca8b062d2cd895b7431d37e11a23f
Author: houbinbin <1060732496@qq.com>
Date: Mon Oct 10 18:05:43 2016 +0800
first commit
- add tag with PGP sign
git tag -s <tagname> -m "sign"
Manage Tags
houbinbindeMacBook-Pro:git-demo houbinbin$ git tag
v0.1
v0.9
v1.0
houbinbindeMacBook-Pro:git-demo houbinbin$ git tag -d v0.1
Deleted tag 'v0.1' (was 0fc098a)
houbinbindeMacBook-Pro:git-demo houbinbin$ git push origin v0.9
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 442 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/houbb/git-demo.git
* [new tag] v0.9 -> v0.9
houbinbindeMacBook-Pro:git-demo houbinbin$ git push origin --tags
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 361 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/houbb/git-demo.git
* [new tag] v1.0 -> v1.0
- remove local tag
git tag -d <tagname>
- remove remote tag
git push origin :refs/tags/<tagname>
Build Git Server
TBC…
Share Projects To Github
- Init git
git init
- Add files
git add .
- Commit files
git commit -m "commit desc"
-
Create Repository on github
-
Add local to remote
git remote add origin https://github.com/houbb/mybatisNet.git
- Push
git push -u origin master
1、fatal: remote origin already exists.
$ git remote rm origin
after this, retry add
$ git remote add origin https://github.com/houbb/mybatisNet.git
2、Updates were rejected because the tip of your current branch is behind
$ git push -u origin master -f
这个会导致强制覆盖。可能会有数据丢失。
Git ignore
众所周知,.gitignore
文件用起来很方便。但是却需要我们手动去编写,
gitignore.io 可以通过我们的勾选,生成对应的忽略文件。
Git 子模块
Git 仓库地址
查看仓亏地址
$ git remote -v
比如:
$ git remote -v
origin http://192.168.0.62/XXX/xxx.git (fetch)
origin http://192.168.0.62/XXX/xxx.git (push)
修改仓库地址
- 直接修改
$ git remote set-url origin URL
- 删除+添加
$ git remote rm origin
$ git remote add origin url