git #git
core.autocrlf
property either for the repo or globally.git add . -u
git commit -m "..."
##- Remove every file from Git's index.
##git rm --cached -r .
rm .git/index
git reset --hard
git add -u .
git add .gitattributes
git commit -m "Normalize all the line endings"
mkdir repo-dir
cd repo-dir
git init
git remote add -f origin git-repo-uri
git config core.sparsecheckout true
bin/
images/
*.htm
git checkout master
git read-tree -m -u HEAD
git log
git log --oneline --graph
git log --name-only
git log -p filename
git log --no-merges <parentbranch>..
Eg: git log --no-merges master..
git log -Sstring
will show only commits that added or removed the specified string
git log -Gregexpattern
# also:
git log --grep [-i] [-E] 'ext-regex-pattern'
git log -L :functionname:filename
git log -- filename
--author="..."
Only show commits made by that author--reverse
Show commits in reverse order (Oldest commit first)--after="..."
Show all commits that happened after certain date--before="..."
Show all commits that happened before certain datadevelop
branch that are not present in master
git log master..develop
git log develop..master
shows commits in master
not merged in develop
.git fetch origin
git log origin/master..HEAD
git show <commit>
git show HEAD~2
# or git show HEAD@{2}
git reset
to unstage added filesgit reset --hard HEAD
or alternately git checkout .
git reset --hard <commit-hash>
.
All changes made after this commit are discarded.git reset <commit-hash>
.
Run git add .
and git commit
to add the changes back to git.git reset --soft <commit-hash>
.
All changes made after this commit are automatically added, i.e. staged for commit, so
you only need to run git commit
to add them back in.git checkout <commit-hash> <filename>
or
git checkout <branchname> <filename>
git diff
or git difftool
for a more visual diffgit difftool -d
when used with tools like meld or winmerge which allow full directory diffsgit diff HEAD
git diff branchname
git diff --stat
or
git diff --no-commit-id --name-only
git diff --staged
git show branchname:filename
git remote prune origin
# list merged branches
git branch --merged
...
# Remove these branches
git branch -d ...
List unmerged branches
git branch --no-merged
Show tracking branches
git branch -vv
git revert -n <commit>
git revert
without -n
will automatically re-commit
reverted files, prompting you to write a new commit message.
The -n
flag tells git to not commit, since all we want to do is look.git add -i
allows interactive staginggit add -p
allows interactive patching for added filesgit stash -p
allows interactive stashinggit stash
git stash branch branchname
git reset HEAD~ --soft
git stash
git stash branch branchname
git stash
git checkout branchname
git stash pop
git branch newbranchname
git reset HEAD~n --hard # n is the number of commits to undo
git checkout newbranchname # this contains the previous commits from master
git checkout your-branch-name
git cherry-pick master
git checkout master
git reset HEAD~n --hard # n is the number of commits to undo
git commit -a
NOTE Files not tracked will not be added
git commit --amend
git add ...
git commit --amend --no-edit # reuse previous commit msg
:cq
to make vim exit witha a non-0 exit code. This
makes git abort the commit.# git add/rm/mv ...
git commit --fixup <badcommit>
git rebase -i --autosquash <badcommit>^
git grep
- Search a git treegit grep
instead of grep -R
to search a git tree.-p
git grep -p basename *.sh
--break
and --heading
to make the output more readable-e pattern1 --and -e pattern2
-e pattern1 -e pattern2
--not -e pattern1
git push
syntaxgit push [remote-name] [branch-or-tag-to-push]
git push origin master
git push origin v1.0.2
git remote show <remotename>
git remote show origin
git show <tagname>
# set proxy before this step if required
git clone https://github.com/petervanderdoes/gitflow-avh.git
cd gitflow-avh
sudo make install
git config --global user.name "your name"
git config --global user.email "your@email.id"
git config --global push.default simple
git config --global color.ui auto
git config --global core.editor vim
git config --global diff.tool meld #used by git difftool, use vimdiff if meld is not available
git config --global merge.tool meld #used by git mergetool, use vimdiff if meld is not available
# For repos with submodules
git config status.submodulesummary 1
git config diff.submodule log
#Linux:
git config --global core.autocrlf input
# Windows
# git config --global core.autocrlf true
git config --system core.longpaths true
or alternately
git config --global core.longpaths true
git config --global alias.ci commit
git config --global alias.st "status --short"
git config --global alias.stat status
git config --global alias.co checkout
git config --global alias.up pull
git config --global alias.unstage "reset HEAD"
git config --global alias.undo "reset --hard HEAD"
git config --global alias.ls "ls-files --eol"
git config --global alias.ls-log "log --name-only --decorate"
git config --global alias.l "log --oneline --graph --decorate"
# if you install git-flow
git config --global alias.release "flow release"
git config --global alias.hotfix "flow hotfix"
git config --global alias.feature "flow feature"
git config --global alias.bugfix "flow bugfix"
Note
git config --global
modifies $HOME/.gitconfig
git config --system
modifies the systemwide /etc/gitconfig
git config --file <filename>
creates/modifies the named config file
git submodule add <repourl>
.gitmodules
along with the base dirgit commit -a -m "added submodules"
git clone --recursive <main-git-project-url>
--recursive
, rungit pull # pull in latest changes
git submodule init
git submodule update
git submodule update
master
branch present on the servergit submodule update --remote <submodulename>
cd <submodule>
git fetch
git merge origin/master
master
, to say for example, develop
modify the .gitmodules
file usinggit config -f .gitmodules submodule.<modulename>.branch develop
git submodule update --remote
will update the submodule to the develop
branchgit diff --submodule
--submodule
as default for diff:git config diff.submodule log
git push
in the submodule tree.git push --recurse-submodules=check
git push --recurse-submodules=on-demand
git submodule foreach 'git stash'
git submodule foreach 'git diff'
git tag --annotate ...
)git describe master
git archive tagname --prefix projdir/ | gzip -c > $(git describe master).tar.gz
git archive tagname --prefix projdir/ --format zip -o $(git describe master).zip
git rebase --onto newbase branch_x topicbranch
git rebase --onto master feature-1 feature-2
will checkout feature-2, find the ancestor of feature-1 and feature-2,
apply all patches from the ancestor commit upto feature-2's latest commit to the master branch. The master branch can be
fast forwarded to feature-2 using: git checkout master; git merge feature-2
git rebase basebranch topicbranch
git rebase master feature-1
will apply all patches from the ancestor commit upto feature-1's latest commit to the master branch. The master branch can be
fast forwarded to feature-1 using: git checkout master; git merge feature-1
Note: After fast-fwding, the feature branches can be deleted.
git pull --rebase
git fetch origin; git merge origin/master
git fetch origin
git log --no-merges localbranch..origin/master
git checkout master
git merge localbranch
git merge origin/master
git push origin/master
*.[oa]
*.so
*.class
*.jar
*.zip
*.pdf
build/
dist/
# Set default behvavior
* text=auto
# Windows batch files
*.bat text eol=crlf
*.cmd text eol=crlf
# Binaries
*.jar binary
*.zip binary
*.png binary
*.gif binary
*.jpg binary
*.jpeg binary
*.xz binary
*.zst binary
*.gz binary
*.bz2 binary