常见的内存泄漏陷阱
Whether you’re new to git or a seasoned veteran there are a few pitfalls that will snare us all on occasion. Nothing too serious, more on the side of frustrating; and likely the cause of embarrassment as opposed to actual danger. After all, git is designed to avoid the most tragic of errors.
无论您是git新手还是经验丰富的资深人士,都有一些陷阱有时会使我们所有人陷入困境。 没什么太严重的,更多的是令人沮丧的一面; 可能是尴尬的原因,而不是实际的危险。 毕竟,git旨在避免出现最悲惨的错误。
If you are brand new to git some of the terminology may seem unfamiliar. However, I think that if you’ve found your way here then you will be able to decipher any foreign words through context clues. So I won’t take any more of your time telling you how I’ve almost managed to lose entire repos, or merge a bug into the master branch.
如果您是git的新手,那么某些术语可能并不熟悉。 但是,我认为,如果您在这里找到了自己的出路,那么您将能够通过上下文提示来破译任何外来词。 因此,我将不再花更多时间告诉您如何几乎失去了整个存储库,或将错误合并到master分支中。
Your day-to-day git work-flow is going to involve six basic commands.
您的日常git工作流程将涉及六个基本命令。
git fetch—make your code aware of what has happened in the remote repository.
git fetch使您的代码了解远程存储库中发生的事情。
git pull — get your code in sync with the latest commits from the remote repository.
git pull —使您的代码与远程存储库中的最新提交保持同步。
git status — see what changes you’ve made on your local machine since your last commit.
git status查看自上次提交以来在本地计算机上进行的更改。
git add *filename* —stage(prepare) the files name after “git add” for committing. You will most likely be using a dot . instead of listing the actual files names but I’ll let this stackoverflow answer explain that option.
git add * filename *-在“ git add”之后暂存(准备)文件名以进行提交。 您很可能会使用点. 而不是列出实际的文件名,而是让这个stackoverflow答案解释该选项。
git commit -m “A message.” — commits the changes you have made to your local repository. Get in the habit of always providing a message; of what, not how.
git commit -m “A message.” —提交对本地存储库所做的更改。 养成始终提供信息的习惯; 什么,而不是如何
git push — replaces your remote repo with your local repo, overwriting the remote repo. I realize there is a lot of unpacking to do here but the base command is a push followed by the industry standard origin then the branch name, perhaps master.
git push —用本地仓库替换远程仓库,覆盖远程仓库。 我意识到这里有很多解压缩工作,但是基本命令是按推,然后是行业标准origin然后是分支名称,也许是master名称。
Let’s assume you’re not committing your code as often as you probably should. Often times you will not catch this mistake until you begin to push your code and have to wonder for a split second why you’re getting this error:
假设您没有像可能那样频繁地提交代码。 通常,直到您开始推送代码并不得不想知道为什么会收到此错误时,您才会发现此错误:
Hopefully you checked you remote repository with git remote -v and saw the repository that you cloned was the repository that you meant to fork first.
希望您使用git remote -v检查了远程存储库,并看到克隆的存储库是您本应首先派生的存储库。
There are a few ways to go about this but I find it most assuring to first delete the current remote repository with git remote rm *repo/branch name*.
有几种解决方法,但我发现最确保首先使用git remote rm * repo / branch name *删除当前的远程存储库。
Now you can check your remotes again git remote -v and see that the specified repository is no longer an option.
现在,您可以再次检查您的遥控器git remote -v ,看看指定的存储库不再是一个选项。
Next we add a new remote with git remote add origin *link to the new repo*. You can also add a new branch by using the branch name in place of “origin” in the command. See more about this git command in this stackoverflow answer.
接下来,我们添加一个带有git remote add origin *链接到新仓库*的新遥控git remote add origin 。 您也可以使用分支名称代替命令中的“ origin”来添加新分支。 在此stackoverflow答案中查看有关此git命令的更多信息。
As troublesome as it is common. The error can come in many forms, here’s one example:
像平常一样麻烦。 错误可能以多种形式出现,这是一个示例:
This error might be simple, or it could be more complex. that all depends on how you want to handle your local changes. In this article I’m assuming you want a quick fix.
该错误可能很简单,也可能更复杂。 这一切都取决于您要如何处理本地更改。 在本文中,我假设您需要快速修复。
In that case git stash is your best friend. Your local repo is reset to the last pull and as the command implies, the differences are not entirely deleted but stashed. So you can access the changes later.
在这种情况下, git stash是您最好的朋友。 您的本地存储库将重置为最后一次提取,并且如命令所示,差异不会被完全删除而是被隐藏。 因此,您以后可以访问更改。
Let’s say you have changes that you want to keep but you also want access to the most recent copy of the remote repo to work with; start by running git fetch then git rebase origin/ *branch name*. Here is a great stackoverflow post on the how and why of these commands.
假设您有要保留的更改,但还希望访问远程回购的最新副本以进行处理; 首先运行git fetch然后运行git fetch git rebase origin/ *分支名称*。 这是一篇有关如何以及为什么使用这些命令的重要文章。
If you’re like me you were chasing a curiosity, created a practice file, and don’t care about the changes or even that new file. You just want to quickly get back to the state at the last pull. git clean -f -d or it’s equivalent git reset --hard HEAD will permanently erase the changes and you’re all set to pull down the remote repository’s updates.
如果您像我一样,则是出于好奇,创建了练习文件,而不管更改或什至是新文件。 您只想快速回到上一次拉动时的状态。 git clean -f -d或等效的git reset --hard HEAD将永久删除更改,并且您都准备好拉下远程存储库的更新。
^ Nᴏᴛᴇ; ᴛʜᴇ -ғ ᴏᴘᴛɪᴏɴ ᴄᴀɴ ʙᴇ ᴛʜᴏᴜɢʜᴛ ᴏғ ᴀs “ғᴏʀᴄᴇ” ᴀɴᴅ ᴛʜᴇ -ᴅ ᴡɪʟʟ ʙᴇ sᴜʀᴇ ᴛᴏ ɪɴᴄʟᴜᴅᴇ sᴜʙᴅɪʀᴇᴄᴛᴏʀɪᴇs ᴀs ᴡᴇʟʟ. Iғ ʏᴏᴜ’ʀᴇ ғᴇᴇʟɪɴɢ ᴛɪᴍɪᴅ ᴛʜᴀᴛ ᴛʜɪs ᴡɪʟʟ ᴅᴇʟᴇᴛᴇ ᴛᴏᴏ ᴍᴜᴄʜ, ʀᴜɴ ɢɪᴛ ᴄʟᴇᴀɴ -ɴ ᴛᴏ sᴇᴇ ᴡʜᴀᴛ ᴡᴏᴜʟᴅ ʙᴇ ᴅᴇʟᴇᴛᴇᴅ ɪғ ʏᴏᴜ ᴡᴇʀᴇ ᴛᴏ ʀᴜɴ ᴛʜᴇ ᴄᴏᴍᴍᴀɴᴅ ɪɴ ᴇᴀʀɴᴇsᴛ. Fᴜʟʟ ᴅᴏᴄᴜᴍᴇɴᴛᴀᴛɪᴏɴ ғᴏʀ ᴛʜɪs ᴄᴏᴍᴍᴀɴᴅ ᴄᴀɴ ʙᴇ ғᴏᴜɴᴅ ʜᴇʀᴇ.
^Nᴏᴛᴇ; ᴛʜᴇғᴏᴘᴛғᴏʀᴄᴇᴀғᴏʀᴄᴇғᴏʀᴄᴇғᴏʀᴄᴇғᴏʀᴄᴇғᴏʀᴄᴇғᴏʀᴄᴇғᴏʀᴄᴇғᴏʀᴄᴇғᴏʀᴄᴇғᴏʀᴄᴇғᴏʀᴄᴇғᴏʀᴄᴇғᴏʀᴄᴇғᴏʀᴄᴇғᴏʀᴄᴇғᴏʀᴄᴇғᴏʀᴄᴇᴀ Iғʏᴏᴜ'ʀᴇғᴇᴇʟɪɴɢᴛɪsɪssɪsᴍᴜᴄʜᴍᴜᴄʜᴍᴜᴄʜᴍᴜᴄʜᴄʟᴇᴀɴᴄʟᴇᴀɴᴄʟᴇᴀɴɴɴᴅᴇʟᴇᴛᴇᴅᴅᴇʟᴇᴛᴇᴅғғғɴᴇᴀʀɴᴇᴇᴀʀɴᴇᴇᴀʀɴᴇᴛᴛᴛᴛ Fᴜʟʟᴅᴏᴄᴜᴍᴇɴᴛᴀᴛɪᴏɴᴛʜɪsᴄᴏᴍᴍᴀɴᴅʙᴇғᴏᴜɴᴅʜᴇʀᴇ。
You have a new app idea. You make the directory,mkdir worlds-best-app and immediately run git init because you’re responsible only you forgot to run cd worlds-best-app first.
您有一个新的应用创意。 您创建目录mkdir worlds-best-app并立即运行git init因为您只负责忘记先运行cd worlds-best-app才有责任。
Depending where you ran git init your IDE might be telling showing this:
根据您运行git init您的IDE可能会显示以下内容:
Save the gallon of ice cream for a real emergency because you’re going to undo this issue with a simple command rm -rf .git
保存一加仑的冰淇淋以备不时之需,因为您将使用一个简单的命令rm -rf .git来解决此问题。
That’s right, we’re simply going to remove the .git file so changes will stop being tracked in this directory and all of it’s subdirectories.
没错,我们只是删除.git文件,因此将停止在此目录及其所有子目录中跟踪更改。
Triple check that you are running your command from the correct directory. The majority of errors, especially as a beginner, will result from running your command in the wrong directory.
三重检查您是否正在从正确的目录运行命令。 大多数错误,尤其是对于初学者而言,是由于在错误目录中运行命令而导致的。
Read your errors! Then use those errors to guide you to success or use them as search criteria on stackoverflow. Git, as a version control software is so pervasive and has been around long enough that you problem has already been encountered before.
阅读您的错误! 然后使用这些错误指导您成功或将它们用作stackoverflow的搜索条件。 Git作为一种版本控制软件如此普及,并且已经存在了很长时间,以至于您之前已经遇到过问题。
DON’T PANIC!! Git has a steep but short learning curve. Many developers work for years and will stray from the six commands listed at the top but a very little.
不要惊慌! Git有一个陡峭但短暂的学习曲线。 许多开发人员已经工作了多年,并且会偏离顶部列出的六个命令,但很少。
Practice. Find a friend or a second computer(a bit redundant) and go through the easy commands until they are smooth and comfortable. Then create edge cases, mistakes, and bugs. Then fix or deal with them.
实践。 找到朋友或第二台计算机(有点多余),并通过简单的命令进行操作,直到它们变得平稳舒适。 然后创建极端情况,错误和错误。 然后修复或处理它们。
翻译自: https://medium.com/@hyrum.butler3/3-common-git-pitfalls-and-how-to-self-correct-ea9bfdfcb564
常见的内存泄漏陷阱