git checkout
git checkout .
git reset
Revert the changes to be committed.
git reset
git revert
git revert <commit 1> <commit 2>
git clean
git clean -f
git clean -fd
git diff
git diff --staged
git difftool --cached # cached is a synonym for staged
git diff commit-id-1 commit-id-2
git stash
git stash push -m welcome_cart app/views/cart/welcome.thtml
git remote
git push -d <remote_name> <branch_name>
git branch -d <branch_name>
CR LF
is in Windows, LF
is in Unix and
CR
is in Mac.
git config --global core.autocrlf false
to disable the
conversion to CR LF
in the Windows system.
git merge
fatal: refusing to merge unrelated histories
The “fatal: refusing to merge unrelated histories” Git error occurs when two unrelated projects are merged (i.e., projects that are not aware of each other’s existence and have mismatching commit histories).
A situation of this is: You have created a new repository, added a few commits to it, and now you are trying to pull from a remote repository that already has some commits of its own. Git will also throw the error in this case, since it has no idea how the two projects are related.
The error is resolved by toggling the
allow-unrelated-histories
switch.
git pull origin master --allow-unrelated-histories
git config --global diff.tool vimdiff
git config --global difftool.prompt false
Typing git difftool
yields the expected behavior.
You can use the following basic commands to merge:
do
- Get changes from other window into the current
window.dp
- Put the changes from current window into the other
window.]c
- Jump to the next change.[c
- Jump to the previous change.zo
- Open folded lines.zc
- Close folded lines.zr
- Unfold both files completely.zm
- Fold both files completely.Ctrl-w-w
- change window.If more than 2 buffers in the merge, use :ls
to show the
buffer number. Then use 2do
to put the changes from the no.
2 buffer to this buffer.
Refer to Git Hooks | Atlassian Git Tutorial.
pre-commit
The following example formats *.f90
in the changes to be
commited. If the formatter raises warnings, set exit 1
which results in failed git commit. Then turn to resolve the warnings of
formatting and try the commit again.
#!/bin/bash
# Regexp for grep to only choose some file extensions for formatting
# exts="\.\(f90\|ext2\)$"
exts="\.\(f90\)$"
# The formatter to use
formatter=`which fprettify`
# Check availability of the formatter
if [ -z "$formatter" ]
then
1>&2 echo "$formatter not found. Pre-commit formatting will not be done."
exit 0
fi
# Format staged files
for file in `git diff --cached --name-only --diff-filter=ACMR | grep $exts`
do
echo "Formatting $file"
message=$("$formatter" "$file" 2>&1)
if [[ "$message" == *"WARNING"* ]]; then
printf "$message\n"
ret=1
else
git add "$file"
fi
done
if [[ "$ret" == "1" ]]; then
RED='\033[0;31m'
NC='\033[0m' # No Color
printf "${RED}Formatting is not finished successfully!${NC}\n"
printf "${RED}Thus the changes are not committed!${NC}\n"
exit 1
fi
# If no files left in index after formatting - fail
ret=0
if [ ! "`git diff --cached --name-only`" ]; then
1>&2 echo "No files left after formatting"
exit 1
fi