Skip to main content

GIT Helper

·438 words·3 mins
Rich
Author
Rich

Git is brilliant when it behaves and deeply memorable when it does not. This post is a small collection of commands I reach for when I need to get back to a known-good place without turning the moment into a drama.

Think of it as a memory jog: quick reminders for common setup and branch workflow tasks.

Initial Configuration
#

git config --global user.name "rich"
git config --global user.email "noreply@fromyouto.me"
git config --global fetch.prune true

Branch Workflow
#

Create a branch, and switch to it.

gitGraph
    commit
    branch feature/my-change
    commit
git switch main
git pull
git switch -c feature/my-change

Running

git branch

Should now show

* feature/my-change
main

Do some work, create files. In relevant directory, add files to be tracked.

git add .

When ready, create a commit with a good message (Future you, take note!).

gitGraph
    commit
    branch feature/my-change
    commit id: "Test function"
    commit id: "Fix syntax"
    commit id: "Enable Log"

git commit -m "description of changes"

Publish the branch

git push -u origin feature/my-change

Danger Will Robinson, Danger!
#

Need to delete a branch, you can’t delete the active branch so switch to alternatuve first.

git switch main
git branch -d feature/my-change

or to force a delete

git branch -D feature/my-change

When Your Branch Is Behind Main
#

Gotten left behind? If your local branch was created some time ago, an option is to rebase.

Sometimes you start a feature branch, do a bit of work, and then main moves on without you. Your branch still works from the old starting point, but it is now missing the latest commits from main.

gitGraph
    commit id: "A"
    commit id: "B"
    branch feature
    checkout feature
    commit id: "C"
    checkout main
    commit id: "D"
    commit id: "E"

In that shape, feature contains your work at C, but it does not contain the newer main commits D and E. Rebasing replays your feature work on top of the latest main, which makes the branch look as though you started from the current version.

gitGraph
    commit id: "A"
    commit id: "B"
    commit id: "D"
    commit id: "E"
    branch feature
    checkout feature
    commit id: "C'"

That can be useful because it:

  • keeps the pull request history easier to read
  • tests your work against the latest version of main
  • avoids a merge commit just to catch up
  • often makes conflicts smaller and easier to understand

The usual flow is:

git fetch origin
git switch feature/my-change
git rebase origin/main

If the branch has already been pushed, update the remote branch with:

git push --force-with-lease

Use --force-with-lease instead of plain --force; it checks that you are not overwriting someone else’s newer work by accident.