Git-Common-Usage
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.
Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.
– by Git
The Git-Common-Usage article has been placed on Git
Git command
git init
git initialized
$ git init
git add
use “git add” to track one file
$ git add readme.txt
or track multiple file
$ git add .
git commit
Record changes to the repository
$ git commit -m "first commit"
git status
Show the working tree status
$ git status
git diff
Show changes between commits, commit and working tree, etc
$ git diff readme.txt
diff --git a/readme.txt b/readme.txt
index 4f1b13a..8794b68 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,3 @@
-one edit
\ No newline at end of file
+one edit
+
+two edit
\ No newline at end of file
git log
Show commit logs
$ git log
commit 299e07a39e547e1b48f3fc657daa7e6d4ecbf6f4
Author: GarveyCalvin <1147626297@qq.com>
Date: Sat Jan 17 17:25:17 2015 +0800
two edit
...
Show commit logs with one line
$ git log --pretty=oneline
git log
Draw a text-based graphical representation of the commit history
on the left hand side of the output. This may cause extra lines
to be printed in between commits, in order for the graph history
to be drawn properly
✗ git log --graph
git reset
Reset current HEAD to the specified state
$ git reset --hard HEAD^
HEAD
is current version.HEAD^
is last version.HEAD^^
is the first two version. We can also use theHEAD~n
to return back version.
We can also use version number
to return to special version.
$ git reset --hard 299e07a39e54
The
299e07a39e54
version number can usegit log
to look.
git reflog
The git relog
can be Manage reflog information
bd48a3b HEAD@{0}: commit: Add the Git/GitSomeQuestiion.md file.
e5a0676 HEAD@{1}: commit: add Git/GitSomeQuestiion.md
dd46969 HEAD@{2}: commit: Added Markdown Usage document.
...
git checkout
Checkout a branch or paths to the working tree
$ git checkout -- readme.txt
git rm
Remove files from the working tree and from the index
✗ git rm GitSomeQuestiion.md
If we need to restore it, We first need
git reset HEAD <file>
it, thengit checkout -- <file>
it.
Git branch manager
git branch
Create test
branch, then switch the current branch
$ git branch test
$ git checkout test
or
$ git checkout -b test
Switched to a new branch 'test'
We can use the git branch
to look all branch
$ git branch
We can use the git branch -d
to delete special branch
$ git branch -d test
Deleted branch test (was 6f9e769).
git merge
Join two or more development histories together
$ git merge test
If there is a files conflict, We can use the
git status
to view files in conflict
git merge –no-ff
We can use it to disable Fast-forward function. Git will be commit a new merge branch.
git stash
Stash the changes in a dirty working directory away
$ git stash
Saved working directory and index state WIP on master: 6e84f57 error
HEAD is now at 6e84f57 error
We can also use git stash "<message>"
to leave some information in stash space.
Then we can use the
git status
to check current working directory is clean
We can also use these code to look stash list
$ git stash list
stash@{0}: WIP on master: 6e84f57 error
Then use git stash apply
to restore current working. Default is best new stash. It is a stash@{0}
.
$ git stash apply
We can also use the git stash apply stash@{n}
to restore current working.
If we not need these stash space, then we use
git stash clear
to clean out stash space.
Git Tag
Create Tag
Create, list, delete or verify a tag object signed with GPG
$ git tag v1.0.0
we can according find commited version number to make tag
$ git tag v0.1.0 2b7a3a07c
We can create tag with tag name with information text
$ git tag -a v2.0.0 -m "formal v2.0.0"
Tag List
We can use git tag
to look tag list
$ git tag
v1.0.0
Show Tag
We can use git tag show
to look tag information
$ git show v1.0.0
commit 9e53375e4a84bde213aa2a8d5c77adc251ca1503
Author: GarveyCalvin <1147626297@qq.com>
Date: Tue Jan 20 11:16:58 2015 +0800
formal
...
Then view it
$ git show v2.0.0
tag v2.0.0
Tagger: GarveyCalvin <1147626297@qq.com>
Date: Tue Jan 20 11:38:10 2015 +0800
formal v2.0.0
Delete Tag
We can use git tag -d
to delete spcial tag
$ git tag -d v1.0.0
Deleted tag 'v1.0.0' (was 9e53375)
Origin git
Use git push
push to remote git
$ git push origin v1.0.0
Total 0 (delta 0), reused 0 (delta 0)
To ***.git
* [new tag] v1.0.0 -> v1.0.0
We can use
git push origin --tags
to push all tags to remtoe git
If pushed, We want to delete tag, we need to delete tag in local
$ git tag -d v0.0.1
Deleted tag 'v0.0.1' (was bc2cb64)
Then delete tag in remote git
$ git push origin :refs/tags/v0.0.1
To ***.git
- [deleted] v0.0.1
Git Alias
We can use alias
to simplify command
such as
git config alias.st status
We can use
--global
put the command into the whole. It looke likegit config --global alias.st status
Then simple use it
$ git st
On branch master
Your branch is up-to-date with 'origin/master'.
Git Log to perfert
git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
It is look like
We can use
alias
to simplify these code
Git Some Questiion
How to ignore some files?
Example for .gitignore
# '#' is annotate.
# Ignore all the named test.txt files
test.txt
# Ignore all the suffix .html files.
*.html
# Do not ignore named test.html files.
!test.html
# Ignore all the suffix .o and .a files.
*.[oa]
- Create a
.gitignore
file - Edit some config in the file
- Commit the file to git storehouse
.gitignore
file is effect in current folder and all sub folder
What is the untracked content erorr?
modified: xxx(modified content, untracked content)
solution:
Untracked project have .git
directory. You maybe used to created. This directory can be deleted. Then add back to your git.
Where is the git config file?
vim ~/.gitconfig