⚠ Warning:
Any uncommitted local change to tracked files will be lost, even if staged.
But any local file that's not tracked by Git will not be affected.
First, update all origin/<branch> refs to latest:
git fetch --all
Backup your current branch (e.g. main):
git branch backup-main
Jump to the latest commit on origin/main and checkout those files:
git reset --hard origin/main
Explanation:
git fetch downloads the latest from remote without trying to merge or rebase anything.
git reset resets the master branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/main.
Maintain current local commits
[*]: It's worth noting that it is possible to maintain current local commits by creating a branch from main before resetting:
git checkout main
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/main
After this, all of the old commits will be kept in new-branch-to-save-current-commits.
Uncommitted changes
Uncommitted changes, even if staged (with git add), will be lost. Make sure to stash or commit anything you need. For example, run the following:
git stash
And later (after git reset), reapply these uncommitted changes:
git stash pop
Answer from RNA on Stack OverflowWhich may create merge conflicts.
version control - How do I force "git pull" to overwrite local files? - Stack Overflow
git - How to get a fresh copy of a branch from the remote repository? - Stack Overflow
How can I make a clean pull request using git rebase?
git - How to prune local tracking branches that do not exist on remote anymore? - Stack Overflow
Hi,
Sometimes I need to refresh the local repository. Basically start from scratch. Practically like removing whole repo and doing a new clone.
I know so far I can do
git fetch
git reset —hard
git clean -fdx
But from my understanding this only treats the checked out branch. Do I really have to script switching to every branch one after another and doing same steps?
⚠ Warning:
Any uncommitted local change to tracked files will be lost, even if staged.
But any local file that's not tracked by Git will not be affected.
First, update all origin/<branch> refs to latest:
git fetch --all
Backup your current branch (e.g. main):
git branch backup-main
Jump to the latest commit on origin/main and checkout those files:
git reset --hard origin/main
Explanation:
git fetch downloads the latest from remote without trying to merge or rebase anything.
git reset resets the master branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/main.
Maintain current local commits
[*]: It's worth noting that it is possible to maintain current local commits by creating a branch from main before resetting:
git checkout main
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/main
After this, all of the old commits will be kept in new-branch-to-save-current-commits.
Uncommitted changes
Uncommitted changes, even if staged (with git add), will be lost. Make sure to stash or commit anything you need. For example, run the following:
git stash
And later (after git reset), reapply these uncommitted changes:
git stash pop
Which may create merge conflicts.
This will remove all uncommitted changes, even if staged,
and then pull:
git reset --hard HEAD
git pull
But any local file that's not tracked by Git will not be affected.
As Jefromi commented,
git checkout master
git reset --hard origin/master
does the right thing: setting the master to its origin state. (If you are already on the master branch, you can omit the first command.) It also leaves the branch's reflog intact.
Old inferior answer:
git checkout dev
git branch -D master
git checkout master
This switches to another branch ("dev" in this case – choose any other branch you might have), deletes the local master branch, and then recreates it from remotes/origin/master (which might not work depending on your settings and Git version). The last command is often equivalent to
git checkout -b master remotes/origin/master
Compared to the new answer above this has the disadvantage that the reflog is destroyed and recreated (i.e. you can't as easy undo this if needed), and it is less clear what happens here. Also, you need to have another branch existing to which you can switch during deletion and recreation (but that was the case in the original question).
Paŭlo Ebermann's answer is correct:
git checkout master
git reset --hard origin/master
And add that if you also wish to remove untracked files and ignored files:
git clean -xfn # dry run with -n
Source with further details: How to remove local (untracked) files from the current Git working tree?
When I rebase my branch from the main branch, I run the git rebase command and resolve any conflicts if they exist. Afterward, I push my changes.
However, I often notice that a lot of commits from the main branch appear in my pull request, even though they’re not related to my work. My reviewer has asked me to send a clean PR by using git rebase -i (interactive rebase).
Could someone please explain, with practical steps, how to use interactive rebase to clean up my PR?
After pruning, you can get the list of remote branches with git branch -r. The list of branches with their remote tracking branch can be retrieved with git branch -vv. So using these two lists you can find the remote tracking branches that are not in the list of remotes.
This line should do the trick (requires bash or zsh, won't work with standard Bourne shell):
git fetch -p ; git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d
This string gets the list of remote branches and passes it into egrep through the standard input. And filters the branches that have a remote tracking branch (using git branch -vv and filtering for those that have origin) then getting the first column of that output which will be the branch name. Finally passing all the branch names into the delete branch command.
Since it is using the -d option, it will not delete branches that have not been merged into the branch that you are on when you run this command.
If you would like to have this as an alias, add these lines to your .bashrc file:
alias git-list-untracked='git fetch --prune && git branch -r | awk "{print \$1}" | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk "{print \$1}"'
alias git-remove-untracked='git fetch --prune && git branch -r | awk "{print \$1}" | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk "{print \$1}" | xargs git branch -d'
Then, the procedure for pruning becomes:
git remote prune --dry-run origin
git remote prune origin # Removes origin/asdf that's no longer on origin
git-list-untracked
git-remove-untracked # removes asdf [origin/asdf] where [origin/asdf] no longer exists (after previous command)
If you use squash merges, replace -d with -D. But note that this means that git is not checking whether the branch has been merged before deleting it.
If you want to delete all local branches that are already merged into master, you can use the following command:
git branch --merged master | grep -v '^[ *]*master$' | xargs -d'\n' git branch -d
If you are using main as your master branch, you should modify the command accordingly:
git branch --merged main | grep -v '^[ *]*main$' | xargs -d'\n' git branch -d
More info.
NOTE: xargs -d'\n' parameter is used to allow proper deletion of branches with an apostrophe in the name, see https://unix.stackexchange.com/questions/38148/why-does-xargs-strip-quotes-from-input.
Hey people! I just played around with the idea of branches and encountered the following:
I added branches, via git and also via the GitHub Web. Now after deleting all branches in GitHub as well as in my local git repo, I ran git branch -a
On GitHub, those branches don't exist anymore and they shouldn't be present in my local repo. Why are they still listed and is there any way to fully remove those entries?
Help is appreciated, many thanks!
I have a repo that has a single 'master' branch which is being used for development. Yeah its a mess. What I want is a clean (useable) master branch and move the development into the it's own branch. My thoughts are to do this :
# create a develop branch, stay on master git branch develop <clean up the master branch, commit changes> git switch develop git reset master # At this point working directory looks just like master did before all this started. <commit the changes in the working directory, moving the development branch ahead of the master with all the messy changes in place>
At this point I will have a clean master, and develop will look like master used to. Is there a better way to do this?
### Edit: Add example
As a simple example, imagine I have only one file `file.md`. This file has some parts that are finished, and some parts that are under developed, perhaps just placeholders and such. I want to clean things up so that on the master branch, this file can stand on its own and all the under developed parts are moved to some new 'development' branch. My thought is to clean the file up as much as possible without removing the under developed parts, then do the process above to move those to a development branch: create a development branch , but stay on master and then delete all those offending parts. Then switch to the development branch and move the head to the tip of master, leaving all the files *with * those parts in the working directory. I can then commit them onto the development branch.