实用git使用cli

I decided on writing a tutorial on Git usage. Especially considering how popular remote work is now. When I began software development, I started with using a GUI but I made too many mistakes with it. Its really easy to screw up using that since there are too many options that you can easily click and I think I may have accidentally deleted some commits at some point. And well, using the CLI really feels quite cool. I won’t be going through setting up a repository since you can find a step by step guide anywhere. No really, there are steps on a new repository itself.

我决定编写有关Git使用的教程。 尤其要考虑到远程工作现在很流行。 当我开始软件开发时,我开始使用GUI,但是我犯了太多错误。 使用它确实很容易搞砸,因为您可以轻松单击太多选项,而且我认为我有时可能会意外删除了一些提交。 而且,使用CLI确实感觉很酷。 因为您可以在任何地方找到分步指南,所以我不会设置存储库。 并非如此,在新存储库本身上有一些步骤。

Using git with the command line is quite simple. For your everyday needs, you just need to know 4 basic commands and how to solve conflicts should they arise (which don’t come in often unless you work in a team on the same components).

在命令行中使用git非常简单。 对于您的日常需求,您只需要了解4条基本命令以及在冲突发生时如何解决冲突(除非您在同一组件中以团队形式工作,否则它们不会经常出现)。

I will try and explain the 4 commands that everyone should know and where certain commands have helped me with respect to the situations in which they arose.

我将尝试解释每个人都应该知道的4条命令,以及某些命令在出现情况时对我有帮助。

Note: I will be using Xcode as an IDE where necessary and don’t worry, the knowledge is transferrable! This isn’t something that is language specific!

注意:必要时我将Xcode用作IDE,不用担心,该知识是可转让的! 这不是特定于语言的!

Before we get started with the commands, something you need to keep in mind is that when talking about git, we talk about 2 “copies” of the same files. One is your local copy and one is on the server. Until you explicitly tell git to send stuff to the server, it will keep all the items on your machine.

在开始使用命令之前,您需要记住的一点是,在谈论git时,我们谈论的是同一文件的2个“副本”。 一个是您的本地副本,另一个是在服务器上。 除非您明确告诉git将内容发送到服务器,否则它将所有项目保留在您的计算机上。

One important command (according to me) before anything else is:

在我执行其他任何操作之前,一项重要的命令(根据我的说法)是:

git status

Sometimes you just need to know what all you need to do. What I mean by this is, if you have a new filed added, the command will tell you, you have files that need to be tracked. If you have added the file to tracking then it will tell you that there are changes that need to be committed. After committing them, it tells you that you are ahead of the remote i.e you have taken the latest code from the remote, worked on it and are yet to send it to the remote. Quite a useful command to re-orient yourself.

有时您只需要知道您需要做什么。 我的意思是,如果添加了新文件,该命令将告诉您,您有需要跟踪的文件。 如果您已将文件添加到跟踪中,那么它将告诉您有一些更改需要提交。 提交它们后,它会告诉您您位于远程服务器之前,即您已从远程控制器获取了最新代码,对其进行了处理,但尚未将其发送到远程控制器。 非常有用的命令来重新定向自己。

So the first few commands come in the following sequence for your daily tasks. I would strongly recommend following the sequence to make sure everything goes according to plan. If there are terms you don’t understand, just wait till you completely read everything.

因此,前几条命令按以下顺序执行您的日常任务。 我强烈建议您按照顺序进行操作,以确保一切均按计划进行。 如果您不了解某些术语,请等到完全阅读所有内容之后再试。

1. git add -A / git add。 (1. git add -A/git add .)

Image for post
Git add -A command. It has no output inherently and is used in tandem with git status
git add -A命令。 它本身没有输出,与git status一起使用

The above command informs git that all files in the current local git repository need to be tracked for changes. Without running this, git will never know about any changes made to a file. I recommend running this command every time unless you consciously decide to not track all files. Generally, you would want changes tracked in all files and any you don’t want, would go into .gitignore file.

上面的命令通知git,需要跟踪当前本地git存储库中的所有文件进行更改。 如果不运行此命令,git将永远不会知道对文件所做的任何更改。 我建议每次都运行此命令,除非您有意识地决定不跟踪所有文件。 通常,您希望跟踪所有文件中的更改,而您不希望的任何更改都将记录到.gitignore文件中。

.gitignore: This is a hidden file that, as the name suggests, has a list of files that git should ignore. Event running git add won’t add the files if there are present in .gitignore. This is useful to not have .DS_Store files or any random auto generated files automatically added on the remote. Note: You will have to make sure this file is present before adding files for tracking to git because once they are added then git will continue to track them even if they are present in the .gitignore and you will have to manually remove them.

.gitignore:顾名思义,这是一个隐藏文件,其中包含git应该忽略的文件列表。 事件运行git add 如果中存在文件,则不会添加文件。 gitignore 。 这对于不具有.DS_Store文件或任何自动自动生成的随机文件自动添加到遥控器上很有用。 注意:在向git添加要跟踪的文件之前,您必须确保该文件存在,因为一旦添加了文件,即使它们存在于git中,git也将继续跟踪它们。 gitignore ,您将必须手动删除它们。

2. git commit -m“提交消息” (2. git commit -m “Commit message”)

Because of the add command, git knows that it needs to track files. And only that. It doesn’t know that you want to save the changes you made to your local copy. Running this command does exactly that. Inside of the double quotes you write a message to identify what all changes/work you have done to be able to better determine a point you might need to revert your code back to. I cannot stress the importance of writing a good commit message, spending a few extra minutes is worth it. It can serve as a daily log for your manager to see what all work you have done too!

由于使用了add命令,git知道它需要跟踪文件。 而且只有那个。 它不知道您要保存对本地副本所做的更改。 运行此命令正是这样做的。 在双引号内,您将编写一条消息,以标识已完成的所有更改/工作,以便能够更好地确定将代码恢复到的点。 我不能强调写出良好的提交消息的重要性,花一些额外的时间是值得的。 它可以作为您经理的每日日志,以查看您还完成了哪些工作!

All the commands till now were simply for your local machine. And you haven’t actually sent anything to the server.

到目前为止,所有命令仅适用于本地计算机。 而且您实际上还没有发送任何东西到服务器。

Image for post
Git commit
Git提交
Image for post
No changes to commit!
无需更改即可提交!

3. git pull <远程> <分支> (3. git pull <remote> <branch>)

This command downloads any updates to the code pushed by someone on the server from somewhere else to your machine. You don’t necessarily have to do this but I would say just do it out of paranoia. Now you can push (more in the next point) without having to do a pull and the CLI will tell you that you need to do a pull first. But well…I am not entirely sure when this may happen but I have heard of people’s code being overwritten (maybe it is just hearsay but better safe than sorry).

此命令会将服务器上某人推送的代码的所有更新从其他地方下载到您的计算机。 您不必一定要这样做,但我会说只是出于偏执。 现在您可以进行推送(在下一点中有更多内容)而无需进行拉动,CLI会告诉您您需要先进行拉动。 但是,好吧……我不确定何时会发生这种情况,但是我听说人们的代码被覆盖了(也许只是传闻,但比后悔更安全)。

Image for post
Git pull for no changes on remote
git pull遥控器上没有任何变化
Image for post
Git pull when remote has changes
遥控器发生变化时的Git拉动

4. git push <远程> <分支>(4. git push <remote> <branch>)

Finally, this one pushes everything that you committed till now to your local machine will be pushed to the server.

最终,这会将您到目前为止提交的所有内容推送到本地计算机上。

Image for post
Git push when there are changes
有变化时Git推送

That is it. That is all you need to know for work on a daily basis! Just these 4. Typically a more senior member of the team will setup everything else and tell you where to push or take a pull from and you don’t have to worry about anything else! All you will have to do is copy the url of the repository and then run:

这就对了。 这就是您每天工作所需要知道的一切! 只需这些4.通常,团队中一名更高级的成员将设置其他所有内容,并告诉您从何处推动或退出,您不必担心其他任何事情! 您所需要做的就是复制存储库的URL,然后运行:

git clone insert_url_here

If you want want to know some more stuff like what branches, remotes, stash and other stuff is. Keep reading on.

如果您想了解更多东西,例如什么是分支,遥控器,隐藏物和其他东西。 继续阅读。

远程 (Remote)

Remote refers to the server url where your repository is hosted. The command you will typically use is git push origin master where origin is your remote url and master is the branch. Now you may wonder why these names. The term origin is a convention that typically denotes the original place where the code exists (I like to think of it this way) . What this means is you can use any other name! But you have to tell git that! You do this by running:

远程是指托管您的存储库的服务器URL。 您通常使用的命令是git push origin master 其中origin是您的远程URL,而master是分支。 现在您可能想知道为什么使用这些名称。 术语“起源”是一个约定,通常表示代码存在的原始位置(我喜欢这样考虑)。 这意味着您可以使用任何其他名称! 但是您必须告诉git! 您可以通过运行以下命令来执行此操作:

git remote add origin1001 insert_url_here.git

and the command becomes

命令变成

git push origin1001 master

Think of origin as nothing but a variable storing the url value. So you have two places to push the code to now, origin and origin1001 and these two are completely independent of each other!

将origin看作是存储url值的变量。 因此,您有两个位置可以将代码推送到现在, originorigin1001 ,这两个彼此完全独立!

Should you want, you can reset the url stored in any variable at any point of time. You need to run:

如果需要,您可以随时重置存储在任何变量中的url。 您需要运行:

git remote set-url origin insert_url_here.git

Now your origin will update the value store inside to the new url passed!

现在,您的来源将把内部的值存储更新为所传递的新网址!

