Git basics
1 Get the Git repository
The first one: Import existing directories or projects into Git
Second: Clone an existing Git repository from the server
1.1 Initialize an existing warehouse in an existing directory
Enter the project directory and enter:
$ git init
If version control is used, start tracking and initial commits:
$ git add *.c
$ git add LICENSE
$ git commit -m 'initial project version'
The meaning of these commands will be explained later
1.2 Clone an existing repository
Clone command:
$ git clone https://github.com/libgit2/libgit2 mylibgit //mylibgit 是目标目录,可省
Unlike other version control 'checkout' commands, Git's 'clone' will make a complete copy of almost all data in the server repository, not just the current working directory.
2 Record changes in Git repository
2.1 View current status
Main tools:
$ git status
If executed immediately after cloning the warehouse, the following information will be displayed:
$ git status
On branch master
nothing to commit, working directory clean
2.2 Track new files
Execute the following command to track the README file:
$ git add README
2.3 Temporarily save modified files
$ git add CONTRIBUTING.md
The git add command can be used to track files, to temporarily store files, and to do other things as well. It can be understood as 'add content to the next submission' rather than 'add this file to the project'.
If the file is modified after executing git add, you need to execute the git add command again.
2.4 Display more concise status information
$ git status -s
或者
$ git status --short
M README
MM Rakefile
A lib/git.rb
M lib/simplegit.rb
?? LICENSE.txt
??: Untracked new files
A: New files that have been staged
M: modified file
2.5 Ignore files
If you want to ignore some automatically generated files (such as log files), in this case you can create a file named .gitignore and list the file patterns to be matched, for example:
$ cat .gitignore
#告诉Git忽略所有以.a .o结尾的文件
*.[oa]
#忽略所有以~结尾的文件
*~
The rules for matching patterns in .gitignore files are as follows:
- Empty lines or lines starting with # will be ignored
- Supports standard glob patterns
- Patterns starting with a slash (/) can be used to disable recursive matching
- Patterns ending with a slash (/) represent directories
- Patterns starting with an exclamation point (!) indicate negation
2.6 View staged and unstaged changes
View changes that have not yet been added to the staging area:
$ git diff
Viewing the staged content will proceed to the next submission:
$ git diff --staged
If you prefer a graphical or external diff viewing program, there is another way to view the diff structure. Execute git difftool so that you can view the differences in emerge, vimdiff and other software. Use git difftool --tool -help to view the available diff tools in the system.
2.7 Commit changes
The simplest way to submit is:
$ git commit
Type the submitted information directly on the command line:
$ git commit -m "Story 182:Fix benchmarks for speed"
2.8 Skip the staging area
$ git commit -a -m 'added new benchmarks'
2.9 Remove files
$ git rm PROJECTS.md
This file will no longer exist when submitted next time, and will not be tracked and managed by git.
If you want to keep a file in the working directory but remove it from the staging area, just use the --cached option:
$ git rm --cached README
2.10 Moving files
Whether you use git's mv command or directly rename the file, Git can infer that this is a rename operation.
$ git mv README.md README
3 View submission history
$ git log
git log has many different options that can intuitively display what you need.
Option -p, you can also add -2, the difference introduced by the last two commits:
$ git log -p -2
Option --stat, you can view brief statistics for each submission
$ git log --stat
Another useful option is --pretty, which can change the default format of log output
$ git log --pretty==oneline
In addition to online, the short, full, and fuller format options respectively reduce or add some information than the default output. The most notable option is format, which allows you to specify your own output format
$ git log --pretty=format:"%h - %an, %ar : %s"
Format options | Output format description |
%H | The hash value of the submitted object |
%h | A short hash of the submitted object |
%T | Hash value of tree object |
%t | A short hash value of the tree object |
%P | The hash value of the parent object |
%p | A short hash of the parent object |
%an | author's name |
%ae | Author's email address |
%ad | Creation date (date format can be specified using the -date= option) |
%ar | creation date relative to current date |
%cn | submitter's name |
%ce |
Submitter's email address |
%cd | Submission date |
%cr | Submission date equivalent to current date |
%s | Submit information topic |
Limit the output range of commit history
$ git log --since=2.weeks
Options | describe |
-(n) | Only show the latest n submissions |
--since,--after | Only output commits after the specified date |
--until,--before | Only output commits before the specified date |
--auth | Only output commits whose authors match the specified string |
--committer | Only output submissions whose submitters match the specified characters |
--grep | Only output commits whose commit information contains the specified string |
-S | Only output commits that contain changes that add or remove the specified string |
4 Undo operation
4.1 Undo temporarily stored files
$ git reset HEAD filename
4.2 Undo changes to files
$ git checkout -- 文件名
This is a very dangerous command because the above command is overwritten with the previous version. Do not use this command unless you are sure that these files are not needed.
5 Use of remote warehouse
5.1 Display remote warehouse
$ git clone http://github.com/schacon/ticgit
5.2 Add remote warehouse
$ git remote add pb https://github.com/paulboone/ticgit
Now you can use pb characters instead of full urls on the command line. For example, to get all the data that Paul owns but you don’t own yet, you can execute the following command:
$ git fetch pb
5.3 Get and pull data from remote warehouse
$ git fecth [remote-name]
5.4 Push data to remote warehouse
$ git push origin master
5.5 Check the remote warehouse
$ git remote show origin
5.6 Delete and rename remote repository
#重命名
$ git remote rename pb paul
#删除
$ git remote rm paul