![]() ![]() Note that unlike a regular, safe git checkout, this particular mode will destroy the current contents of the named files even if they are not saved anywhere else. Having copied the file(s) you named from the commit you named into the index, this git checkout goes on to write the files into your work-tree. ![]() The commit-hash part can be anything acceptable to git rev-parse, as long as it names a commit or tree object internally a commit hash is good here. The - itself is only there to make sure that these file names don't look like flags or branch names or whatever. The paths argument you give, after the -, determines which files come out of the chosen commit. It's important to remember Git's index here, because this kind of git checkout first writes the files to the index. The one we care about here is the one that checks out particular files from particular commits. It has, depending on how you count, something like four to seven different modes of operation. The git checkout command is absurdly complex. We'll get to that in a moment, but first let's talk more about git checkout. This is where Git 2.23 and later are better, because there's a separate command for this, git restore. Well, that's all well and good, but now you want some particular file out of one particular commit, without switching commits at all. Git now remembers which branch you're on, and making a new commit will write the new commit's hash ID into that branch name. When you use git checkout branch-name( or again git swtich), Git does the same thing, except that now the special name HEAD is attached to the branch name. This reads the chosen commit into the index-the index, or staging area, holds your proposed next commit so switching to a commit requires filling it in from that commit-and in the process, adjusts the contents of your work-tree to match the commit you've switched-to. (or since Git 2.23, the same thing with git switch), you ask Git to switch to the given commit as a detached HEAD. The details get a little sticky, although the new git restore probably does exactly what you want, right out of the box, as it were: git restore - path/to/filename1 path/to/filename2Įach commit in a Git repository is (or holds, really) a snapshot of all of your files. You can extract particular files from particular commits this way, without actually changing commits. ![]() This is the most flexible option, but despite the name, it doesn't modify files.You probably ( probably, this can get complicated) want a particular mode of git checkout or, in Git 2.23 or later, git restore, here. git reset -mixed, which is the default, and keeps all files the same but unstages the changes.Only use this if you know what you're doing. git reset -hard, which will completely destroy any changes and remove them from the local directory.git reset -soft, which will keep your files, and stage all changes back automatically.They all rewrite Git history, and they all move the HEAD back, but they deal with the changes differently: There are three different kinds of resets in Git, and they all differ based on how they handle the commits that get left hanging. Note that this is different from git revert Works, but resetting the HEAD and then only staging the correct changes will achieve this. This can be very hard to figure out if you don't know how git reset Or perhaps you accidentally made a commit that included some changes you didn't want to track. Back to the previous commit, and then re-commit correctly. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |