Sunday, May 4, 2014

Git cheat sheet - the most useful commands

After a little practise, Git can be fully utilised from the command line without any GUI. Adding, committing, pulling, and pushing is part of the daily work; however, there are other commands that are useful but not that frequently used. Here is my collection of those:

# Create and checkout new branch based on the current branch
git checkout -b <newBranch>

# Undo all local pending changes
git reset --hard HEAD

# Erase the last 3 commits
git reset --hard HEAD~3

# Undo a single file change
git checkout -- <fileName>

# Push new branch to server
git push origin <branchName>

# Delete remote branch
git push origin --delete <branchName>

# List all local and remote branches
git branch -a

# Remove remotely deleted branches - will not delete local branches, only copies of remotes
git remote prune origin

# Pull remote master branch and merge it into current branch
git pull origin master

# Undo last commit but keep changes unstaged
git reset HEAD~1

# Rename current branch
git branch -m <newname>

# Save changes temporarily - branch independent
git stash

# List temporary saves
git stash list

# Apply temporarily saved changes
git stash apply

# Delete temporary saves
git stash drop

# Delete untracked files and directories (-n to just test)
git clean -fd

# Resolve conflict by accepting their changes
git checkout --theirs <file>

# Interactive stage / unstage
git add -i

# Compare branch to another branch
git diff <otherBranch>

#display all changes with additions/deletions between two branches
git diff --stat master..<otherBranch>

No comments:

Post a Comment