Git — 运行 git 所需的命令!

2025-06-08

Git — 运行 git 所需的命令!

首先,让我们确保已安装 git — 如果没有,请检查下面与您的系统相关的链接:

出于本文的目的,我将遵循您最有可能用于新存储库的流程,尽管某些步骤将用于现有存储库。

另外,对于其他命令——例如创建新目录时,我将使用 BASH shell 命令。这里有一个方便的参考

创建一个新的存储库:

首先让我们创建一个新目录来保存我们的存储库:

mkdir newRepo
Enter fullscreen mode Exit fullscreen mode

现在我们将进入新目录:

cd newRepo
Enter fullscreen mode Exit fullscreen mode

我们通过以下命令将其创建为一个新的 git 存储库:

git init
Enter fullscreen mode Exit fullscreen mode

要克隆现有存储库:

从服务器即 GitHub:

git clone https://github.com/path/to/repository.git
Enter fullscreen mode Exit fullscreen mode

例如,从 这里克隆引导启动器模板:

git clone https://github.com/BlackrockDigital/startbootstrap-creative.git
Enter fullscreen mode Exit fullscreen mode

您将看到绿色按钮“克隆或下载”:

或者 …

从本地计算机上的现有存储库:

git clone /path/to/repository
Enter fullscreen mode Exit fullscreen mode

向 git 添加文件:

现在我们已经设置了一个存储库,我们需要添加一些文件供 git 跟踪:

添加目录中所有新的和修改/编辑的文件:

git add .
Enter fullscreen mode Exit fullscreen mode

现在,我们要求 git 添加文件,然后我们需要提交这些文件:

git commit -m "add these changes" 
Enter fullscreen mode Exit fullscreen mode

在上面的命令中我们说了几件事:

git commit ... //commit the added files.
Enter fullscreen mode Exit fullscreen mode
... -m "add these changes" //-m will precede a message which will let people know what changes are included in this commit.
Enter fullscreen mode Exit fullscreen mode

将我们的提交/更改推送到远程存储库:

一旦我们添加提交了文件,我们现在需要将这些更改发送或推送到我们的存储库:

git push origin master
Enter fullscreen mode Exit fullscreen mode

上述命令的含义如下:

git push ... //send our changes.
Enter fullscreen mode Exit fullscreen mode
... origin ... //the location of our repository.
Enter fullscreen mode Exit fullscreen mode
... master //the _branch_ name, we will talk more about branches shortly.
Enter fullscreen mode Exit fullscreen mode

分支机构:

假设您正在构建一个可能包含多个不同功能的应用或项目。好的做法是将应用开发拆分成多个单独的功能。

假设我们正在构建一个应用程序,我们需要编写的代码功能之一可能是登录系统:

使用git我们可以创建一个名为login的新分支

git checkout -b login
Enter fullscreen mode Exit fullscreen mode

在上面的命令中:

git checkout //switch to the following branch name.
Enter fullscreen mode Exit fullscreen mode
... -b login //-b will precede the new branch name.
Enter fullscreen mode Exit fullscreen mode

上述命令实际上是一个精简的命令,因为它实际上是在一次性签出创建一个新分支。

如果我们已经创建了一个名为login的分支,我们可以通过以下方式切换到它:

git checkout login
Enter fullscreen mode Exit fullscreen mode

推动分支:

上面我们只是创建了一个本地分支。为了让其他人可以访问它,我们需要将其推送到远程仓库:

git push origin login
Enter fullscreen mode Exit fullscreen mode

拉动:

假设我们需要检索远程存储库的所有最新更新 - 例如,如果您和您的团队在不同的机器上处理同一个项目,另一个团队成员可能做了一些更改,而您想确保您是最新的:

git pull // tells git to retrieve the latest version.
Enter fullscreen mode Exit fullscreen mode

合并:

如果我们想要我们之前在登录分支中所做的提交合并到工作分支中:

让我们首先检查一下我们的分支:

git checkout master
Enter fullscreen mode Exit fullscreen mode

然后我们可以将新分支合并到:

git merge login
Enter fullscreen mode Exit fullscreen mode

在发出合并命令之后,git系统会将分支进行合并,但是git并不总是能够自动实现这一点。

如果您需要自己解决这些冲突,这里有一个易于遵循的指南

撤消更改:

您肯定会遇到需要撤消对本地存储库的更改的情况 - 例如,您想要删除对名为index.html的文件所做的更改:

git checkout -- index.html
Enter fullscreen mode Exit fullscreen mode

如果您想删除所有更改并恢复到最新的远程版本:

git fetch origin //get the latest remote version.
Enter fullscreen mode Exit fullscreen mode
git reset --hard origin/master //remove all local changes.
Enter fullscreen mode Exit fullscreen mode

有用的链接:

当您可以轻松地探索git 的更多功能(有很多)时,这些是一些有价值的资源:

我将会发布更多有关更高级功能的帖子,但现在,就这些了……

如果您有任何问题或补充,请在下面的评论中告诉我或在Twitter上联系我。

〜戴夫。

鏂囩珷鏉ユ簮锛�https://dev.to/davedodea/git--commands-you-need-to-git-going-3mpg
PREV
Git 工作流:您是否致力于掌握您的个人项目?
NEXT
深入探究 Kubernetes Schema 验证