Initializing GIT
Configuration
- Name,
1
|
git config --global user.name "Your Name"
|
- Email,
1
|
git config --global user.email "email@example.com"
|
📜 “….” - words in this note mean variable. You have to change ‘em!
Initialization
- Initialize on a empty directory,
- Clone from a GitHub/ similar repository,
1
|
git clone "repo-user/repo-name"
|
- Clone with all sub-modules,
1
|
git clone --recurse-submodules user/name
|
📜 -j8
can be used to boost performance!
- For an already cloned repository,
1
|
git submodule update --init --recursive
|
Working with GIT
Commit
- Check status
- Add a (file|folder) | Work → Stage
📜 file = file | folder = folder/
- Add everything (except files in the
.gitignore
)
or,
- Git commit, | Stage → Commit
1
|
git commit -m "message"
|
- 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!
Clean ignored files
- To remove files that are listed in the
.gitignore
but still on the repository,
This will remove all files from staged copy. Then a commit should do the trick for you due to your .gitignore
. And finally to remove ignored files locally, you can try git clean -d -x -f
.
2. To remove files that are on the .gitignore
but not on the repository (commit), then simple git clean
should do the trick.
Reset
- 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
!
Restore
- restore one file with
1
|
git restore file/folder
|
- restore everything with,
Logging
- Show the sequence of commits starting from the current one, in reverse chronological order of the Git repository in the current working directory:
- Show the history of a particular file or directory, including differences:
1
|
git log -p path/to/file_or_directory
|
- Show an overview of which file(s) changed in each commit:
- Show a graph of commits in the current branch using only the first line of each commit message:
1
|
git log --oneline --graph
|
- Show a graph of all commits, tags and branches in the entire repository:
1
|
git log --oneline --decorate --all --graph
|
- Show only commits whose messages include a given string (case-insensitively):
1
|
git log -i --grep search_string
|
- Show the last N commits from a certain author:
1
|
git log -n number --author= author
|
- Show commits between two dates ( yyyy-mm-dd ):
1
|
git log --before=" 2017-01-29 " --after=" 2017-01-17 "
|
Sub-module
- 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
-
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
This link can help you further.
Find more from,
Git Large File Storage
Remote Branch
- List all local branches
📜 use -r
for remote branches or, -a
for all (remote + local)
- New branch
📜 Use -M
to move a branch, like, git branch -M main
- Change branch
1
|
git checkout branch-name
|
📜 Not just branch, you can also trigger a previous commit!
- Deleting a branch
1
|
git branch -D branch-name
|
Merge
- 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
- List all remote,
- add remote,
- remove remote,
1
|
git remote remove origin
|
- push,
1
|
git push -u remote branch
|
📜 -u
will save remote and branch, for you so next time, just run, git push
!
- pull,