Initializing GIT
Configuration
- Name
git config --global user.name "
Your Name
"
git config --global user.email "
email@example.com
"
Important
Italic words in this note mean variable. You have to change ‘em!
Initialization
- Initialize on a empty directory,
git init
- Clone from a GitHub/ similar repository,
git clone
_repo-user/repo-name_
- Clone with all sub-modules,
git clone --recurse-submodules
````````` ```````` ``````` `````` ````` ```` ``` `` `user/name` `` ``` ```` ````` `````` ``````` ```````` `````````
Important
-j8 can be used to boost performance!
- For an already cloned repository,
git submodule update --init --recursive
Working with GIT
Commit
- Check status
git status
- Add a (file|folder) | Work → Stage
git add
file|folder
Important
file = file | folder = folder/
- Add everything (except files in the
.gitignore
)
git add --all
or, git add .
- Git commit, | Stage → Commit
git commit -m "
message
"
- 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
- reset staged changes | Stage → Work
git reset
_file|folder_
Important
or, try git reset . to reset everything!
Restore
-
restore one file with
git restore
file/folder
-
restore everything with,
git restore :/
Logging
- 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
- Show the history of a particular file or directory, including differences:
git log -p
path/to/file_or_directory
- Show an overview of which file(s) changed in each commit:
git log --stat
- Show a graph of commits in the current branch using only the first line of each commit message:
git log --oneline --graph
- Show a graph of all commits, tags and branches in the entire repository:
git log --oneline --decorate --all --graph
- Show only commits whose messages include a given string (case-insensitively):
git log -i --grep
search_string
- Show the last N commits from a certain author:
git log -n
number
--author=
author
- Show commits between two dates (yyyy-mm-dd):
git log --before="
2017-01-29
" --after="
2017-01-17
"
-
Copyright
Sub-module
- 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
-
Install
**git-lfs**
first. Available on most of official repository of various Linux distributions. -
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
- List all local branches
git branch
Important
use -r for remote branches or, -a for all (remote + local)
- New branch
git branch
branch-name
Important
Use -M to move a branch, like, git branch -M main
- Change branch
git checkout
branch-name
Important
Not just branch, you can also trigger a previous commit!
- Deleting a branch
git branch -D
branch-name
Merge
- 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
- List all remote,
git remote -v
- add remote,
git remote add
url
- remove remote,
git remote remove
origin
- push,
git push -u
_remote branch_
Important
-e will save remote and branch, for you so next time, just run, git push
- pull,
git pull