Enhance your version control skills with this handy Git command cheat sheet!

Git Commands Cheat Sheet

Here’s a quick reference for commonly used Git commands:

  1. Initialize a Repository
    git init
  2. Clone a Repository
    git clone <repository_URL>
  3. Add Changes to Staging Area
    git add <file_name>
    git add .
  4. Commit Changes
    git commit -m "Commit message"
  5. Push Changes to Remote Repository
    git push origin <branch_name>
  6. Pull Changes from Remote Repository
    git pull origin <branch_name>
  7. Check Status
    git status
  8. Check Differences
    git diff
  9. Create a New Branch
    git branch <branch_name>
  10. Switch to a Branch
    git checkout <branch_name>
  11. Merge Branches
    git merge <branch_name>
  12. View Commit History
    git log
  13. Undo Changes in Working Directory
    git checkout -- <file_name>
  14. Undo Changes in Staging Area
    git reset HEAD <file_name>
  15. Undo Last Commit
    git reset HEAD^
  16. Revert to a Previous Commit
    git reset --hard <commit_SHA>
  17. Create a Tag
    git tag <tag_name>
  18. List Tags
    git tag
  19. Delete a Tag
    git tag -d <tag_name>
  20. Fetch Remote Changes
    git fetch

These are some commonly used Git commands. Make sure to replace placeholders like <branch_name> and <file_name> with actual branch names or file names.

Git isn’t noticing the changes you made to your .gitignore file

If it seems like Git isn’t noticing the changes you made to your .gitignore file, you might want to check the following points:

  • There might be a global .gitignore file that might interfere with your local one
  • When you add something into a .gitignore file, try this:
git add [uncommitted changes you want to keep] && git commit
git rm -r --cached .
git add .
git commit -m "fixed untracked files"
  • If you remove something from a .gitignore file, and the above steps maybe don’t work,if you found the above steps are not working, try this:
it add -f [files you want to track again]
git commit -m "Refresh removing files from .gitignore file."

// For example, if you want the .java type file to be tracked again,
// The command should be:
// git add -f *.java

Source: https://stackoverflow.com/a/32377642

How to remove a big file wrongly committed

I added a large file to a git repository (102Mb), commited and push and got an error due to size limit limitations on github

remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com. 
remote: error: Trace: 7d51855d4f834a90c5a5a526e93d2668
remote: error: See http://git.io/iEPt8g for more information.
remote: error: File coverage/sensitivity/simulated.bed is 102.00 MB; this exceeds GitHub's file size limit of 100.00 MB

Here, you see the path of the file (coverage/sensitivity/simualted.bed). 

So, the solution is actually quite simple (when you know it): you can use the filter-branch command as follows:

git filter-branch --tree-filter 'rm -rf path/to/your/file' HEAD git push