Infinite | Squares

Art of code and more

Git Commands Collection

Git is very powerful distributed revision control. It has lots of commands but I only use several of them. I often forget some of the rarely used git commands so I make notes here.

  • git clone REPOURL LOCALFOLDER –> obviously to clone
  • git status –> get status of what files are modified, added or deleted
  • git log –> get commit log
  • git checkout -b NEWBRANCH –> create new branch from existing
  • git checkout BRANCH –> checkout the branch.
  • git add . –> add updated files into staging
  • git commit -am "commit message" –> commit staging
  • git push REMOTE BRANCH –> push to remote
  • git fetch REMOTE –> get update from remote
  • git merge REMOTE/BRANCH –> merge with remote branch
  • git pull REMOTE BRANCH –> fetch and merge from remote branch
  • git rm --cached FILE –> remove file from git cache
  • git rm -r --cached FOLDER –> remove folder from git cache
  • git checkout -- . –> reset all current changes

Here are some not-so-common git command that I found out based on necessity

  • git checkout SHA –> checkout the commit SHA
  • git diff --name-status SHA1 SHA2 –> check what file is changed between two commits
  • git reset HEAD -- . –> clear staging
  • git merge --abort –> cancel merge
  • git reset --hard HEAD~1 –> nuke the last commit
  • git branch -a –> list all branch remote and local
  • git branch -r –> list remote branch
  • git branch -m <oldname> <newname> –> rename any branch
  • git branch -m <newname> –> rename current branch
  • git branch -d <branch> –> delete branch
  • git push origin newname –> push new branch to remote
  • git push origin :oldname –> remove old branch from remote
  • git clean -fx –> delete untracked files and directories, -f force, -n directory
  • git clean -n –> clean dry run, list what to be deleted
  • git checkout -- FILENAME –> cancel changes on a filename
  • git log -- FILENAME –> view a file changing history
  • git commit --amend -m "New commit message" –> change latest commit message

Reset

1
2
3
4
5
6
7
8
git reset --hard HEAD~1
->The HEAD~1 means the commit before head.

git reset --hard HEAD~4
git reset --hard <sha1-commit-id>

git reset --hard <REMOTE>/<BRANCH_NAME>
git reset --hard origin/master

Delete a file from commit

1
2
3
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

Update .gitignore and commit

Replace master branch

1
2
3
4
5
6
7
8
9
// on development machine
git branch -m master old-master
git branch -m newbranch master
git push -f origin master

// on deployment machine
git branch -m master old-master
git fetch
git checkout master

Delete history of a file. This is useful if you accidentally included a file that should not be included (contains sensitive information)

1
2
3
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch FILENAME' --prune-empty --tag-name-filter cat -- --all
git push --force --verbose --dry-run
git push --force

Ignore files that have already been committed to a Git repository

1
2
3
git rm -r --cached .
git add .
git commit -m ".gitignore is now working"

Undo last merge. Assume local master was not ahead of origin/master. It preserve changes. Here ORIG_HEAD should point to a commit directly before merge has occurred.

1
git reset --merge ORIG_HEAD

I will add more when I found out other git commands

Comments