Useful GIT commands

When working on both Windows and Linux platforms be sure to add proper line ending support, i.e. work with only LF in repositories.

  • On Windows work with CRLF, git config --global core.autocrlf  true
  • On Linux work with LF, git config --global core.autocrlf input

If you want to force a repository to always use specific line endings, add the setting to a .gitattributes file.

The table below shows some useful git commands. In the command column, a quoted word is parameter, e.g. ‘repo’ could be origin. The local and the remote repository are called repo and remote.

git clone 'directory1' 'directory2'Clone remote at 'directory1' to repo at 'directory2'.
git remote -vList urls/directorires of all remotes.
git add 'remote' 'directory'Add the remote at 'directory' to the repo with nickname 'remote'.
git fetch 'remote' 'branch'Update cache of repo with remote branch.
git pull 'remote' 'branch'Merge content of remote branch into repo (shortcut for git fetch.. & git merge... ).
git merge 'branch'Merge content of local 'branch' into current branch.
git merge 'remote'/'branch'Merge content of local cached remote/branch into repo.
git push 'repo' 'branch'Merge local repo into remote. Don't try to push into non bare remote, instead make a pull from the remote.
git commit -m “commit comment”Commit changes with the comment.
git add .Prepare all modified/new files for commit.
git add 'file'Prepare 'file' for commit.
git branch 'branch'Create branch 'branch'.
git checkout .Undo changes on all files.
git checkout 'file'Undo changes on 'file'.
git checkout 'commit'Checkout specific commit. To go back to HEAD use git checkout 'branch'
git checkout 'branch'Change working directory to head of branch
git reset HEAD~1Remove latest commit, leaving changes in working directory.
git reset --hard HEAD~1Remove latest commit and undo changes in working directory.
git stashSave changes in working directory.
git stash listShow list of saved stashes.
git stash popApply changes from last stash.
git config --listList git configuration.
git clean -dxfClean up non controlled files in working directory (use -dxn to do a dryrun)
git log --statShow commit log, inclusive which files have been changed
git commit --amendAdd changes to last commit. Don't use if you have already pushed the last commit to a remote.

Leave a Reply

Your email address will not be published. Required fields are marked *