jGit - how to add all files to staging area

前端 未结 3 1642
攒了一身酷
攒了一身酷 2021-01-01 16:47

I tried in a lot of ways to clone a repo with jGit (it works). Then, I write some archive in the repository, and tried to add all (a git add *, git add -A

3条回答
  •  旧时难觅i
    2021-01-01 17:12

    I had a situation where I had to move a file f1 from the current directory to another directory called 'temp'. After moving the file, calling git.add().addFilePattern(".").call() acted in a weird way since git status gave the following result:

    Changes to be committed:
      (use "git reset HEAD ..." to unstage)
    
        new file:   temp/f1.html
    
    Changes not staged for commit:
      (use "git add/rm ..." to update what will be committed)
      (use "git checkout -- ..." to discard changes in working directory)
    
        deleted:    f1.html
    

    It recognized that a new file temp/f1 was created but didn't detect that the file was deleted first. This was perhaps because moving the file can be seen as follows

    • Deleting/Cutting the file f1
    • Creating a folder called temp
    • Creating/Pasting the file f1

    Then I came across the setUpdate(true) that looks for updates to files that are already being tracked and will not stage new files. (Check java-doc for more info)

    So I had to change my code to two lines like so in order for git to recognize both files added and modified (which includes deletion):

    git.add().addFilepattern(".").call();
    git.add().setUpdate(true).addFilepattern(".").call();
    

    git status now gives the expected result:

    renamed:    f1.hml -> temp/f1.html
    

提交回复
热议问题