Home > Software design >  How do I do a `git checkout` that removes the adds?
How do I do a `git checkout` that removes the adds?

Time:01-26

I know I can do

git checkout my-branch .

to replace all the files that have been modified on my branch. It works fine with the modified files, but if I had files on my current branch that were added, it had kept them.

Is there a way of doing the git checkout such that the adds are not done?

Workflow:

  1. Steps to get to the state before the operation
git init test
cd test
touch a
touch b
touch c
git add .
git commit -a -m "Initial"
echo "My name is A" > a
git commit -a -m "Changed name"
git checkout -b my-branch
echo "My name is B" > b
git commit -a -m "Changed name in branch"
git checkout master
echo "My name is B" > b
touch d
git add .
git commit -a -m "changed name in master and added file"

From this point I want decided I want my-branch (which does not have d to be what's on master. What I would normally do is

git checkout my-branch .

but doing that does not delete d. So I wanted something that would delete d ideally making my HEAD == my-branch

CodePudding user response:

You could simply delete everything in your repo (except the .git directory) and then do the checkout:

git rm -rf .
git checkout my-branch

After that, d is deleted:

➜  test git:(master) ✗ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    deleted:    d
  •  Tags:  
  • git
  • Related