11

Following up on Easy LaTeX Editor with SVN or GIT connection and auto-update, I am trying to set up a way to automatically commit my latest changes with the current date and time (e.g. -m "autosave on YYYY-MM-DD_hh:mm") to a git repository using TeXstudio (on Windows).

Using the "Automatically check in after save" feature (see image below) has the following disadvantages:

  • Cluttering of the repository (maybe to a lesser extent by saving less often); however, I am aware this is part of the functionality and can hardly be avoided
  • The function only commits the file you just saved (even worse, simply adds the current file name to the svn/git command).
    If another file was modified (which happens quite a lot if the generated PDF is part of the repository), git won't commit the changes.
    Modifying the built-in svn command to something like git -a (commit all modified files) fails since a file name is appended to the command, which gitdoesn't allow.
    To clarify, the full command executed looks like this:
    git -m "my commit message" currentfile.tex
    I have not yet found a way to not append the file name.
  • The built-in use of the % sign makes it impossible (on a Windows machine, at least) to add system variables such as %date%to the commit message

I am not dead-set on using the autosave feature; it would suffice to add a custom command to the standard compilation process.

TexStudio SVN autosave feature

  • I am using a git alias for my log output (l = log --graph --pretty=format:'%C(red)%h%Creset%C(green)%d%Creset %C(white dim)%s%Creset %C(white)%an, %ar%Creset', so I immediately see the date my commit was done. Maybe - instead of forcing the commit message - you could use something like this, making your life a bit easier :) – Habi Aug 14 '15 at 7:39
8

I found a solution to this problem with the following steps:

  1. Create a batch file in the directory of your main *.tex file
  2. Open the batch file and add the following line: git commit -am "autosave %date%-%time:~0,8%". This will commit all changes made (-a switch) and set the commit message to e.g. autosave 2015-08-17-09:34:05
  3. Add a custom command in TeXstudio that executes your batch script (see image below for an example) add custom TeXstudio command
  4. Add your custom command to the list of commands for compilation add custom command to compilation
  5. After the next compilation of the document, the following message will appear in the message log: commit log message
  6. Additionally, the batch file could be modified accordingly to push the changes directly to the remote repository via git push
|improve this answer|||||
4

Try to work with git as described here - citated:

Use git instead of svn. So you have to change the commands for using git via svn-commands at Options->Configure TeXstudio → Commands → SVN and SVNADMIN to git.

enter image description here

Then open bash and tell git to use the command ci:

git config --global alias.ci "commit"

enter image description here

There you go. Now you can enable auto-commit at Options → Configure TeXstudio → SVN.

Be advised that these automatic checkins will clutter your repository. It may be a good idea to switch to a different branch before working with TeXstudio and pick/squash the changes to the master branch after you are done.

|improve this answer|||||
  • I did it but when I try to use it I get this error: "fatal: Not a git repository (or any of the parent directories): .git" – skan Nov 28 '16 at 1:28
  • works like a charm! @skan did you init a git repository first? – Philippe Dec 7 '16 at 1:20
  • from TexStudio or from the command line? – skan Dec 7 '16 at 12:48
  • It works fine, but has the small disadvantage that you have to make sure to add all files to git (git add . ) - otherwise new files you add to your tex project won't be kept under version control. – LeBird Oct 5 '17 at 9:39
0

I was looking for a way to commit to GitHub when I found this post, which proved rather useful. =). I guess my answer is a tad late and only elaborates on the solutions already posted a bit beyond the scope of OP's question. However, the issue I was faced with is that the automated timestamp did not work on Linux (Ubuntu 19.10) with the previously posted solutions. Also, adding the script to "Build & View" will create a LOT of commits if you use it frequently. So, personally I came to the conclusion it works better by adding a button/hotkey in TexStudio that can be used to commit.

I found these tutorials for adding an existing project to GitHub using the command line, and for Overleaf GIT TexStudio integration, which I used to set up TexStudio with GitHub. Anyway, I thought I'd add the steps I went through to set up TexStudio with Git and GitHub, with the addition of a timestamp working correctly with Linux.

  1. Create a dummy repository on GitHub and find the "Add .gitignore"-button and select "TeX". After creating the repo, download it and copy .gitignore to the folder you want to commit. (This saves you a lot of trouble figuring out which files you need to save and not.)
  2. Set up your GitHub account for use with SSH, if you haven't already. Now create a new, clean GitHub repository, without any adding a README or .gitingore. (Having files already present in my repo created staging problems when I tried to upload.)
  3. Follow the instructions to set up your repository with GitHub. Set it up with the SSH-link, as https will create a prompt for username and password every time you try to upload a commit.
  4. After this is done, the process of uploading a commit can be automated in a script. Open a text editor in terminal in /usr/local/bin as super user. (Ctrl+Alt+T to open terminal, then write sudo gedit /usr/local/bin/gitcommit.sh). Write something along the lines of:
    #!/bin/sh

    git add . 
    # Adds all files in the local repository and stages them for commit (except those defined in .gitignore). 

    git commit -m `date "+%Y-%m-%d_%H:%M:%S_%Z"`
    # Commits the tracked changes with the current time in the format "2020-02-15_23:29:56_CET". 

    git push origin master
    # Pushes the changes in your local repository up to the remote repository you specified as the origin
  1. In TexStudio, under the preferences ("Configure TexStudio", check the Advanced Mode box), in the Build menu add a new user command as in the picture: enter image description here
  2. Go to the Toolbars menu. Find your command under "All menus>Tools>User" and add to "Tools". You can also go to shortcuts and find your command there and add a desired keyboard shortcut. (Now, you could choose to add this command to "Build & View" or compiler by adding the command txs:///gitcommit.sh, like @barghest suggests. However, for me that ended up in an unreasonable amount of commits, so I chose not to.)
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.