Contents

Git Simple Cheatsheet


Initializing GIT

Configuration

  1. Name,
1
git config --global user.name "Your Name" 
  1. Email,
1
git config --global user.email "email@example.com" 

📜 “….” - words in this note mean variable. You have to change ‘em!

Initialization

  1. Initialize on a empty directory,
1
git init 
  1. Clone from a GitHub/ similar repository,
1
 git clone "repo-user/repo-name"
  1. Clone with all sub-modules,
1
git clone --recurse-submodules user/name          

📜 -j8 can be used to boost performance!

  1. For an already cloned repository,
1
git submodule update --init --recursive 

Working with GIT

Commit

  1. Check status
1
git status 
  1. Add a (file|folder) | Work → Stage
1
git add "file|folder"

📜 file = file | folder = folder/

  1. Add everything (except files in the .gitignore )
1
git add --all 

or,

1
git add . 
  1. Git commit, | Stage → Commit
1
git commit -m "message" 
  1. Git commit with tracked file changes,
1
git commit -am "message" 

📜 git add . will track untracked files (not ignored) but git add -u won’t!

Reset

  1. reset staged changes | Stage → Work
1
git reset "file|folder"

📜 or, try git reset . to reset everything or, git reset --hard, which may fix errors regarding git pull!

  1. To remove files that are listed in the .gitignore but still on the repository,
1
git rm -r --cached .

Then a commit should do the trick for you.

Restore

  1. restore one file with
1
git restore file/folder            
  1. restore everything with,
1
git restore . 

Logging

  1. Show the sequence of commits starting from the current one, in reverse chronological order of the Git repository in the current working directory:
1
git log 
  1. Show the history of a particular file or directory, including differences:
1
git log -p  path/to/file_or_directory  
  1. Show an overview of which file(s) changed in each commit:
1
git log --stat 
  1. Show a graph of commits in the current branch using only the first line of each commit message:
1
git log --oneline --graph 
  1. Show a graph of all commits, tags and branches in the entire repository:
1
git log --oneline --decorate --all --graph 
  1. Show only commits whose messages include a given string (case-insensitively):
1
git log -i --grep  search_string  
  1. Show the last N commits from a certain author:
1
git log -n  number  --author= author  
  1. Show commits between two dates ( yyyy-mm-dd ):
1
git log --before=" 2017-01-29 " --after=" 2017-01-17 " 

Sub-module

  1. First set it by,
1
git submodule add "https://github.com/user/repo-name" "path/"      

You can track them from .gitmodules file (a hidden text file) and to sync it’s update with the parent, cd into it, git pull and then, git add in the parent.

Large file

  1. Install git-lfs first. Available on most of official repository of various Linux distributions.

  2. set it up ( once per user ) by running, git lfs install

make sure file is tracked in the .gitattributes and add it in git. For migrations git-lfs-migrate
This link can help you further.

Find more from,

Git Large File Storage

Remote Branch

  1. List all local branches
1
git branch 

📜 use -r for remote branches or, -a for all (remote + local)

  1. New branch
1
git branch            branch-name            

📜 Use -M to move a branch, like, git branch -M main

  1. Change branch
1
git checkout branch-name            

📜 Not just branch, you can also trigger a previous commit!

  1. Deleting a branch
1
git branch -D branch-name            

Merge

  1. Merge an another branch with the current one,
1
git merge another-branch           

📜 To fix errors, you may need, git merge main --allow-unrelated-histories

While merging, there can be conflicts which needs to be resolved manually. Or perhaps you want to prefer your changes? Then, this command is for you,

1
`git merge -s ours`another-branch

Likewise, if you prefer their changes, try,

1
git merge -X theirs another-branch

Sync

  1. List all remote,
1
git remote -v 
  1. add remote,
1
git remote add url    
  1. remove remote,
1
git remote remove origin  
  1. push,
1
git push -u remote branch        

📜 - e will save remote and _ branch _, for you so next time, just run, git push

  1. pull,
1
git pull