就这么简单!10 分钟入门 Git,


虽然Git从本质上讲是监控和跟踪文本的更改,但它的定位依然是版本控制系统。你可能已经以某种方式使用过git;由于其分布式特性,git成为了事实上的代码版本控制标准,与集中式Apache Subversion (SVN)截然相反。

安装Git

要检查是否在终端中安装了Git,请运行:

  1. git version  
  2. # git version 2.25.1 

https://git-scm.com/downloads

Ubuntu用户可以使用apt安装:sudo apt install git。

配置Git

我们需要配置的东西不多: 

  1. git config --global user.name "John Doe" && # your name  
  2. git config --global user.email johndoe@example.com && # your email  
  3. git config --global init.defaultbranch main # default branch name, to be compatible with GitHub  

你可以通过以下方式查看当前的全局配置:

  1. git config --global --list  
  2. # Type ":q" to close 

git以纯文本形式存储配置,当然你也可以直接在~/.gitconfig或~/.config/git/config中编辑全局配置。

正如命令所建议的那样,删除--global将使这些命令的范围限定为当前文件夹。为了测试这一点,我们需要一个存储库。

创建新的存储仓库

存储仓库就是一个包含要跟踪的所有内容的文件夹。创建存储仓库请运行:

  1. mkdir gitexample &&   
  2. cd gitexample &&   
  3. git init  
  4. # gitexample git:(main) 

此命令在gitexample文件夹中创建了文件夹.git。隐藏的.git文件夹是一个存储仓库:所有本地配置和更改都存储在那里。

做一些变更

让我们在存储库中创建一些东西:

  1. echo "Hello, Git" >> hello.txt 

如果我们运行git status,我们将看到新创建的未被跟踪的文件:

  1. git status  
  2. # On branch main  
  3. #   
  4. # No commits yet  
  5. #   
  6. # Untracked files:  
  7. #  (use "git add <file>..." to include in what will be committed)  
  8. #   hello.txt  
  9. #  
  10. # nothing added to commit but untracked files present (use "git add" to track) 

接下来让我们来添加文件,可以直接这样做:

  1. git add . # Or `git add hello.txt`, if we don't want all files 

如果你现在检查存储库状态,你将看到文件已添加(也称为已暂存),但尚未提交:

  1. git status  
  2. # On branch main  
  3. #   
  4. # No commits yet  
  5. #   
  6. # Changes to be committed:  
  7. #  (use "git rm --cached <file>..." to unstage)  
  8. #   new file:   hello.txt 

要记录更改,先提交:

  1. git commit -m "Add hello.txt"  
  2. # [main (root-commit) a07ee27] Adds hello.txt  
  3. # 1 file changed, 2 insertions(+)  
  4. # create mode 100644 hello.txt 

小提示:git commit -m <MESSAGE>是一个简写命令,你也可以使用git commit打开编辑器(主要是vim)并提供详细的提交描述。

让我们来检查更改记录:

  1. git log  
  2. # type :q to close 

将显示如下内容:

  1. commit a07ee270d6bd0419a50d1936ad89b9de0332f375 (HEAD -> main)  
  2. Author: Your Name <your@email.address>  
  3. Date:   Sun Jul 11 11:47:16 2021 +0200  
  4.     Adds hello.txt  
  5. (END)  

创建分支

在很多情况下,拥有单独版本的初始代码会很有用:例如在测试不确定的功能时,也可以在协同工作时避免代码冲突。这就需要git分支登场了:它是从历史记录的某个特定点发展开来的。

要创建分支可以运行git branch NAME,切换分支可以运行git checkout NAME。或者干脆就这样做:

  1. git checkout -b dev # switches to a new branch called "dev"  
  2. # Switched to a new branch 'dev'  
  3. # gitexample git:(dev) 

让我们更改hello.txt文件中的内容并提交更改:

  1. echo "\nHello, Git Branch" >> hello.txt &&  
  2. git commit -am "Change hello.txt" 

现在让我们切换回主版本:

  1. git checkout main &&  
  2. cat hello.txt  
  3. # Switched to branch 'main'  
  4. # Hello, Git 

如你所见,文件内容仍与原来相同。要比较分支,我们可以运行:

  1. git diff dev  
  2. # diff --git a/hello.txt b/hello.txt  
  3. # index 360c923..b7aec52 100644  
  4. # --- a/hello.txt  
  5. # +++ b/hello.txt  
  6. # @@ -1,3 +1 @@  
  7. # Hello, Git  
  8. # -  
  9. # -Hello, Git Branch  
  10. # (END)  
  11. # type ":q" to close 

让我们也对主分支进行更改:

  1. echo "\nHi from Main Branch" >> hello.txt &&  
  2. git commit -am "Change hello.txt from main"  
  3. # [main 9b60c4b] Change hello.txt from main  
  4. # 1 file changed, 2 insertions(+) 

现在让我们尝试合并更改:

  1. git merge dev  
  2. # Auto-merging hello.txt  
  3. # CONFLICT (content): Merge conflict in hello.txt  
  4. # Automatic merge failed; fix conflicts and then commit the result. 

因为文件在同一个地方被更改了两次,所以有了冲突。看文件:

  1. cat hello.txt  
  2. <<<<<<< HEAD  
  3. Hello, Git  
  4. Hi from Main Branch  
  5. =======  
  6. Hello, Git  
  7. >>>>>>> dev 

