![]() This is what I use often: git fetch upstream develop If you accidentally do this on the current branch (and git will not keep you from this), you may become confused, because the new branch content does not match the working tree, which did not change (to fix, update the branch again, to where it was before). Use caution when doing this, though, and use gitk or a similar tool to double check source and destination. This is especially helpful if multiple branches need to be updated to new remote heads. It simply moves mybranch's head to another commit, whatever is given as the second argument. This method leaves the checked out branch as it is, and the working tree untouched. If branch "mybranch" is not currently checked out, to reset it to remote branch "myremote/mybranch"'s head, you can use this low-level command: git update-ref refs/heads/mybranch myremote/mybranch Since there is at least one "duplicate" question, Reset branch completely to repository state, which does not assume that the branch is checked out, here's an alternative: In comments, OP hap497 clarified that the branch is indeed checked out, but this is not explicitly required by the original question. Previous answers assume that the branch to be reset is the current branch (checked out). Otherwise, you should be aware that it's not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular). Did you recently push into your local repo? If not, then no worries - something else must have caused these files to unexpectedly end up modified. Note that the first example assumes that the remote repo's name is "origin" and that the branch named "master" in the remote repo matches the currently checked-out branch in your local repo.īTW, this situation that you're in looks an awful lot like a common case where a push has been done into the currently checked out branch of a non-bare repository. Now your work is saved on the branch "my-saved-work" in case you decide you want it back (or want to look at it later or diff it against your updated branch). If you want to save your current branch's state before doing this (just in case), you can do: git commit -a -m "Saving my work, just in case" Running your test script with the proposed checkout change outputs: Initialized empty Git repository in /home/user/repo/.Setting your branch to exactly match the remote branch can be done in two steps: git fetch origin to git commit -q and git status to git status -short) outputs: Initialized empty Git repository in /home/user/repo/.git/ Running your test script (with some minor tweaks changing git commit. Since version 1.7.0, Git's ls-files honors the skip-worktree flag. Note: With git checkout -overlay HEAD - (Git 2.22, Q1 2019), files that appear in the index and working tree, but not in are removed, to make them match exactly.īut that checkout can respect a git update-index -skip-worktree (for those directories you want to ignore), as mentioned in "Why do excluded files keep reappearing in my git sparse checkout?". If you have extra files in the working tree which don't exist in HEAD, a git checkout HEAD - won't remove them. git checkout HEAD - does a hard reset for a path, replacing both the index and the working tree with the version from the HEAD commit.Īs answered by Ajedi32, both checkout forms don't remove files which were deleted in the target revision.git checkout - doesn't do a hard reset: it replaces the working tree contents with the staged contents. ![]() Note (as commented by Dan Fabulich) that: ![]() That would replace both the index and working tree with HEAD content, like an reset -hard would, but for a specific path. With Git 2.23 (August 2019), you have the new command git restore (also presented here) git restore -source=HEAD -staged -worktree - aDirectory The original behavior can be accessed via git checkout -ignore-skip-worktree-bits - a (where a is the directory you want to hard-reset). According to Git developer Duy Nguyen who kindly implemented the feature and a compatibility switch, the following works as expected as of Git 1.8.3: git checkout - a ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |