Skip to content

Cheatsheet: Git for Simutrans

Matthew edited this page Aug 23, 2023 · 7 revisions

Git for Simutrans-Extended: Cheatsheet

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

Change branch

git switch <MY-BRANCH>

The old way was git checkout <MY-BRANCH> but that's confusing terminology and if shares a name with a file or a commit then unexpected things can happen.

Compare two branches

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

Create a new local branch

git switch -c <MY-BRANCH>

which replaced

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>

Squash all commits on current branch into one

Make sure that you are on the branch that you want to use

git switch <MY-BRANCH>`

In the following, change master to main if the repository uses that

git reset --soft $(git merge-base master HEAD)
git commit -m "<MY-COMMIT-MESSAGE>"

There should not be an any need to git add with this approach.

If this branch has already been pushed to a remote, you will probably need to use git push --force-with-lease to force this push. This is not advised if other people could have already pulled from the remote (e.g. to assess a PR), which you should be warned about with if you use --force-with-lease rather than the riskier -f or --force.

Source CC-BY-SA

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'
Clone this wiki locally