Quick references for basic tasks with git. For simpler tasks, consider using Gitkraken for a visual interface. However, you have to pay to use all useful features. You might also find the note on GitKraken in commands interesting.
- Download and install (to use command lines): git's homepage.
- Git client tools: Github Desktop (Windows and MacOS, Linux), GitKraken (Windows, Linux, MacOS).
- Do commit early and often. (ref)
- Do make useful commit messages.
- Create a new branch for every new feature.
- Use pull requests to merge code to
master.
- For temporary branches, name them starting with
_.
Check some commit:
Check current
HEADCheck the change of some file,
Check the list of files in the last commit,
Check all commits w.r.t. a specific file
💡 GitKraken:
Ctrl + P > Type "History" > Enter the file path.Create a file
.gitignore.Ignore local changes on some files from
pull/pushClone without ssh using Personal Access Token.
Check if there are conflicts before merging?
If there are changes from both local and remote, there will be conflicts! Something likes that,
If you use Visual Studio Code, there is a small toolbar above each conflict and you can choose which one you prefer to keep!
Prefer one of them?
Keep both? Using Visual Studio Code or,
Already in conflicted state?
Exclude some files from merge (keep
ours),Exclude some folders (we cannot use
git in this case):- If you delete these folders after merging, just commit and later merges will ignore them.
- If you meet a conflict, add the folder's name in a file called
reset_folders.sh
Each time,
Add a description (using Vim editor):
In the case you wanna exit Vim, press <kbd>ESC</kbd> then type
:q to quit or :wq to write and quit.Clean all history of a repo,
Remove from git, not from system,
It’s useful if you accidentally added a secret key on the first commit and you want to make everything again.
In the case you want to discard the changes but want to make a save before moving to another branch to test. You can use below line.
If you wanna get back to the place you saved (and remove it from the stashed list), just go back to the branch you make the save and use
- (Windows) Generate a ssh key
ssh-keygen -t rsa -b 4096 -C "[email protected]"inC:\Users\dinha\.sshunder the nameid_rsa(for private) andid_rsa.pubfor public. It's important, the name!!!!
- Open and copy key in
C:\Users\dinha\.ssh\id_rsa.pub
- Go to Gitlab > Settings > SSH Keys > paste a copied key and name it.
- Clone again the repo and it shoule be working!
Problem with
pre-commit? (Cannot removing it?)You can also go back to the previous commit (
git reset --hard) and you LOSE all uncommit files.WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!Go to
~/.ssh/ (Linux) or C:\Users\dinha\.ssh (Windows), remove the host from known_hosts and re-connect again.Git submodules allow you to keep a git repository as a subdirectory of another git repository.
To stop
Fetching submodule.... when git pull: git config submodule.recurse false!To update all submodules,
- fast-forward means that the commits can be applied directly on top of the working tree without requiring a merge. When
git pulland get this message, we can be sure that the new update are not confict with our current modifications.
- Find big files / commits: Using this scripts and this answer.