![]() ![]() No changes added to commit (use "git add" and/or "git commit -a") " to discard changes in working directory) Your branch is up to date with 'origin/master'. Now do the changes you need to with your unpushed work.I think what you're seeing is something like this: $ git status Now get back your work from stash by running git stash pop You will see your commits from your remote repository are removed. Now run git reset -hard HEAD~1 (again modify the number to remove n commits) to remove the remote commits from your repository and do a force push git push -f. Now make your local sync with the remote by running git pull Save this work locally by moving it temporarily to a stash by running git stash. Now you will see your committed files as unstaged So run git reset -soft HEAD~1 this will move your local repository on your machine 1 commit behind (modify the number to move n commits behind) Now I want to remove my secrets file from the repository but want to keep my work too. So my secrets are now exposed (Gitguardian's yelling I accidentally committed and pushed a secrets file along with my Git stash pop # get back your unpushed work from stashĪ detailed explanation of why this works. Git push -f # force push to sync local with remote Git reset -hard HEAD~1 # hard reset 1 commit behind (deletes local commit) ![]() Git pull # bring your local in sync with remote Git stash # hold your work temporary storage temporarily. Quick Answer git reset -soft HEAD~1 # 1 represents only last 1 commit Here's a clean way of removing your commits from the remote repository without losing your work. Preserved but not marked for commit) and reports what has not been Resets the index but not the working tree (i.e., the changed files are If you don't use -hard nor -soft, the default mode is used that is -mixed. If you use -hard option, git status will tell you that your local branch is exactly the same as the remote one. In the latter case runnig git status shows the files changed since. Then you have chance to check your latest changes and keep or drop all or parts of them. If you want to keep changes on file system and just modify index (commit history), use -soft flag like git reset -soft HEAD~3. Then you can replace HEAD~N with like this: git reset -hard You can retrieve the desired commit id by running git log Then run the following command (on your local machine) to force the remote branch to rewrite its history: git push -force If you want to delete for example the last 3 commits, run the following command to remove the changes from the file system (working tree) and commit history (index) on your local branch: git reset -hard HEAD~3 This is a convenient shortcut for: $ git branch -f Similar to -create except that if already exists, it will be reset to. That will restore the index and working tree, like a git reset -hard would. ![]() (replace n by the number of commits to remove) That is: git switch -C mybranch origin/mybranch~n With Git 2.23 (August 2019, nine years later), you would use the new command git switch. Plus, as noted by ringo in the comments, if the remote branch is protected against force push, a git revert, as in this answer, might be preferable. You need to be prepared to point out people to the RECOVERING FROM UPSTREAM REBASE section of the git rebase man page. This SO answer illustrates the danger of such a command, especially if people depends on the remote history for their own local repos. ( other solution here, involving deleting the remote branch, and re-pushing it) You git reset -hard your local branch to remove changes from working tree and index, and you git push -force (or git push -force-with-lease) your revised local branch to the remote. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |