git每个开发人员一个分支_每个开发人员都应该知道的10个Git命令

git每个开发人员一个分支

Git is an important part of daily programming (especially if you're working with a team) and is widely used in the software industry.

Git是日常编程的重要组成部分(尤其是与团队合作时),并且在软件行业中得到广泛使用。

Since there are many various commands you can use, mastering Git takes time. But some commands are used more frequently (some daily). So in this post, I will share and explain the 10 most used Git commands that every developer should know.

由于可以使用许多命令,因此精通Git需要花费时间。 但是某些命令使用得更频繁(每天一些)。 因此,在本文中,我将分享和解释每个开发人员都应该知道的10个最常用的Git命令。

Note: To understand this article, you need to know the basics of Git.

注意:要理解本文,您需要了解Git的基础知识。

1. Git克隆 (1. Git clone)

Git clone is a command for downloading existing source code from a remote repository (like Github, for example). In other words, Git clone basically makes an identical copy of the latest version of a project in a repository and saves it to your computer.

Git clone是用于从远程存储库(例如,Github)下载现有源代码的命令。 换句话说,Git克隆基本上会在存储库中复制项目的最新版本,并将其保存到您的计算机中。

There are a couple of ways to download the source code, but mostly I prefer the clone with https way:

有两种下载源代码的方法,但是大多数情况下,我更喜欢使用https方式进行克隆

git clone <https://name-of-the-repository-link>

For example, if we want to download a project from Github, all we need to do is click on the green button (clone or download), copy the URL in the box and paste it after the git clone command that I've shown right above.

例如,如果我们想从Github下载项目,我们要做的就是单击绿色按钮(克隆或下载),复制框中的URL,然后将其粘贴到我正确显示的git clone命令之后以上。

This will make a copy of the project to your local workspace so you can start working with it.

这会将项目复制到您的本地工作空间,以便您可以开始使用它。

2. Git分支 (2. Git branch)

Branches are highly important in the git world. By using branches, several developers are able to work in parallel on the same project simultaneously. We can use the git branch command for creating, listing and deleting branches.

在git世界中,分支非常重要。 通过使用分支,几个​​开发人员可以同时在同一个项目上并行工作。 我们可以使用git branch命令创建,列出和删除分支。

Creating a new branch:

创建一个新分支:

git branch <branch-name>

This command will create a branch locally. To push the new branch into the remote repository, you need to use the following command:

此命令将在本地创建一个分支。 要将新分支推送到远程存储库,您需要使用以下命令:

git push -u <remote> <branch-name>

Viewing branches:

查看分支:

git branch or git branch --list

Deleting a branch:

删除分支:

git branch -d <branch-name>

3. Git结帐 (3. Git checkout)

This is also one of the most used Git commands. To work in a branch, first you need to switch to it. We use git checkout mostly for switching from one branch to another. We can also use it for checking out files and commits.

这也是最常用的Git命令之一。 要在分支中工作,首先需要切换到该分支。 我们主要使用git checkout从一个分支切换到另一个分支。 我们还可以使用它来检出文件和提交。

git checkout <name-of-your-branch>

There are some steps you need to follow for successfully switching between branches:

要在分支之间成功切换,需要遵循一些步骤:

  • The changes in your current branch must be committed or stashed before you switch

    在切换之前,必须先提交或保存当前分支中的更改
  • The branch you want to check out should exist in your local

    您要签出的分支机构应该在本地

There is also a shortcut command that allows you to create and switch to a branch at the same time:

还有一个快捷命令,可让您同时创建并切换到分支:

git checkout -b <name-of-your-branch>

This command creates a new branch in your local (-b stands for branch) and checks the branch out to new right after it has been created.

此命令在本地创建一个新分支(-b代表分支),并在创建分支后立即将其检出到新分支。

4. Git状态 (4. Git status)

The Git status command gives us all the necessary information about the current branch. 

Git status命令为我们提供了有关当前分支的所有必要信息。

git status

We can gather information like:

我们可以收集如下信息:

  • Whether the current branch is up to date

    当前分支是否最新
  • Whether there is anything to commit, push or pull

    是否有任何要提交,推动或拉动的东西
  • Whether there are files staged, unstaged or untracked

    是否已暂存,未暂存或未跟踪的文件
  • Whether there are files created, modified or deleted

    是否创建,修改或删除文件

5. Git添加 (5. Git add)

When we create, modify or delete a file, these changes will happen in our local and won't be included in the next commit (unless we change the configurations).

当我们创建,修改或删除文件时,这些更改将在本地进行,并且不会包含在下一次提交中(除非我们更改配置)。

We need to use the git add command to include the changes of a file(s) into our next commit. 

我们需要使用git add命令将文件的更改包括到我们的下一次提交中。

To add a single file:

要添加单个文件:

git add <file>

To add everything at once:

一次添加所有内容:

git add -A

When you visit the screenshot above in the 4th section, you will see that there are file names that are red - this means that they're unstaged files. The unstaged files won't be included in your commits.

当您访问上面第四部分中的屏幕截图时,您会看到其中的文件名是红色的-这意味着它们是未暂存的文件。 未暂存的文件不会包含在您的提交中。

To include them, we need to use git add:

要包括它们,我们需要使用git add:

Important: The git add command doesn't change the repository and the changes are not saved until we use git commit.

重要提示:git add命令不会更改存储库,并且更改将不会保存,直到我们使用git commit为止。

6. Git提交 (6. Git commit)

This is maybe the most-used command of Git. Once we reach a certain point in development, we want to save our changes (maybe after a specific task or issue).

这可能是最常用的Git命令。 一旦达到开发的特定点,我们就希望保存更改(可能在特定任务或问题之后)。

Git commit is like setting a checkpoint in the development process which you can go back to later if needed.

Git commit就像在开发过程中设置检查点一样,如果需要,您可以稍后再返回。

We also need to write a short message to explain what we have developed or changed in the source code.

我们还需要写一条简短的消息来解释我们在源代码中开发或更改的内容。

git commit -m "commit message"

Important: Git commit saves your changes only locally.

重要:Git提交仅在本地保存更改。

7. Git推 (7. Git push)

After committing your changes, the next thing you want to do is send your changes to the remote server. Git push uploads your commits to the remote repository.

提交更改后,下一步要做的就是将更改发送到远程服务器。 Git push将您的提交上传到远程存储库。

git push <remote> <branch-name>

However, if your branch is newly created, then you also need to upload the branch with the following command:

但是,如果分支是新创建的,则还需要使用以下命令上载分支:

git push --set-upstream <remote> <name-of-your-branch>

or

要么

git push -u origin <branch_name>

Important: Git push only uploads changes that are committed.

重要提示:Git push仅上传已提交的更改。

8. Git拉 (8. Git pull)

The git pull command is used to get updates from the remote repo. This command is a combination of git fetch and git merge which means that, when we use git pull, it gets the updates from remote repository (git fetch) and immediately applies the latest changes in your local (git merge).

git pull命令用于从远程仓库获取更新。 此命令是git fetchgit merge的组合,这意味着,当我们使用git pull时,它将从远程存储库(git fetch)获取更新,并立即在本地应用最新的更改(git merge)。

git pull <remote>

This operation may cause conflicts that you need to solve manually.

此操作可能会导致您需要手动解决的冲突。

9. Git还原 (9. Git revert)

Sometimes we need to undo the changes that we've made. There are various ways to undo our changes locally or remotely (depends on what we need), but we must carefully use these commands to avoid unwanted deletions.

有时我们需要撤消所做的更改。 有多种方法可以在本地或远程撤消更改(取决于我们需要什么),但是我们必须谨慎使用这些命令,以避免不必要的删除。

A safer way that we can undo our commits is by using git revert. To see our commit history, first we need to use git log -- oneline:

一种更安全的撤消提交的方法是使用git revert 。 要查看我们的提交历史,首先我们需要使用git log-oneline:

Then we just need to specify the hash code next to our commit that we would like to undo:

然后,我们只需要在要撤消的提交旁边指定哈希码即可:

git revert 3321844

After this, you will see a screen like below - just press shift + q to exit:

之后,您将看到如下屏幕-只需按shift + q即可退出:

The Git revert command will undo the given commit, but will create a new commit without deleting the older one:

Git revert命令将撤消给定的提交,但是将创建一个新的提交而不删除旧的提交:

The advantage of using git revert is that it doesn't touch the commit history. This means that you can still see all of the commits in your history, even the reverted ones. 

使用git revert的优点是它不会触及提交历史。 这意味着您仍然可以查看历史记录中的所有提交,甚至包括已还原的提交。

Another safety measure here is that everything happens in our local system unless we push them to the remote repo. That's why git revert is safer to use and is the preferred way to undo our commits.

这里的另一个安全措施是,除非我们将它们推送到远程存储库,否则一切都会在我们的本地系统中发生。 这就是为什么git revert更安全地使用,并且是撤消提交的首选方法。

10. Git合并 (10. Git merge)

When you've completed development in your branch and everything works fine, the final step is merging the branch with the parent branch (dev or master). This is done with the git merge command.

当您在分支中完成开发并且一切正常之后,最后一步是将分支与父分支(dev或master)合并。 这是通过git merge命令完成的。

Git merge basically integrates your feature branch with all of its commits back to the dev (or master) branch. It's important to remember that you first need to be on the specific branch that you want to merge with your feature branch.

Git merge基本上将功能分支及其所有提交集成回dev(或master)分支。 重要的是要记住,首先需要位于要与功能分支合并的特定分支上。

For example, when you want to merge your feature branch into the dev branch:

例如,当您要将功能分支合并到dev分支中时:

First you should switch to the dev branch:

首先,您应该切换到dev分支:

git checkout dev

Before merging, you should update your local dev branch:

合并之前,您应该更新本地dev分支:

git fetch

Finally, you can merge your feature branch into dev:

最后,您可以将功能分支合并到dev中:

git merge <branch-name>

Hint: Make sure your dev branch has the latest version before you merge your branches, otherwise you may face conflicts or other unwanted problems.

提示:在合并分支之前,请确保您的dev分支具有最新版本,否则您可能会遇到冲突或其他不必要的问题。

So these are my 10 most-used git commands that I come across in my daily programming. There are many more things to learn about Git and I will explain them later in separate articles.

这些是我日常编程中遇到的10个最常用的git命令。 有关Git的更多信息,我将在稍后的单独文章中进行解释。

If you want to learn more about web development, feel free to follow me on Youtube!

如果您想了解有关Web开发的更多信息,请随时 在YouTube上关注我

Thank you for reading!

感谢您的阅读!

翻译自: https://www.freecodecamp.org/news/10-important-git-commands-that-every-developer-should-know/

git每个开发人员一个分支

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值