Initializing GIT

Configuration

  1. Name

git config --global user.name "Your Name"

  1. Email

git config --global user.email "email@example.com"

Important

Italic words in this note mean variable. You have to change ‘em!

Initialization

  1. Initialize on a empty directory,

git init

  1. Clone from a GitHub/ similar repository,

git clone _repo-user/repo-name_

  1. Clone with all sub-modules,

git clone --recurse-submodules ````````` ```````` ``````` `````` ````` ```` ``` `` `user/name` `` ``` ```` ````` `````` ``````` ```````` `````````

Important

-j8 can be used to boost performance!

  1. For an already cloned repository,

git submodule update --init --recursive

Working with GIT

Commit

  1. Check status

git status

  1. Add a (file|folder) | Work → Stage

git add file|folder

Important

file = file | folder = folder/

  1. Add everything (except files in the .gitignore)

git add --all or, git add .

  1. Git commit, | Stage → Commit

git commit -m "message"

  1. Git commit with tracked file changes(git add -u),

git commit -am "message"

Important

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

Reset

  1. reset staged changes | Stage → Work

git reset _file|folder_

Important

or, try git reset . to reset everything!

Restore

  1. restore one file with git restore file/folder

  2. restore everything with,

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:

git log

  1. Show the history of a particular file or directory, including differences:

git log -p path/to/file_or_directory

  1. Show an overview of which file(s) changed in each commit:

git log --stat

  1. Show a graph of commits in the current branch using only the first line of each commit message:

git log --oneline --graph

  1. Show a graph of all commits, tags and branches in the entire repository:

git log --oneline --decorate --all --graph

  1. Show only commits whose messages include a given string (case-insensitively):

git log -i --grep search_string

  1. Show the last N commits from a certain author:

git log -n number --author=author

  1. Show commits between two dates (yyyy-mm-dd):

git log --before="2017-01-29" --after="2017-01-17"

Sub-module

  1. First set it by,

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 [link] can help you.

Find more from,

Git Large File Storage

Download and install the Git command line extension.
https://git-lfs.github.com/

Remote

Branch

  1. List all local branches

git branch

Important

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

  1. New branch

git branch branch-name

Important

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

  1. Change branch

git checkout branch-name

Important

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

  1. Deleting a branch

git branch -D branch-name

Merge

  1. Merge an another branch with the current one,

git merge _another-branch_

Important

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

Sync

  1. List all remote,

git remote -v

  1. add remote,

git remote add url

  1. remove remote,

git remote remove origin

  1. push,

git push -u _remote branch_

Important

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

  1. pull,

git pull