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 mastergit checkout bk-add-users— switch branch to bk-add-usersgit checkout -b bk-add-projects— create new branch from current branch and switch to itgit checkout— — switch branch to previous branchgit 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 changesgit add README.md— add specific file or directorygit add -p— add changes progressivelygit add -N .— make not staged files subject for adding
COMMIT
git commit -m “Add projects management”— commit with messagegit commit --amend— merge with last commit with possibility to change messagegit commit --amend -CHEAD— merge with last commit without changing message
BRANCHES
git log— see commits historygit rebase -i HEAD~5— rebase interactively (squash/reorder/remove/rename) last 5 commits on current branchgit fetch— update branches from remote repositorygit fetch --all --prune— remove outdated remote-tracking referencesgit branch add-users origin/add-users— checkout and track remote branchgit branch -m bk-add-ussers bk-add-users— rename branchgit branch -d bk-add-users— remove local branchgit push origin :bk-add-users— remove remote branchgit 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 branchgit push origin bk-add-users— push to specific branch on remotegit push origin HEAD— push to current branch on remotegit push origin HEAD --force— enforce history overwritinggit push origin HEAD --force-with-lease— safer, overwrites history if no new changes were introduced
OTHER
git status— no comment needed heregit diff— show current changesgit show c04c07e— show changes from specific commitgit stash— store current changes in stashgit stash pop— restore recently stashed changesgit cherry-pick c04c07e— get specific commit to your current branchgit checkout .— get rid of current modifications, unstaged files will be keptgit reset --hard origin/master— reset to remote HEADgit reset HEAD~2— “uncommit” last two commits, keep changes in unstaged stategit reset HEAD~2--hard — get rid of last two commits, changes will be lostgit reset HEAD --hard— get rid of all current changes and new filesgit clean -fd— get rid of all unstaged files and directories





