Using git to update an application

git
linux
Published

February 9, 2021

Imagine you are using an open source web application downloaded from internet, you did some modifications to it, and you are not controlling the changes with a VCS (I know, it is a bad practice but eh! Sometimes you behave badly ;-)

Once a new version of this application is out, if they don’t provide an upgrade method or if your changes are incompatible with the provided method, you can not use it, then you have 2 versions: yours and the new official one, without knowing what has changed.

git is your friend

1 - Create a new folder mdkir new_folder && cd new_folder

2 - Initialize the folder with git git init

3 - Put the code of your application (the previous version modified by yourself) inside this folder cp /wherever/is/my/application/* ./

4 - Stage the changes and commit git add -A && git commit -m 'my version of the code'

5 - Download the new version of the application and copy it the new folder cp /wherever/is/the/new/version/* ./

6 - Stage the changes and commit again git add -A && git commit -m 'the new version of the code'

Now, you can check the last commit to see the changes.

If you want to know which files has been deleted between the 2 versions, you can delete all the files in the folder “new_folder” after step 4 and before step 5.

Bonus : copy only the modified files

the command git diff-tree gives you the list of the modified files in a commit. Once we have this list, we can plug it to a loop to copy it to another folder.

1 - Getting the commit id git reflog

the output will be : ba5f89d HEAD@{17}: commit: the new version of the code 50b0cf0 HEAD@{18}: commit (initial): my version of the code

Here, the commit id of the last commit is ba5f89d

2 - Copying the files git diff-tree --no-commit-id --name-only -r ba5f89d | for f in $(< /dev/stdin); do mkdir -p "../files_to_copy/$(dirname $f)"; cp $f ../files_to_copy/$f; done

Again, if you want to know which files have been removed and then remove them, use the “delete trick” and check the commit.

Voilà !