Skip to content

Useful GIT Commands

Hernan G. Arango edited this page Jul 23, 2024 · 7 revisions
Command Description
git add <FileName> Add a file to the repository staging area
git branch List all local branches (the asterisk denotes the current branch)
git branch -a List all local and remote branches
git branch -d <BranchName> Delete a local branch
git branch -D <BranchName> Delete a local branch forcefully
git branch -m <OldName> <NewName> Rename a local branch
git checkout -b <BranchName> Create a new local branch and switch to it
git checkout <BranchName> Switch to an existing local or remote branch
git clone <RepositoryURL> Clone a public repository
git commit -am "<Message>" Commit changes to all files
git commit -am "<Message>" -m "<MessageURL>" Commit changes to all files with two message lines
git commit --amend -m "<NewMessage>" Amend previous commit message
git diff Show all changes between HEAD and working branch
git diff <FileName> Show changes for a specific file between HEAD and working branch
git difftool -d <BranchName1> <BranchName2> Compare the difference between two branches with KDIFF3
git fetch Retrieve new work done by other people
git fetch origin --prune Remove tracking of any deleted remote branch in your local repository
git log View repository changes
git log --oneline Show the list of commits in one-line format
git log --summary View repository detailed changes
git merge --no-commit <BranchName> Merge a branch into the active branch
git pull Update local repository or branch to the newest origin commit
git pull -p Remove/Prune any local tracking branch that no longer exists on the remote
git push origin --delete <BranchName> Delete a remote branch
git push -u origin <BranchName> Push changes to the remote repository (and remember the branch)
git reset --hard HEAD~1 Undo last commit
git revert <CommitID> Revert commit changes
git stash Store current branch uncommitted temporarily changes to work on another branch
git stash apply stash@{1} Like pop, but do not remove the state @{1} from the stash list
git stash list List stashed modifications
git stash pop stash@{1} Remove stashed state @{1} from the stash list and apply it on top of the current branch
git stash show Inspect stashed modifications
git status Check/display changes to the repository or particular branch
git tag List all tags

Complicated Commands:

  • If a branch is renamed in a repository on GitHub, the local clone on a computer needs to updated:
git branch -m OldName NewName
git fetch origin
git branch -u origin/NewName NewName
git remote set-head origin -a
  • How to merge changes in the feature/name1 branch into the feature/name2 branch:
git checkout feature/name1
git pull

git checkout feature/name2
git pull
git merge --no-ff --no-commit feature/name1
git commit -am "Merging feature/name1 into feature/name2" -m "MessageURL"
git difftool -d feature/name1 feature/name2
git push -u origin feature/name2
Clone this wiki locally