Useful git commands

22 Mar 2016

git how to

List of various git commands that often can save the day.


Create new repository

git init

More: git init


Get remote repository

git clone <repository-url>

More: git clone


Add all files from current directory to next commit

git add . -A

More: git add


Check changes prepared for next commit

git status

More: git status


Clean working directory

git clean
//-f      delete non-ignored untracked files
//-fx     delete all untracked files
//-fX     delete ignored files
//-d      also remove directories
//-n      list files that will be deleted

Most commonly used as a pair

git clean -dfxn
git clean -dfx

More: git clean, tortoise git manual


Add remote repository to local repository

git remote add <remote-name> <repository-url>

More: git remote


Detach remote repository from local repository

git remote remove <remote-name> <repository-url>

More: git remote


Push changes from local branch to remote repository

git push -u <remote-name> <branch-name>

More: git push


Push all local branches to remote repository

git push -u <remote-name> -all

More: git push


Force update of remote branch with your changes (after altering the history) (with making sure that you don't affect others)

git push --force-with-lease

More: git push


Change current branch

git checkout <branch-name>

More: git checkout


Create branch

git branch <branch-name>

More: git branch


List all branches

git branch
//-r      also show remote branches

More: git branch


Delete branch

git branch -D <branch-name>

More: git branch


Add git submodule

git submodule add <repository-url> <local-path>
// local-path can have nested folders folder/subfolder/repo-name
// to change the commit to which submoduel points just navigate to the submodule repo folder and do regular git checkout

More: Atlassian git tutorials: git-submodule


Git Pull with Submodule

// For a repo with submodules, we can pull all submodules using
git submodule update --init --recursive

// To update submodules
git pull --recurse-submodules

More: Git Pull with Submodule


Remove all local changes done to a branch

git reset --hard <remote-name>/<branch-name>

More: git reset, StackOverflow: Throw away local commits in git


Merge with merge commit

git merge --no-ff

More: git merge


Merge with squash

git merge --squash

More: git merge


Undo uncommited merge that has conflicts

git merge --abort

//prior to git 1.7.4
git reset --merge

//prior to git 1.6.2
git reset --hard

More: git merge, git reset, StackOverflow: How to undo a git merge with conflicts


Revert commited merge

git revert -m [1 or 2] [merge commit hash]
// the 1 and 2 refer to the parents of the merge - check git history to know which one to use

More: Undo a pushed merge with git


Alter last commit files without changing commit message

git commit --amend --no-edit

More : git revert, Git Tools - Rewriting History, StackOverflow: How to revert a merge commit that's already pushed to remote branch?


Find when a file was deleted

git log -- [file path]

More: git log, StackOverflow: Find when a file was deleted in Git


Find changes to the file beyond renamas

git log --follow [file path]

More: git log


Check git configuration

git config --global --edit

More: git config


Change git line endings to work on windows

git config --global core.autocrlf true

More: git config, StackOverflow: How to change line-ending settings, gitBook: Formatting and Whitespace


Git reading list
Vogella Git Tutorial
Atlassian Git Tutorial
GitHub Guides


Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.