Article Directory
Note: This section is a short note from the second chapter of progit .
The most important thing is to learn to view documents by yourself
1. Get the git repository
There are two ways to obtain a git repository:- Convert a local directory without version control to a git repository:
enter any directory, type in the command linegit init
, generate a .git folder, and start version control. But at this time, no files are tracked, andgit add .
all files in the current folder (excluded by the .gitignore file) will be tracked; then usegit add LICENSE
add license; finally usegit commit -m "initial project version"
submit.
- Clone an existing git repository from another server
The command to clone a git repository isgit clone <url>
. For example: thegit clone https://github.com/libgit2/libgit2
example command uses the http protocol to clone the git link library libgit2
The protocols supported by git include:
http://protocol
ssh://protocol
git:// protocol
2. Record each update to the warehouse
git status | View the detailed status of all files |
git add your_filename | Start tracking untracked files |
git add your_filename | Submit the changed tracked file to the staging area |
git status -s | Brief file status: A, M, ?? marks indicate newly added to the temporary storage area, modified files, and newly tracked files (MM means temporary storage and modification after modification) |
.gitignore file | Ignore the content specified in the file, github has a detailed list of .gitignore files, the link is https://github.com/github/gitignore |
git diff | View the difference between the current file and the temporary file |
git diff --staged | Compare the difference between the temporary file and the last file submitted |
git commit | Submit the staging area (the default editor will appear for you to enter the submission information |
git commit -m "your_commit_explanation" | Enter the submission information directly without opening the text editor |
git commit -a -m "your_commit_explanation" | Submit all tracked files directly without adding git add to the staging area |
git rm filename | Remove tracked files |
git mv old_filename new_filename | Move or rename |
3 View submission history-use of git log
Syntax: git log [] [] [[--]Enter ```git help log``` or ```git log --help``` on the command line to get help
Common options for git log
Common options for git log | |
Options | Description |
-p | Display the differences introduced by each submission in patch format. |
--stat | Display the statistics of each submitted file modification. |
--shortstat | Only display the last number of rows in --stat, modify, add, and remove statistics. |
--name-only | The list of modified files is displayed only after submitting the information. |
--name-status | Display the list of newly added, modified and deleted files. |
--abbrev-commit | Only the first few characters of all 40 characters in the SHA-1 checksum are displayed. |
--relative-date | Use a shorter relative time instead of the full format to display the date (such as "2 weeks ago"). |
--graph | The branch and merge history are displayed in ASCII graphics beside the log. |
--pretty | Use other formats to display historical submission information. The available options include oneline, short, full, full, and format (used to define your own format). |
--oneline | --pretty=oneline --abbrev-commit is a shorthand for combined use. |
Representation submitted in accordance with the difference patches output format introduced last twogit log -p -2
Options to limit git log output
Options | Description |
- | Only the most recent n submissions are displayed. |
--since, --after | Only show submissions after the specified time. |
--until, --before | Only show submissions before the specified time. |
--author | Only show submissions whose authors match the specified string. |
--committer | Only show submissions whose submitter matches the specified string. |
--grep | Only display submissions that contain the specified string in the submission description. |
-S | Only display submissions that add or delete content matching the specified string. |
git log --pretty="%h-%s" --author='Junio C Hamano' --since="2008-10-01" --before="2008-11-01" --no-merges- -t/ Check which of Junio Hamano’s submissions other than merge submissions under the directory t in October 2008 modified the test file
4. Undo the operation
- git commit --amend: Submit files in the staging area, which will overwrite the results of the previous submission.
- git reset HEAD filename: cancel the temporary storage of the filename file
- git checkout - filename: restore the filename file to the last submitted version
5. Use of remote warehouse
If you clone a repository using the clone command, the command will automatically add it as a remote repository and use "origin" as abbreviation by defaultcommand | effect |
git remote | Display all remote warehouses that have been specified |
git remote -v | Display the abbreviations saved in Git and their corresponding URLs that need to be read and written in remote warehouses |
git remote add <shortname> <url> | Add a new remote Git repository and specify a convenient shorthand |
git fetch <remote> | 访问远程仓库,从中拉取所有你还没有的数据。 不会自动合并或修改你当前的工作 |
git push <remote> <branch> | 将本地的分支推送到remote服务器 如git push origin master将 master 分支推送到 origin 服务器 你需要有服务器的写入权限,且之前没有人推送过时,这条命令才能生效 |
git remote show <remote> | 列出远程仓库的 URL 与跟踪分支的信息,并说明运行git pull 和git push 的效果 |
git remote rename old_name new_name | 重命名 |
git remote remove name | 移除name仓库,remove可以用rm替换 |
2.6 打标签(手动指定版本)
- git tag: 列出已有标签
- git tag --list “str": 匹配含有”str“的标签,--list简写为-l
- git tag v1.3: 给提交打上v1.3标签(轻量标签)
- git tag -a v1.4 -m "my version 1.4":给提交打上标签v1.4
- git show v1.4: 匹配标签v1.4的信息(标签信息和提交信息)
- git tag -a v1.2 9fceb02: 补打标签,9fceb02是部分校验和
- git push origin <tagname>git push不推送标签,必须显式指定来共享标签,该过程就像推送共享远程分支
- git tag -d tag_name: 删除本地tag_name标签
- git push <remote> :refs/tags/<tagname>git push不推送标签,必须显式指定来删除标签
- git push origin --delete : 同上
检出标签:detached HEAD状态,详见here
2.7 git别名
如果不想每次都输入完整的 Git 命令,可以通过 git config 文件来轻松地为每一个命令设置一个别名。如: git config --global alias.co checkout
给checkout动作设置了别名co