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

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

--

--

--

Software Engineer @ London [https://www.linkedin.com/in/elvisciotti]

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Boom or Bust, 50 Days to Colorado Gold Rust: Day 9

How to add GIFs in GitHub README Without Software

Shutdown & Restart Your Computer Using Python

What’s New in RDFox v5.6: Highlighting Semantic Reasoning

Common Mistakes You Make While Learning Programming Languages and How to Avoid Them

Docker 03 / Installing WordPress & MySQL/ Docker Commands

TensorBook Unboxing

KUBERNETES & IT’S USE CASES

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Elvis Ciotti

Elvis Ciotti

Software Engineer @ London [https://www.linkedin.com/in/elvisciotti]

More from Medium

How to Get Started with Azure Cosmos DB

Avoid headache from creating diagrams — use PlantUML

Make Application Modernization This Year’s Top Resolution

IslaDogs: Getting Creative with the Access Application Interface