Commands
git checkout -b __newbranch__
: get a new branch and switch to itgit checkout -b __branchname__ / __branchname__
: get a local copy of a remote existing branchgit checkout __branchname__
: switch branchgit checkout --orphan __branchname__
: create branch without full historygit log -n X -p
: show X last commits with diffsgit log __branchA__ ^__branchB__
: commits in branch A that aren't in branch Bgit log --pretty=oneline --stat --all __foldername__
: modified files under a given foldergit fetch
git pull origin __branchname__
git pull --rebase
: Avoid having a merge commit if you had changes and origin had commits too (rewrites your local history)git merge --no-ff __sourcebranch__
: merge into current branch source one without rebase/FFgit push origin __destinationbranch__
: push merges/changesets to a branchgit push origin __destinationbranch__ --force-with-lease
: push merges/changesets to a branch. If remote had different commits not in local, fails and does not push. Safer than--force
which would override remote commits.git remote show origin
: display the path of the repositorygit remote set-url origin xxx
: Change remote URI to xxxgit remote rm origin
: Remove remote URIgit remote add origin git@github.com:<path>.git
: replace the origin remote URI. Need to firstgit remote rm origin
to remove the previous one.git git remote add <user> git@github.com:<user>/<path>
: add<user>
fork to a repository as a remote. When pushing changes afterwards to that remote, Github will propose you to create a pull request directly from the fork remote towards the original remote.git add xxx
: add files (use . for everything, folder/.. for folder recursive children)git commit
: commit changesgit status
: show status of uncommited filesgit checkout __file__
: revert a filegit checkout __branchname__ __file__
: Checkout all changes to file from branch branchname into currentgit checkout __revision__ .
: revert a full branch to specified revision if not commitedgit revert __commit1__ __commit2__ ...
: Reverts certain commits if commitedgit clean -f
: remove all local uncommited modificationsgit branch
: display local branches, active one is with a *git diff
: Show changes in filesgit diff __branch__ origin/__remotebranch__
: show a diff between a local branch and a remote onegit rebase __branchname__
: rebases current branch with specified branch (fetches remote branch changes and then adds yours at the tip)git rebase -i __branchname__
: rebases currrent branch with specified branch, allowing you to reorder changesgit rm __filename__
: delete a file from branch and filesystemgit branch -d __branchname__
: delete a local branchgit push origin --delete __branchname__
: delete a remote brachgitk __filename__
: show visual git loggit reset __revision__ .
: revert a full branch to specified revision if commitedgit reset --soft HEAD~1
: reset to last commit (even if pushed). Can re-commit stuff but if already pushed will need to push with--force
.git reset --hard HEAD^
: forcibly resets to the branch's head, discarding any commit.git reset --soft <new-root-sha1> && git commit --amend -m "<new message>" && git push --force
: squash all branch pushed commits previous to the one specified into a single commit with the desired new message.git log origin/__branchname__..__branchname__
: Show diff between local commits and remote commitsgit config --list
: List currently setup config valuesgit config --global user.name "Kartones"
: Setup global user namegit config --global user.email "d...@....net"
: Setup global user emailgit config --global credential.helper 'cache --timeout=28800'
: Make git cache credentials for 8 hoursgit config --global color.ui true
: Activate colors in diffs, etc.git config --global core.autocrlf true
: Fix Convert newlines to Unix-style ones (Windows)git config --global core.autocrlf input
: Fix Convert newlines to Unix-style ones (Unix)*git config --global core.excludesfile ~/.gitignore
: Instruct git to always ignore patterns defined at~/.gitignore
git config --global pager.log 'diff-highlight | less'
: Better diff highlighting (same for 3 following options)git config --global pager.show 'diff-highlight | less'
git config --global pager.diff 'diff-highlight | less'
git config --global interactive.diffFilter diff-highlight
git submodule update --init --recursive
: Init and update all submodulesgit submodule init && git submodule update
: Retrieve and update all submodules (alt)- Switch from current branch having a submodule to a branch without it:
rm -Rf __submoduledir__
git reset && git checkout .
git checkout __branchname__
git pull https://github.com/__username__/__reponame__.git __branchname__
: Merge a pull request to local branchgit stash
: Stash current changesgit stash apply
: Unstash and merge stored changesgit checkout --theirs xxxx
git checkout --ours xxxx
: Keep changes from incoming branch or local one, respectively.git blame -M
: Blames original commit, not the move commitgit blame -CCC
: Looks at all commits in historygit cherry-pick __commit__
: merges and commits a specific commit to current branchgit reflog
+git reset HEAD@__commit__
: show all changes on all branches and revert to a specific onegit commit --amend
: Squash a change on previous commit and change the commit messagegit diff --staged
: Show both staged and unstaged changes that you will commit- Undo a commit removing it from history:
git reset --hard HEAD~1
# your new commit here
git push origin <branch> --force
- Tag any commit of a repo (e.g. with a certain version):
git tag <label> <commit-id>
git push origin <label>
- Syncing a fork (first configure remote upstream)
- Apply a
.diff
file:git apply <filename>.diff
- Push to a different remote branch:
git push origin <localb-ranch>:<remote-branch>
- Rename a branch:
git branch -m <old-name> <new-name>
git commit --no-verify ...
: With great power comes great responsibility. This flag disables all commit hooks, so use it only when really in need.
Searches
- Issues in which you're mentioned
- Pull requests in which you're mentioned
- All open issues and pull requests of organization
TEST
: Change to proper organization name, and for example can filter to all assigned to you
Third-party Tools
- tig: to navigate commits & branches
- Github:
- hub: CLI tool to better interact with Github from the command-line
- Commands for automatically closing tickets when merged to default branch
- Search to display all issues/PRs of an organization:
https://github.com/issues?utf8=%E2%9C%93&q=is%3Aopen+org%3Athemotion+sort%3Aupdated-desc
- Additionally, filter to things assigned to me or involving me:
https://github.com/issues?utf8=%E2%9C%93&q=is%3Aopen+org%3Athemotion+sort%3Aupdated-desc+involves%3Akartones
Tutorials
- Atlassian Git tutorial
- 9 useful tricks of git branch
- Git Cookbook
- Most Common Git tips & tricks (awesome list)
Misc
source: https://xkcd.com/1597/
Added some nice commands seen at Oh shit, git!