A Curated List Of Git Commands Enough To Keep Your Git History Flat

A Curated List Of Git Commands Enough To Keep Your Git History Flat

A curated list of git commands enough to keep your git history flat I’ve been working on projects where we keep the git history flat (no merge-commits) for quite a bit of time now, and I’ve decided to make a list of git commands I often use during every-day development. Some of them I use rarely, but just want to keep the list sort of complete. Maybe some of you will find it useful as well.

CHECKOUT

  • git checkout master — switch branch to master
  • git checkout bk-add-users — switch branch to bk-add-users
  • git checkout -b bk-add-projects — create new branch from current branch and switch to it
  • git checkout — — switch branch to previous branch
  • git checkout master -- db/structure.sql— get file from different branch

PULL

  • git pull --rebase origin master — rebase onto specific branch (add your commits to end of history)
  • git pull --rebase --autostash origin master — same as above, but works on dirty repository

ADD

  • git add . — add all changes
  • git add README.md — add specific file or directory
  • git add -p — add changes progressively
  • git add -N . — make not staged files subject for adding

COMMIT

  • git commit -m “Add projects management” — commit with message
  • git commit --amend — merge with last commit with possibility to change message
  • git commit --amend -CHEAD — merge with last commit without changing message

BRANCHES

  • git log — see commits history
  • git rebase -i HEAD~5 — rebase interactively (squash/reorder/remove/rename) last 5 commits on current branch
  • git fetch — update branches from remote repository
  • git fetch --all --prune — remove outdated remote-tracking references
  • git branch add-users origin/add-users — checkout and track remote branch
  • git branch -m bk-add-ussers bk-add-users — rename branch
  • git branch -d bk-add-users — remove local branch
  • git push origin :bk-add-users — remove remote branch
  • git merge bk-add-users --ff-only — merge branch into current branch keeping flat history (I am usually just clicking a “Rebase and merge” button on Github instead)

PUSH

  • git push — push to remote / tracked branch
  • git push origin bk-add-users — push to specific branch on remote
  • git push origin HEAD — push to current branch on remote
  • git push origin HEAD --force — enforce history overwriting
  • git push origin HEAD --force-with-lease — safer, overwrites history if no new changes were introduced

OTHER

  • git status — no comment needed here
  • git diff — show current changes
  • git show c04c07e — show changes from specific commit
  • git stash — store current changes in stash
  • git stash pop — restore recently stashed changes
  • git cherry-pick c04c07e — get specific commit to your current branch
  • git checkout . — get rid of current modifications, unstaged files will be kept
  • git reset --hard origin/master — reset to remote HEAD
  • git reset HEAD~2 — “uncommit” last two commits, keep changes in unstaged state
  • git reset HEAD~2 --hard — get rid of last two commits, changes will be lost
  • git reset HEAD --hard — get rid of all current changes and new files
  • git clean -fd — get rid of all unstaged files and directories