还有一个工具可以用来分别查看更改:

  1. git diff --ours # :q to close   
  2. git diff --theirs #:q to close 

你可以手动编辑文件并提交更改,但这里假设我们只需要其中一个版本。我们首先中止合并:

  1. git merge --abort 

并使用theirs策略重新开始合并,这意味着在发生冲突时,我们将始终使用传入分支的内容:

  1. git merge -X theirs dev  
  2. # Auto-merging hello.txt  
  3. # Merge made by the 'recursive' strategy.  
  4. # hello.txt | 5 +----  
  5. # 1 file changed, 1 insertion(+), 4 deletions(-) 

与此策略相反的是ours策略。将两个更改合并在一起需要手动编辑(或使用git mergetool)。

要查看所有分支的列表,请运行:

  1. git branch # type :q to close  
  2. #  dev  
  3. # * main 

最后,是如何删除分支:

  1. git branch -d dev  
  2. # Deleted branch dev (was 6259828). 

Rebase命令

分支从git历史记录中的特定点开始“生长”,rebase命令允许更改这些特定点。让我们创建另一个分支并再次对hello.txt添加一些更改:

  1. git checkout -b story &&  
  2. echo "Once upon a time there was a file">>story.txt &&  
  3. git add story.txt &&  
  4. git commit -m "Add story.txt"  
  5. # Switched to a new branch 'story'  
  6. # [story eb996b8] Add story.txt  
  7. # 1 file changed, 1 insertion(+)  
  8. # create mode 100644 story.txt 

现在,让我们回到主分支并添加更改:

  1. git checkout main &&  
  2. echo "Other changes" >> changes.txt &&  
  3. git add changes.txt &&  
  4. git commit -m "Add changes.txt" 

要重现我们在main中对story分支所做的更改,请运行:

  1. git checkout story &&  
  2. git rebase main  
  3. # Successfully rebased and updated refs/heads/story. 

你可以看到在main分支中创建的新文件被添加到story分支:

  1. ls  
  2. # changes.txt hello.txt   story.txt  

注意:不要重新rebase其他人可能使用过的分支,例如主分支。此外,请记住,远程存储库上的每个历史操作都需要强制这些更改生效。

远程存储仓库

如果你还没有存储库,那么请创建一个GitHub帐户,登录并创建一个新的空存储库(私有或公共)。

假设存储库名称是example,运行以下命令(用你自己的用户名替换):

  1. git remote add origin git@github.com:USERNAME/example.git &&  
  2. git push -u origin main 

你可以刷新页面并查看主分支中的文件。要将所有本地分支推送到远程存储库,请运行:

  1. git push --all origin 

让我们在GitHub上编辑一些内容:单击任意文件和pencil图标。你想要什么文本都可以通过一行代码添加进去,然后按Commit changes。

现在在本地运行此命令以获取远程更改:

  1. git checkout main &&  
  2. git pull 

管理未提交的更改

如果要保存本地更改以供以后使用,可以使用git stash:

  1. echo "Changes" >> hello.txt &&  
  2. git stash 

然后你可以使用以下命令来检查、应用或放弃这些更改:

  1. git stash list  
  2. # stash@{0}: WIP on main: 92354c8 Update changes.txt  
  3. git stash pop # to apply changes  
  4. git stash drop # to drop changes 

提示:你可以使用stash编号,即git stash pop 0来应用特定的stash或通过git stash drop 0来删除它。

如果你想放弃所有本地更改并简单地将存储库恢复到上次提交的更改,运行:

  1. git restore . 

管理已提交的更改

创建提交后,此更改将保存在本地git历史记录中。如前所述,所有影响远程历史的更改都需要git push --force。以下所有命令都值得一记。

让我们从编辑最后一条提交消息开始:

  1. git commit --amend # type :wq to save and close  
  2. # Press "i" to edit, "Esc" to stop editing 

我们把一切都重置到一开始怎么样?

要查找最开始提交的ID,请运行此命令并滚动(向下箭头)到最后:

  1. git log --abbrev-commit  
  2. # commit a07ee27  
  3. # Author: Your Name <your@email.address>  
  4. Date:   Sun Jul 11 11:47:16 2021 +0200  
  5.     Adds hello.txt  
  6. (END)  
  7. # type ":q" to close 

现在来重置存储库,但保持所有更改未暂存:

  1. git reset --soft COMMIT # e.g. a07ee27 

与此相反,你也可以使用git reset --hard COMMIT进行强制重置并摆脱所有更改。你可以从git文档中了解其他几种类型的重置。

别名

大多数情况下,你将只用到少量命令(大多数情况下为checkout、add、commit、pull、push 和merge),但多了解一些总是有备无患。

另一种方法是git别名。要配置别名,只需在配置中设置即可。例如,我经常使用的一个别名是git tree,它以树的形式打印漂亮的历史日志:

  1. git config --global alias.tree 'log --graph --decorate --pretty=oneline --abbrev-commit'  
  2. # Try it with `git tree` 

另一个有用的别名是删除所有合并的分支:

  1. git config --global alias.clbr '!git branch --merged | grep -v \* | xargs git branch -D'  

如你所见,它使用!作为前缀,这允许我们使用其他命令,而不仅仅是git命令。

今天就到这里,希望对你的开发之旅有所帮助。

相关内容