Git: local and remote basics, log ranges, and a script to sync via rebasing

Using git log ranges. Plus a script to sync that can be used as a replacement for Evernote/Dropbox solutions

Elvis Ciotti
3 min readDec 28, 2021

How git push and pull work (skip if you already know)

The git command git pull (well explained here) is basically the composition of git fetch (to get the data from the remote repository) followed by a git merge.

Once you run it, the command will run the fetch (network operation taking some time), and — if there are no incoming changes— tell you “Already up to date”.

yourRepoDir $ git pull origin main
From github.com:username/repo
* branch main -> FETCH_HEAD
Already up to date.

Similarly, the git push (well explained here) will contact the network (some time needed) and tell you `Everything up-to-date` if there is nothing to push.

note: I’m using “main” as the branch name, as “master” seems to be the default one being used. (That was a necessary change as computers were very offended by that, they told me once).

Detect what you have more or miss from the remote branch (git log ranges)

The git pull and push commands both perform network operations. If you want to know the differences from the remote branch (without performing yet any merge) without performing any action (merge or push), you can just run first the git fetch, and then use the git log command using ranges (official documentation) to detect if there are any new commits from remote, or you have commits that remote does not have.

git fetch origin# this will show if the remote main has commits that you do NOT have
git log ..origin/main
# this will show commits that YOU HAVE but remote does not
git log origin/main..

Script to update and send new commits to the remote repository in one go (dropbox/google drive style)

What I’ll present here would be sacrilegious to use in team with multiple developers and more than basic git flow needs, but it should work well for a repository only used by you across multiple machines. It’s a single command to

  • Create a commit with all the changed files (message taken from the command line or set to a default)
  • If there is something new on main, perform a rebase. If that fails, do a merge instead
  • If there is something new locally to push (previous commit or stuff not yet pushed), push it
#!/bin/bashcd ~/yourRepo[ "$(git status --porcelain)" == "" ] || (
git add .
git commit -m "${@:-auto commit}";
)
echo "read remote..."
git fetch origin --prune
[ "$(git log ..origin/main)" == "" ] || (
echo "something new on main"
git rebase origin main || (
git rebase --abort
git merge origin/main
)
)
[ "$(git rev-list origin/main..)" == "" ] || (
echo "something new local"
git push origin main
)

I’m using a similar script to version some text notes. This works for me much better than files in a cloud solution (google drive, dropbox) as I can better solve conflicts, version and perform advanced history search operations via git.

Plus, on github and gitlab it’s all completely free.

You could use evernote, but I found the conflict resolution annoying, you have to pay for versioning, and the editor is not as powerful as modern IDEs (for replace/search etc..) like VisualStudio (Free) or IntelliJ (paid).
I also didn’t like the evernote encryption stuff, very basic. With your own solution you can e.g. crypt all the files in a directory and exclude some lines starting with a special prefix just before pushing.

If you need images, there are IDE plugin that allow to past and visualise image inside a markdown file.

Let me know if that works for you

--

--

Elvis Ciotti

Software Contractor — Java, Spring, k8s, AWS, Javascript @ London - hire me at https://www.linkedin.com/in/elvisciotti