These are common Git commands used in various situations:
start a working area (see also: git help tutorial) clone Clone a repository into a new directory init Create an empty Git repository or reinitialize an existing one
work on the current change (see also: git help everyday) add Add file contents to the index mv Move or rename a file, a directory, or a symlink restore Restore working tree files rm Remove files from the working tree and from the index
examine the history and state (see also: git help revisions) bisect Use binary search to find the commit that introduced a bug diff Show changes between commits, commit and working tree, etc grep Print lines matching a pattern log Show commit logs show Show various types of objects status Show the working tree status
grow, mark and tweak your common history branch List, create, or delete branches commit Record changes to the repository merge Join two or more development histories together rebase Reapply commits on top of another base tip reset Reset current HEAD to the specified state switch Switch branches tag Create, list, delete or verify a tag object signed with GPG
collaborate (see also: git help workflows) fetch Download objects and refs from another repository pull Fetch from and integrate with another repository or a local branch push Update remote refs along with associated objects
'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept. See 'git help git'for an overview of the system. fr3y@fr3y-pc:~$ git --version 14:24:49.617849 git.c:455 trace: built-in: git version git version 2.34.1
Mac OS
直接从AppStore安装Xcode,Xcode集成了Git,不过默认没有安装,你需要运行Xcode,选择菜单“Xcode”->“Preferences”,在弹出窗口中找到“Downloads”,选择“Command Line Tools”,点“Install”就可以完成安装了。
fr3y@fr3y-pc:~$ mkdir learngit fr3y@fr3y-pc:~$ cd learngit fr3y@fr3y-pc:~/learngit$ pwd /home/fr3y/learngit fr3y@fr3y-pc:~/learngit$ git init 15:50:51.222462 git.c:455 trace: built-in: git init hint: Using 'master' as the name for the initial branch. This default branch name hint: is subject to change. To configure the initial branch name to use in all hint: of your new repositories, which will suppress this warning, call: hint: hint: git config --global init.defaultBranch <name> hint: hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and hint: 'development'. The just-created branch can be renamed via this command: hint: hint: git branch -m <name> Initialized empty Git repository in /home/fr3y/learngit/.git/
接下来我们在readme.txt中添加一行Git is a distributed version control system.之后运行git status。
1 2 3 4 5 6 7 8 9
fr3y@fr3y-pc:~/learngit$ git status 16:43:03.856838 git.c:455 trace: built-in: git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
使用git reset回退到上一个版本add a sentence。HEAD表示上一版本,^表示前一版本。
1 2 3 4 5 6 7
fr3y@fr3y-pc:~/learngit$ git reset --hard HEAD^ 21:28:29.207423 git.c:455 trace: built-in: git reset --hard HEAD^ HEAD is now at 354bc8d add a sentence fr3y@fr3y-pc:~/learngit$ cat readme.txt Hello Git! Git is a distributed version control system.
回退到任一版本,输入git reset --hard+commit id 前几位。
1 2 3 4 5 6
fr3y@fr3y-pc:~/learngit$ git reset --hard b2e5e 21:32:41.759184 git.c:455 trace: built-in: git reset --hard b2e5e HEAD is now at b2e5ef1 append GPL fr3y@fr3y-pc:~/learngit$ cat readme.txt Hello Git! Git is a distributed version control system under the GPL.