Languages
[Edit]
EN

Git - script to reset project directory (reset working tree)

10 points
Created by:
Nataniel-Barclay
319

In this short article, we would like to show how to reset the project directory (git working tree) using Bash.

Presented below solution:

  1. resets all files and directories - with ignored items too,
  2. switches working tree into master,
  3. pulls last project version from the repository.

Note: below script is an alternative to git clean -fdx command.

Quick solution:

#!/bin/bash

function reset_git_dir()
{
	cd "$1"

	for i in .* *
	do
		if [ -d "$i" ] || [ -f "$i" ]
		then
			[ "$i" = "." ] && continue
			[ "$i" = ".." ] && continue
			[ "$i" = ".git" ] && continue
			
			rm -rf "$i" &> /dev/null || return 1
		fi
	done
	
	if git reset --hard &> /dev/null && git clean -fd &> /dev/null && git checkout master &> /dev/null && git pull &> /dev/null
	then
		echo "'$1' reset!"
		return 0
	fi

	return 1
}

reset_git_dir "/path/to/git/project"
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join