(Branch)

Now in the example above, the branch is called master by convention as it is the root, the primary and the original branch that will have other branches flowing out of. You can set any other branch to be the default branch and rename it even but I would strongly suggest not doing this. Since these are things other people working on your code will expect.

现在在上面的示例中,按照惯例,该分支称为分支,因为它是根分支,主要分支和原始分支,其他分支将流出。 您可以将任何其他分支设置为默认分支,甚至对其重命名,但我强烈建议您不要这样做。 由于这些是其他在您的代码上工作的人所期望的。

Think of master as the trunk of a tree. There can be other branches but conventionally the master branch will have the most stable and working code. And all other team members will be working on branches of master. To create a new branch, just do:

主人当作一棵树的树干。 可以有其他分支,但通常,master分支将具有最稳定和最有效的代码。 其他所有团队成员将在master分支上工作 要创建一个新分支,只需执行以下操作:

git checkout -b branch_name

To view the list of all available branches type:

要查看所有可用分支的列表,请输入:

git branch
Image for post
On branch development. Denoted by the text in green and asterisk.
关于分支发展。 由绿色和星号表示。

And to switch between branches you need to do:

要在分支之间切换,您需要执行以下操作:

git checkout branch_to_go_to

Work on the second branch will be completely independent from the original branch. The second branch will contain code from the point you created it out of the original and that is it. The new branch will not care about the other and vice versa.

第二个分支的工作将完全独立于原始分支。 第二个分支将包含您从原始位置创建的代码,也就是它。 新分支将不关心另一个分支,反之亦然。

合并中 (Merging)

Let us say you have done work on branch development after checking it out from the master branch. After doing some work on development you are ready to release the code. So what you can do now is go to the master branch using checkout. And once you are on master, you can do:

假设您从master分支签出后,您就完成了分支开发工作。 在完成一些开发工作之后,您就可以发布代码了。 因此,您现在可以使用checkout转到master分支 一旦您掌握了,就可以:

git merge development

This will merge all the committed code on development to your master branch along with the commit history.

这会将开发中的所有已提交代码与提交历史记录合并到您的master分支中。

Make sure you do a git push afterwards to push all your changes to the remote.

确保随后执行git push将所有更改推送到遥控器。

(Stash)

The stash is basically a place where you send all the changes you don’t want to commit. This can be useful when you accidentally have done some work on a different branch than the one you were supposed to (I have done this more times than I can count). Simply run this on the branch who’s uncommitted code you want to temporarily say bye to:

基本上,存储是您发送所有不想提交的更改的地方。 当您意外地在与预期分支不同的分支上完成了某些工作时,这可能会很有用(我完成此操作的次数超出了我的预期)。 只需在您要暂时告别的未提交代码的分支上运行此代码即可:

git stash

Some people think that this command deletes the modifications, what it really does is that it takes all the changes and stores them separately and these modifications can be re-applied at a later date.

有人认为此命令删除了修改,实际上是将所有更改都保存并分开存储,这些修改可以在以后重新应用。

To re-apply the stashed changes:

要重新应用隐藏的更改:

git stash apply

This will pop the most recent stash from the top of the stack and apply it to the code.

这将从堆栈顶部弹出最近的存储,并将其应用于代码。

还要别的吗? (Anything else?)

git log

Above command will provide you with a list of all your commits on the branch you are on. The information provided involves the commit id, author name and commit message. This is why giving a detailed commit message is quite important as this is the only way of knowing what you did when you committed the code and want to revert to it.

上面的命令将为您提供您所在分支上所有提交的列表。 提供的信息涉及提交ID,作者姓名和提交消息。 这就是为什么提供详细的提交消息非常重要的原因,因为这是知道在提交代码并希望还原为代码时所做操作的唯一方法。

Image for post
Just one commit message for now but typically this will be a huge list
现在只有一个提交消息,但是通常这将是一个巨大的列表

These are the few commands that I think every software developer should know. The add, commit, pull and push commands in that particular order are key for a beginner. Once you get more accustomed to them then I would recommend moving onto the subsequent ones.

这些是我认为每个软件开发人员都应该知道的几个命令。 按此特定顺序执行add,commit,pull和push命令是初学者的关键。 一旦您习惯了它们,我建议您继续学习后续的内容。

This was all for this guide, I’ll introduce some more problem specific items in the next guide because I don’t want to make this one unbearably long. I think that a majority of these problems are faced by a good number of people and I have seen people take the manual approach of deletion, adding code and copying files instead of relying on git. So stay tuned!

这就是本指南的全部内容,在下一本指南中,我将介绍一些特定于问题的项目,因为我不想将其过长。 我认为这些问题中的大多数都是许多人面临的,并且我已经看到人们采用手动删除,添加代码和复制文件的方法,而不是依靠git。 敬请期待!

翻译自: https://medium.com/swlh/practical-git-using-cli-8a7fad448e06

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值