All - GradedJestRisk/git-training GitHub Wiki
git <CMD> --help
List:
- show changes
- since last commit
git diff
- between current and a branch
?
- between 2 branches
git diff <BRANCH_1> <BRANCH_2>
- since last commit
- file
- with no branch switch:
git show REF:FILENAME
- with no branch switch:
- commit
- per hunks
git add --patch
- without test (disable pre-commit hook)
git commit --no-verify
- change last commit
git commit --amend
- per hunks
- push
- without test (disable pre-push hook)
git push --no-verify
- after rebase
git push --force--with-lease
(preventing someone else'change to be overwritten)
- without test (disable pre-push hook)
4 operations:
- set with no value : * text
- unset: * -text
- set with specific value: * text=auto
- unspecified: * !text
Header (that apply to ALL files) starts by *
, eg: * text=auto
Normalization: when a text file is normalized, its line endings are converted to LF in the repository.
Control when normalization is triggered. Values:
- set : do not check if file contains text, normalize anyway
- unset: do not check for text, never normalize (check-in and check-out)
- auto : check for text on check-in; if this is text, normalize (when the file has been committed with CRLF, no conversion is done ??)
- unspecified : use user-specific settings instead (core.autocrlf)
Control how normalization is applied. Values:
- CRLF:
- on check-in: force normalization (convert to LF)
- on check-out: convert LE to CRLF
- LF:
- on check-in: force normalization (convert to LF)
- on check-out: prevent conversion to CRLF (preserve LF)
- unspecified (!eol): look at the core.eol config
Clone in specific folder
git clone [email protected]:whatever FOLDER_NAME
Because of branch name restriction, see here, better avoid:
- slash /, antislash \
- dot .
- space
- tilde ~
- caret ^
- colon :
- question-mark ?
- asterisk *
- open bracket []
- parenthese ()
- arobase @
- use dash -, if you don't use it as first character
- use parenthese (), if you double-quote the branch name
git branch "my()branch"
, but be careful especially when using zsh (see here why
- a-z, A-Z, 1-9
- - wiwthin
git checkout version <TAG_NAME> -b <BRANCH_NAME> git checkout tags/version <TAG_NAME> -b <BRANCH_NAME>
Manage uncommited changes when switching branches.
Handful:
- push changes to stash
git stash push
- list stashed changes
git stash list
- pop changes from stash
git stash pop
- show stash in status:
git config --global status.showStash true
- restore specific stashed changes
git stash apply <STASH_ID>
- clear the stash
git stash clear
-
git stash push --patch (-p)
Discard end-of-line difference (use it only on Windows)
git config --global core.whitespace cr-at-eol
Handful:
- add modified,deleted
git add --all
- add all (new, modified,deleted)
git add --all
- handle hunks separately
git add --patch
List:
- Write a comment
- you forgot something
- and not pushed
- just a line:
git commit --amend
- changed many things, change the commit message
git commit --amend --no-edit
- just a line:
- and already pushed :
git push --force origin <BRANCH>
- and not pushed
Options
- rebase :
--rebase=merges
- preserve uncommited changes (use stash) :
--autostash
[pull] rebase = merges [rebase] autoStash = true
View all modifications on a single file
git log --follow -- <FILE_NAME>
View all modifications made by a commit
git show <COMMIT_SHA1>
Compare 2 branches, ignoring spaces
git diff --ignore-space-change <BRANCH_1> <BRANCH_2>
Remove remote branches
git fetch -p
Remove local branches not on remote
git remote prune origin
For files you don't want to be handled in git:
- binary files;
- temporary files;
- IDE files.
Create a .gitignore
file, at root of the repo, and commit it.
Example for all java compiled classes *.class
*.class
In you want to exclude some file under a specific folder, that may be anywhere in your repo, prefix this folder with a **/
**/.idea/workspace.xml
However, if .gitignore has been modified after files appeared in untracked changed, these files won't disappear.
Execute this to remove them form untracked changes.
git rm -r --cached . git add . git commit -m "fixed untracked files"
To deal with specific issues (ie: some use Windows, some others Linux)
Update the repo-based local file exclude
: it will filter objects before the shared .gitignore
applies.
It is stored in .git/info/exclude
Setup
touch ~/.gitignore git config --global core.excludesfile '~/.gitignore'
Remove
git config --global --unset core.excludesfile
=> to check...
Git store everything in repository by default using Unix EOL characters : LF. (??)
core.autocrlf
parameter controls this:
- true: Checkout Windows-style, commit Unix-style;
- input: Checkout as-is, commit Unix-style (when working in Unix, if by incident a bad file is introduced, it will be normalized);
- false: Checkout as-is, commit as-is.
For cross-platforms projects, use LF in repository, so set:
- Unix: input (or false if you're lucky);
- Windows: true.
- normalize text files when added
- files that are already normalized in the repository stay normalized.
Handful:
- get existing settings file:
git config --list --show-origin
- show all config values :
git config --list --show-origin
- show all repository-specific values:
git config --local --list
- show specific config value :
git config --get <PROPERTY>
Set config on repository (not global) Command-line
git config user.name “Joe Bloggs” git config user.email “[email protected]”Config file (.git/config)
[user] name = "Joe Bloggs” email = “[email protected]”
git config --global --unset <PROPERTY>
List:
- locate local repo :
find ~ -name ".git" 2>/dev/null
Steps:
- add target
git remote add -f b path/to/repo_b.git
- update target
git remote update
- get diff
git diff master remotes/b/master
- remove target
git remote rm b
Windows : git update-git-for-windows
Get remote: .git/config
(..) [remote "origin"] url = https://github.com/g0t4/jenkins2-course-spring-boot.git fetch = +refs/heads/*:refs/remotes/origin/* (..)
Get all repositories (= find all .git folders):
- Linux:
find / -name ".git"
- Windows (PowerShell) :
Get-ChildItem . -Attributes Directory,Directory+Hidden -ErrorAction SilentlyContinue -Include ".git" -Recurse
where git.exe
You can use a template for
- all repositories
- a specific repository (store that template in the repo)
echo "Feature - Symptom - Cause " > .gitCommitTemplate git config --global commit.template .gitCommitTemplate
Save last commit (?)
vi .git/hooks/post-commit #!/bin/sh printf "`git log -1 --pretty=%s`" > .gitmessage.txt chmod +x .git/hooks/post-commit
Prepare commit message & enforce rules
Remove an already-pushed commit (warn people that checked it out locally)
git reset --hard HEAD^ git push --force-with-lease
Remove from history implies remove all history
Include OS command and git staging:
- git-rm
- git-mv
List:
- from a commit up to HEAD
rebase --execute
- on commit rantge: rev-list