Most Useful Git Commands

This is a reminder post for myself to remember the most useful git commands.

Commands changing the local history

  • Undo last commit and stage changes: git reset --soft HEAD^
  • Permanently delete last commit : git reset --hard HEAD^
  • Use HEAD^^ for the last two commits, HEAD^^^ for the last three commits, … Use HEAD~<number> to reference the commit

Note: Do NOT use any of these commands after pushing to a repository as this would alter (the) history!

Merges and branches

  • Use git checkout -b <branch name> to create a new branch and automatically check the branch out.
  • Deleting branches using git branch -d <name> will not work when commits are unreachable after this. Use -D to override.
  • Invoke merge commands from the branch you want to merge into, e.g., from master to merge feature_branch into master. (git merge feature_branch in master branch) After merging a branch, it can be safely deleted.

Remotes and remote branches

  • The -u option in git push sets the passed remote and branch name as the default for the current repository. (git push [-u] <remote name> <local branch>)
  • Using git push <remove name> :<remote branch> deletes the remote branch. The colon marks the branch for deletion.
  • git remote show <remote name> displays lots of information about this remote.
  • git remote prune <remove name> deletes all local branches which do not have any corresponding remote branches anymore.

Tags

  • git tag lists all tags.
  • git checkout <tag name> checks out the commit referenced by the tag.
  • git tag -a <tag name> -m "<tag description>" adds a new tag.
  • git push --tags pushes the tags to the default remote.

Conflicts/Rebase

  • HEAD represents the local version in any conflict marking.
  • When using feature branches, first, run git rebase on feature branch and then git merge using fast forward in the master branch. This ensures that the possible conflict merges are made in the feature branch and not in the master branch.
  • Merge is better then rebase when the branches diverged by many (really many) commits.

Additional notes to myself

  • Use git aliases with git config alias.<abbreviation> "<full version>"!
  • git blame <file> displays which line of the given file has been authored by which user at which time in which commit.

 

Which commands do you consider useful?

Advertisements

Helpful git command chaining

I am currently working in a team of two using a git repository. In that environment, you want to have a clean history which can only achieved with the git rebase command. Using this small code snippet, I can fetch the changes online, rebase my code according to the changes fetched and push my changes automatically. If an error occurs, the command will abort.

git fetch && git rebase && git push

However, this does not work if you still have files locally which are not committed, e.g. some changed files in your working directory. For this purpose, the stash can be used. The git stash is a stack for storing changes temporarily. By saving the uncommitted changes before fetching and rebasing, you can integrate them back afterwards.

git stash save && git fetch && git rebase && git push && git stash pop

What git tricks do you use?

PS: The commands above work on *nix and Windows.