Skip to content

Cheatsheet: Git for Simutrans

Matthew edited this page Mar 1, 2023 · 7 revisions

Git for Simutrans-Extended: Cheatsheet

This cheatsheet assumes that your Github repository is set up as origin

Change branch

$ git checkout <MY-BRANCH>

Compare two branches

$ git diff <A-BRANCH>..<ANOTHER-BRANCH>

Create a new local branch

$ git checkout -b <MY-BRANCH>

Follow a new remote branch

$ git fetch --all
$ git checkout --track <THEIR-REPO>/<THEIR-BRANCH>

Push a local branch for the first time

$ git push -u origin <MY-BRANCH>

Merge changes from another local branch into this local branch

$ git merge <OTHER-BRANCH>

Revert last commit

If you want to retain changes in the present working directory, as though you had edited but not committed them:

$ git reset --soft HEAD~1

If you want to delete the changes forever:

$ git reset --hard HEAD~1

To find the branch a commit was first added to

$ git name-rev <SHA>

Revert ancient commits

$ git revert <SHA1 SHA2 etc.>

Git will then prompt you to edit any files that have conflicts. When you have done this:

$ git revert --continue

If you want to abandon editing while there are still conflicts:

$ git revert --abort

Add a new remote repository

$ git remote add <NEW-REPO> <http://THEIR.REPO.URL>

Make the current branch track a remote branch

$ git branch -u <THEIR-REPO>/<THEIR-BRANCH>

Rebase forked main branch onto upstream main branch

Change main to master if the repositories predate that change

If necessary, add the upstream repository as upstream

$ git remote add upstream https://github.com/<THEIR-USERNAME>/<THEIR-REPO>.git

First, critical, step is to make sure that you are on your main branch and have committed any changes!

$ git status
$ git fetch upstream
$ git rebase upstream/main

You now need to push these changes to your own repo on GitHub. Use --force because otherwise git prefers the GitHub version

$ git push origin main --force

If necessary, rebase the branch you are working on

$ git checkout <BRANCH-NAME>
$ git rebase main

Check the history of a line of code

To get the commit history of line 15 of simworld.h in the present working directory:

$ git log -L15,+1:'./simworld.h'

To get the history of the 5 following lines

$ git log -L15,+5:'./simworld.h'