Wipe out and reset GitHub Repository from command line

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
ยท@thierryddยท
0.000 HBD
Wipe out and reset GitHub Repository from command line
<div class="pull-left">
https://steemitimages.com/150x150/https://steemitimages.com/DQmQYrF3pHFJ6hcrzx8Wx9Jn8nkMtEpSVAd2pwTtVtqZAiZ/image.png</div>

Sometimes, when we work on a Tutorial, we want to wipe out our Project repository on GitHub and restart. Doing it from command line is easy and fast.

Let's say your username on GitHub is "`username`" and you want to reset the project "`my-project`". Open a console and do the following:

# Delete your current project

```sh
$ ls -a
.   ..   my-project
$ rm -rf my-project
```
โš  Performing `rm -rf` from within your project directory will not work as it does not remove the hidden files and directories.

# Reinitialize an empty Git Repository

```sh
$ mkdir my-project
$ cd my-project
my-project$ git init
Initialized empty Git repository in /home/username/projects/my-project/.git/
my-project$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
```

# Link to GitHub

```sh
my-project$ git remote add origin git@github.com:username/my-project.git
my-project$ git remote -v
origin  git@github.com:username/my-project.git (fetch)
origin  git@github.com:username/my-project.git (push)
```

# Force PUSH the new Repository

```sh
my-project$ touch README.md # ๐Ÿ…ฐ
my-project$ git add .
my-project$ git commit -m "Initial commit"
[master (root-commit) b17b424] Initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
my-project$ git push -uf origin master # ๐Ÿ…ฑ
Counting objects: 3, done.
Writing objects: 100% (3/3), 213 bytes | 213.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:username/my-project.git
 + ed755d5...b17b424 master -> master (forced update)
Branch master set up to track remote branch master from origin.
```

A few remarks:

๐Ÿ…ฐ You have to commit at least one file before you can push your repository to Git.

๐Ÿ…ฑ The flags we use when pushing are to force the reset of our repository on GitHub (`-f`) and to set the remote `origin` as default (`-u`). After you run this comment, you will be able to push using just `git push`.

# Bash script

If you reset repositories a lot, you may want to create a bash script for this:

<br />

<div class="pull-right"><sup>`reset_git.sh</sup></div>
```bash
#!/bin/bash  
echo "GitHub Repository Reset"
echo "GitHub username?"
read username
echo "GitHub repository?"
read repository
mkdir $repository
cd $repository
git init
git remote add origin git@github.com:$username/$repository.git
touch README.md
git add .
git commit -m "Initial commit"
git push -uf origin master
echo "Repository $repository for User $username has been reset!"
```

We deliberately didn't include the deletion (`rm -rf $repository`) inside the bash script considering this to be too dangerous.

So, after you change the execution permission on the script (`chmod +x reset_git.sh`) you may run it as:

```sh
$ rm -rf my-project
$ ./reset_git.sh
GitHub Repository Reset
GitHub username?
username
GitHub repository?
my-project
Initialized empty Git repository in /home/username/my-project/.git/
[master (root-commit) 8d7d600] Initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
Counting objects: 3, done.
Writing objects: 100% (3/3), 214 bytes | 214.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:username/my-project.git
 + 479bc7e...8d7d600 master -> master (forced update)
Branch master set up to track remote branch master from origin.
Repository my-project for User username has been reset!
```
๐Ÿ‘