Reputation: 10502
I am trying to come up with a sequence of git commands that will change to a known branch inside a pre-existing git repo. 3 things I know are:
origin
This is what I am doing:
git clean -xfd
git reset HEAD --hard
git add --all; git stash; git stash drop
git fetch origin
git checkout -B $BRANCH
git pull origin $BRANCH
Is this the minimal set of git commands to achieve this? Are there any situations, where these sequence of commands will fail?
Upvotes: 0
Views: 62
Reputation: 60275
So the idea is your current checkout could be in a completely garbaged, potentially malicious state, and you want to abandon it entirely?
git fetch
git checkout -f -B branch origin/branch
git clean -dfx
is what you want if you don't have submodules. If you do, the nuclear option is
git read-tree --empty
git clean -dffx # <-- two f's, forces submodule removal
git fetch
git checkout -B branch origin/branch
git submodule update --init
(edit: switched from find -exec rm to read-tree/clean)
Don't use git pull
for this, that does a fetch and then merges the pulled history into your current checkout. Doing git checkout -B branch
first just labels your current checkout branch
, so the result of that and a pull is very far from switching to the origin's branch.
Upvotes: 1
Reputation: 142084
You can make is simplier:
git fetch --all --prune
git pull origin <branch>
If you are doing git reset --hard
there is no need to to a git stash
since your working directory will be cleaned
git reset HEAD --hard
get reset --hard
Resets the index and working tree.
Any changes to tracked files in the working tree since are discarded.
I want to undo any temporary (staged or unstaged) changes made inside this folder.
No need to do do since all the non committed stuff will not be added to your branch unless you add it explicitly
Still if you want to clean the current working directory
# small x
git clean -xfd
# capital x
git clean -Xfd
-x
Don’t use the standard ignore rules read from .gitignore (per directory) and $GIT_DIR/info/exclude, but do still use the ignore rules given with -e options. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git reset) to create a pristine working directory to test a clean build.
-X
Remove only files ignored by Git.
This may be useful to rebuild everything from scratch, but keep manually created files.
Upvotes: 0