Technology

#How To Completely Reset a Git Repository (Including Untracked Files) – CloudSavvy IT

“#How To Completely Reset a Git Repository (Including Untracked Files) – CloudSavvy IT”

Git commands are complicated, and you may often find yourself thinking,“screw this, I’ll just start over!” Luckily, Git is built to track every version of your changes, and if you messed up, it provides tools for resetting your repository back to its old working state.

Saving Your Changes (Git Stash)

Before you fire off a command to reset your repository, you should make sure you’re not losing data you want to save.

Git provides a few different kinds of resets. Soft and Mixed resets will reset the repository back to the state it was in at a certain commit (often the HEAD of a branch), but will keep your local changes that you haven’t yet committed. Hard resets, on the other hand, are destructive, and will throw away changes that haven’t been committed yet.

Often a hard reset is needed to properly clean the repository. If you want though, you can “stash” your changes, which will take all uncommitted changes and store them locally. You can pop the stash open with “stash apply” at a later time.

git stash
git stash apply

You can also make a new branch, commit the changes, and then reset back to master. This would keep your changes in the commit history forever, and could also be sent to remote source control to be shared with your coworkers.

Performing a Reset (Git Reset)

First, you’ll need to fetch the latest state of the remote repository, usually “origin,” and then checkout the master branch (or whichever one you’re resetting to).

git fetch origin
git checkout master

You can also check out and reset to an individual commit using its ID, e.g., git checkout 342c47a4.

Then, perform a hard reset (unless you want to keep changes with a soft reset). Keep in mind this operation is destructive to any unsaved changes.

git reset --hard origin/master

You can reset to a local commit instead of origin/master, but most of the time you’ll be resetting to the state of the remote.

Resetting Untracked Files (Git Clean)

However, git reset is usually not enough. Resetting in Git only resets files that are actually tracked by Git. This includes code and resources that receive changes.

However, there are also files like packages, local config, build artifacts/output, log files, and other transitory items that aren’t stored in the Git repository (and ignored in .gitignore). To clean these up, and bring your local repo to 100% parity with the state of the remote, you can run git clean:

git clean -d --force

You can actually run this command without running git reset, which may actually be what you want. If you don’t want to effect your code files, but want to clear up your builds, logs, and packages to start over, git clean may be all you need.

Giving Up Instead: Cloning A New Repo

While the above is the “clean way” to do it with official Git commands, doing a complete reset isn’t very far off from just nuking your local repository folder and cloning a brand new one. There’s no shame in doing that if you plan to reset everything anyway:

sudo rm -r git-repository
git clone https://github.com/user/git-repository.git
cd git-repository

Keep in mind that this can only reset back to the state of your remote repository, not to a local commit. This will clone the master branch by default, but you can switch to the branch of your choise with git switch or clone it from the start with git clone -b <branchname>.

If you liked the article, do not forget to share it with your friends. Follow us on Google News too, click on the star and choose us from your favorites.

For forums sites go to Forum.BuradaBiliyorum.Com

If you want to read more like this article, you can visit our Technology category.

Source

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
Close

Please allow ads on our site

Please consider supporting us by disabling your ad blocker!