GIT: How to commit a change with .git folder already inside a working directory to that repository via batch command?

StackOverflow https://stackoverflow.com/questions/13155730

Question

Here's my current repository, and made it as my working directory.

enter image description here

As you can see, there's a BAT file, "backup.cmd" with the contents shown below:

"C:\Program Files (x86)\Git\bin\git.exe" add . && "C:\Program Files (x86)\Git\bin\git.exe" commit -am "Periodic Backup %DATE%" 

Now, I am setting a task scheduler in Windows 7 that allows me to weekly create a backup to this repository. However, there's a problem when it comes to commiting changes in any of these files.

The problem is that, by running the BAT file, Git is unable to commit changes. The cause for it unable to commit changes is because of the ".git" folder residing in the working directory.

I have tried to use ".gitignore" and ".git/info/exclude" to ignore/exclude the ".git" directory, but it still fails to let me commit the changes. I do not know any other methods of ignoring/excluding the ".git" directory.

Can anyone help me on this? Thanks in advance.

Was it helpful?

Solution 2

Thanks to @hammar, I now know what my problem is.

Because the Task Scheduler was running the batch file in a location where there's no GIT commands available to the command prompt (which means it was running the commands by its default location (C:\Windows\system32)), it would output an error saying that ".git" doesn't exists.

The fix is to change the location to the actual GIT repository, and then re-execute the batch commands, so that way the Task Scheduler knows what to do with them.

OTHER TIPS

Instead of staging the directory each time (git add .), use git add -A to stage all new files. The presence of the .git directory indicates that the repository is already initialized, and does not need to be staged during each batch run. git add -A stages all changed and new files, which seems to be what you want, but you might prefer git add -u.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top