There are many different ways to use Git. Git supports many command-line tools and graphical user interfaces. The Git command line is the only place where you can run all the Git commands.
Ref.: ProGit
-v
--version
git config --global user.name "NAME"
git config --global user.email "EMAIL ID"
git init
git add <filename>
To track all the files
git add <filename> .
The Git rm –cached flag removes a file from the staging area.
git rm --cached <filename>
Undo git rm
git reset
- Current log (author,mail,date)
Previous commit
git log
To see a very compressed log where each commit is one line:
git log --pretty=oneline
See only which files have changed:
git log --name-status
- Issue Tracking
git commit -m <msg>
- Reverts a commit
git revert HEAD
Checking the Status of Your Files
git status
Short Status
git status -s
mv
git mv file_from file_to
check the first 7 char.
[main (root-commit) fd914d5]
Working Dir. ----> Staging area ----> Local commit
git add <file name>
Tags are ref's that point to specific points in Git history.
git tag
Delete Tag
git tag -d v1.0
- Annotated Tag
git tag -a v1.0 -m "<msg>"
- Lightweight Tag
git tag v1.4-lw
To push the tag
git push origin v1.0
Create branch:
git checkout -b <branch name>
List the branch:
git branch
Switch to an existing branch in Git:
git checkout <branch name>
Switch to previous branch:
git switch -
Delete the branch:
git branch -d <branch name>
To push branch to Git repo.:
git push origin <branch name>
Merging with the main branch:
git merge <branch name>
- Creating shortcuts' for Git commands
eg: Instead of typing git commit, you just need to type git ci.
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
Send a collection of patches as emails
git send-email [<options>] <file|directory>…​
git send-email [<options>] <format-patch options>
git send-email --dump-aliases
shows the diff between your staged changes and the commit
git diff --cached <commit>
git help everydaygit help -ggit log -S'<a term in the source>'git log -p <file_name>git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch <path-to-your-file>' --prune-empty --tag-name-filter cat -- --all && git push origin --force --allgit fetch origin && git reset --hard origin/master && git clean -f -dgit ls-tree --name-only -r <commit-ish>git update-ref -d HEADgit reset --keep <commit>git diff --name-only --diff-filter=Ugit diff-tree --no-commit-id --name-only -r <commit-ish>git diffgit diff --cachedAlternatives:
git diff --stagedgit diff HEADgit branch --merged mastergit checkout -Alternatives:
git checkout @{-1}git branch --merged master | grep -v '^\*' | xargs -n 1 git branch -dAlternatives:
git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d # will not delete master if master is not checked outgit branch -vvgit branch -u origin/mybranchgit branch -d <local_branchname>git push origin --delete <remote_branchname>Alternatives:
git push origin :<remote_branchname>git branch -dr <remote/branch>git tag <tag-name>git tag -d <tag-name>git push origin :refs/tags/<tag-name>git checkout -- <file_name>git revert <commit-ish>git reset <commit-ish>git commit -v --amendgit cherry -v mastergit commit --amend --author='Author Name <email@address.com>'git commit --amend --reset-author --no-editgit remote set-url origin <URL>git remoteAlternatives:
git remote showgit branch -agit branch -rgit add -pcurl -L http://git.io/vfhol > ~/.git-completion.bash && echo '[ -f ~/.git-completion.bash ] && . ~/.git-completion.bash' >> ~/.bashrcgit log --no-merges --raw --since='2 weeks ago'Alternatives:
git whatchanged --since='2 weeks ago'git log --no-merges --stat --reverse master..git checkout <branch-name> && git cherry-pick <commit-ish>git branch -a --contains <commit-ish>Alternatives:
git branch --contains <commit-ish>git config --global alias.<handle> <command>
git config --global alias.st statusgit stashAlternatives:
git stash pushgit stash -kAlternatives:
git stash --keep-indexgit stash push --keep-indexgit stash -uAlternatives:
git stash push -ugit stash push --include-untrackedgit stash push -m <message>Alternatives:
git stash push --message <message>git stash -aAlternatives:
git stash --allgit stash push --allgit stash listgit stash show -p <stash@{n}>git stash apply <stash@{n}>git stash popAlternatives:
git stash apply stash@{0} && git stash drop stash@{0}git stash clearAlternatives:
git stash drop <stash@{n}>git checkout <stash@{n}> -- <file_path>Alternatives:
git checkout stash@{0} -- <file_path>git ls-files -tgit ls-files --othersgit ls-files --others -i --exclude-standardgit worktree add -b <branch-name> <path> <start-point>git worktree add --detach <path> HEADgit rm --cached <file_path>Alternatives:
git rm --cached -r <directory_path>git clean -ngit clean -fgit clean -f -dgit submodule foreach git pullAlternatives:
git submodule update --init --recursivegit submodule update --remotegit cherry -v masterAlternatives:
git cherry -v master <branch-to-be-merged>git branch -m <new-branch-name>Alternatives:
git branch -m [<old-branch-name>] <new-branch-name>git rebase master feature && git checkout master && git merge -git archive master --format=zip --output=master.zipgit add --all && git commit --amend --no-editgit fetch -pAlternatives:
git remote prune origingit branch -vv | grep ': gone]' | awk '{print <!-- @doxie.inject start -->}' | xargs git branch -D git rev-list --reverse HEAD | head -1Alternatives:
git rev-list --max-parents=0 HEADgit log --pretty=oneline | tail -1 | cut -c 1-40git log --pretty=oneline --reverse | head -1 | cut -c 1-40git log --pretty=oneline --graph --decorate --allAlternatives:
gitk --allgit log --graph --pretty=format:'%C(auto) %h | %s | %an | %ar%d'git log --graph --decorate --oneline $(git rev-list --walk-reflogs --all)git subtree push --prefix subfolder_name origin gh-pagesgit subtree add --prefix=<directory_name>/<project_name> --squash git@github.com:<username>/<project_name>.git mastergit subtree pull --prefix=<directory_name>/<project_name> --squash git@github.com:<username>/<project_name>.git mastergit bundle create <file> <branch-name>git clone repo.bundle <repo-dir> -b <branch-name>git rev-parse --abbrev-ref HEADgit update-index --assume-unchanged Changelog; git commit -a; git update-index --no-assume-unchanged Changeloggit rebase --autostashgit fetch origin pull/<id>/head:<branch-name>Alternatives:
git pull origin pull/<id>/head:<branch-name>git describe --tags --abbrev=0git diff --word-diffgit difftool [-t <tool>] <commit1> <commit2> <path>git update-index --assume-unchanged <file_name>git update-index --no-assume-unchanged <file_name>git clean -X -fgit checkout <deleting_commit> -- <file_path>git checkout <commit-ish> -- <file_path>git config --global pull.rebase trueAlternatives:
#git < 1.7.9
git config --global branch.autosetuprebase alwaysgit config --listgit config --global core.ignorecase falsegit config --global core.editor '$EDITOR'git config --global help.autocorrect 1git name-rev --name-only <SHA-1>git clean -fd --dry-rungit commit --fixup <SHA-1>git rebase -i --autosquashgit commit --only <file_path>git add -igit check-ignore *git status --ignoredgit log Branch1 ^Branch2git log -<n>Alternatives:
git log -n <n>git config --global rerere.enabled 1git diff --name-only | uniq | xargs $EDITORgit count-objects --human-readablegit gc --prune=now --aggressivegit instaweb [--local] [--httpd=<httpd>] [--port=<port>] [--browser=<browser>]git log --show-signaturegit config --global --unset <entry-name>git checkout --orphan <branch_name>git show <branch_name>:<file_name>git log --first-parentgit rebase --interactive HEAD~2git checkout master && git branch --no-mergedgit bisect start # Search start
git bisect bad # Set point to bad commit
git bisect good v2.6.13-rc2 # Set point to good commit|tag
git bisect bad # Say current state is bad
git bisect good # Say current state is good
git bisect reset # Finish search
git commit --no-verifygit log --follow -p -- <file_path>git clone -b <branch-name> --single-branch https://github.com/user/repo.gitgit checkout -b <branch-name>Alternatives:
git branch <branch-name> && git checkout <branch-name>git switch -c <branch-name>git config core.fileMode falsegit config --global color.ui falsegit config --global <specific command e.g branch, diff> <true, false or always>git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/git grep --heading --line-number 'foo bar'git clone https://github.com/user/repo.git --depth 1git log --all --grep='<given-text>'git log --oneline master..<branch-name> | tail -1Alternatives:
git log --reverse master..<branch-name> | head -6git reset HEAD <file-name>git push -f <remote-name> <branch-name>git remote add <remote-nickname> <remote-url>git remote -vgit blame <file-name>git shortloggit push --force-with-lease <remote-name> <branch-name>git log --author='_Your_Name_Here_' --pretty=tformat: --numstat | gawk '{ add += <!-- @doxie.inject start -->; subs += <!-- @doxie.inject end -->; loc += <!-- @doxie.inject start --> - <!-- @doxie.inject end --> } END { printf "added lines: %s removed lines: %s total lines: %s
", add, subs, loc }' -Alternatives:
git log --author='_Your_Name_Here_' --pretty=tformat: --numstat | awk '{ add += <!-- @doxie.inject start -->; subs += <!-- @doxie.inject end -->; loc += <!-- @doxie.inject start --> - <!-- @doxie.inject end --> } END { printf "added lines: %s, removed lines: %s, total lines: %s
", add, subs, loc }' - # on Mac OSXgit revert -m 1 <commit-ish>git rev-list --count <branch-name>git config --global alias.undo '!f() { git reset --hard $(git rev-parse --abbrev-ref HEAD)@{${1-1}}; }; f'git notes add -m 'Note on the previous commit....'git log --show-notes='*'git --git-dir=<source-dir>/.git format-patch -k -1 --stdout <SHA1> | git am -3 -kgit fetch origin master:refs/remotes/origin/mymastergit merge-base <branch-name> <other-branch-name>git log --branches --not --remotesAlternatives:
git log @{u}..git cherry -vgit diff --ignore-all-space | git apply --cachedgit config [--global] --editgit blame -L <start>,<end>git var -l | <variable>git format-patch -M upstream..topicgit rev-parse --show-toplevelgit log --since='FEB 1 2017' --until='FEB 14 2017'git log --perl-regexp --author='^((?!excluded-author-regex).*)
git request-pull v1.0 https://git.ko.xz/project master:for-linusgit ls-remote git://git.kernel.org/pub/scm/git/git.gitgit ls-files --others -i --exclude-standard | xargs zip untracked.zipgit config -l | grep alias | sed 's/^alias\.//g'Alternatives:
git config -l | grep alias | cut -d '.' -f 2git status --short --branchgit checkout master@{yesterday}git push origin HEADgit push -u origin <branch_name>git rebase --onto <new_base> <old_base>git config --global url.'git@github.com:'.insteadOf 'https://github.com/'cd <path-to-submodule>
git pull origin <branch>
cd <root-of-your-main-project>
git add <path-to-submodule>
git commit -m "submodule updated"git config --global core.autocrlf false