Git Basics

  • git init && git clone
#Initializing a Repository in an Existing Directory
$ git init

#Cloning an Existing Repository
$ git clone https://github.com/libgit2/libgit2

#Cloning an Existing Repository with another name
$ git clone https://github.com/libgit2/libgit2  mylibgit
  • git status
$ git status

#short status
$ git status -s
  • Ignoring Files
$ cat .gitignore

The rules for the patterns you can put in the .gitignore file are as follows:
Blank lines or lines starting with # are ignored.
Standard glob patterns work.
You can start patterns with a forward slash (/) to avoid recursivity.
You can end patterns with a forward slash (/) to specify a directory.
You can negate a pattern by starting it with an exclamation point (!).

# no .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory
doc/**/*.pdf
  • git diff
#see changes not yet staged
$ git diff

#see changes have staged
$ git diff --staged

  • git commit
#commit changes
$ git commit 

#commit with commit message inline
$ git commit -m "commit messages"

#commit to skip the staging area,for the traced files
$ git commit -a -m "commit messages"
  • git rm
#remove a file from Git,also remove it from working directory
$ git rm README.md

#remove a file from Git,keep it in working directory
$ git rm --cached README.md

#You can pass files, directories, and file-glob patterns to the git rm command.
$ git rm log/\*.log
  • git mv
#rename a file
$ git mv file_from file_to

#this is equivalent to running something like this:
$ mv README.md README
$ git rm README.md
$ git add README
  • git log
#view the commit history
$ git log

#-p shows the difference, -2 limits the output to last two entries:
$git log -p -2 

#--stat display what happened,see abbreviated stats for each commit
$ git log --stat

# --pretty,changes the log output to formats other than the default.
$ git log --pretty=oneline

# format, specify you own log output format.
$ git log --pretty=format:"%h - %an, %ar: %s"

# --graph,adds a nice little ASCII graph showing
$ git log --pretty=format:"%h %s" --graph

#limit log output
$ git log --since=2.weeks

#see which commits modifying test files in the Git source code history are merged and were committed by Junio Hamano in the month of October 2008
$ git log --pretty="%h - %s" --author=gitster --since="2008-10-01" \
   --before="2008-11-01" --no-merges -- t/


Option Description
-(n) Show only the last n commits
--since, --after Limit the commits to those made after the specified date.
--until, --before Limit the commits to those made before the specified date.
--author Only show commits in which the author entry matches the specified string.
--committer Only show commits in which the committer entry matches the specified string.
--grep Only show commits with a commit message containing the string
-S Only show commits adding or removing code matching the string
  • git undo
# --amend,amend to the last commit 
$ git commit --amend

#unstaging a staged file
$ git reset HEAD ....

#unmodify a modified file
$ git checkout -- ...
  • git remote
#list remotes
$ git remote

$ git remote -v

#add remote repository
$ git remote add pb https://github.com/paulboone/ticgit

#fetch from remote
$ git fetch pb

#push to remote
$ git push origin master

#inspect a remote
$ git remote show origin

#rename and remove
$ git remote rename pb paul

$ git remote rm paul
  • git tag
#list your tags
$ git tag

#search tags
$ git tag -l "v1.8.5*"

#create a tag
$ git tag -a v1.4 -m "my version 1.4"

#show a tag
$ git show v1.4

#push tag
$ git push origin [tagname]

#push all tags
$ git push origin --tags

#checkout tags
$ git checkout -b [branchname] [tagname]

你可能感兴趣的:(Git Basics)