Wipe out and reset GitHub Repository from command line

in #git7 years ago

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

$ 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

$ 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

my-project$ git remote add origin [email protected]:username/my-project.git
my-project$ git remote -v
origin  [email protected]:username/my-project.git (fetch)
origin  [email protected]:username/my-project.git (push)

Force PUSH the new Repository

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:


`reset_git.sh
```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 [email protected]:$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:

$ 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!
Sort:  

Congratulations @thierrydd! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You published 4 posts in one day

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

Upvote this notification to help all Steemit users. Learn why here!