https://github.com/arjunu/git-cheatsheet
https://github.com/arjunu/git-cheatsheet
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/arjunu/git-cheatsheet
- Owner: arjunu
- Created: 2016-04-19T08:55:21.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2019-09-16T09:44:43.000Z (almost 7 years ago)
- Last Synced: 2025-03-13T21:18:20.997Z (over 1 year ago)
- Size: 8.79 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Git Cheatsheet
? - indicates optional arguments
Dangerous commands are marked with :warning:
## Configure
- Set username
`$ git config --global user.name ""`
- Set email
`$ git config --global user.email ""`
:information_source: Drop `--global` option to set local (repo specific) config.
## Create/Clone
- Create new local repo
`$ git init `
- Clone existing
`$ git clone `
- Create from existing code
`$ git init`
Add files & commit
`$ git remote add origin `
`$ git push origin master`
## Local Changes
- Add all changes for next commit
`$ git add .`
- List all new & modified files to be committed
`$ git status`
- Show unstaged changes
`$ git diff`
- Commit
`$ git commit -m ""`
## Update
- Fetch
`$ git fetch`
- Pull (fetch & merge)
`$ git pull`
- Push
`$ git push `
## Branches
- Get current branch name
`$ git branch | grep \* | cut -d ' ' -f2`
- List all existing branches
`$ git branch -av`
- Create new branch
`$ git branch `
- Switch to branch
`$ git checkout `
- Carry changes to a new branch
`$ git checkout -b `
- Merge specified branch into current branch
`$ git merge `
- Delete branch
`$ git branch -d `
- Delete unmerged branch
`$ git branch -D ` :warning:
- Rename branch
`$ git branch -m ` # Rename current branch
`$ git branch -m ` # Rename another branch
- Rename remote branch (after above step) [2]
`$ git push origin :old_branch` # Delete the old branch
`$ git push --set-upstream origin new_branch` # Push the new branch, set local branch
- Reset branch w.r.t. master
`$ git reset $(git merge-base master )`
## Remotes
- Show all remotes
`$ git remote -v`
- Add remote
`$ git remote add `
- Show remote information
`$ git remote show `
- Rename remote shortname
`$ git remote rename `
## Removing Files
- Delete file from working directory and stage the deletion
`$ git rm `
- Remove file from version control but preserve it locally
`$ git rm --cached `
## History
- Show all commits in reverse chronological order
`$ git log`
## Undoing
- Remove uncommitted changes
`$ git checkout .` :warning: # use -f for force
- Change last commit
`$ git commit --amend`
- Undo last local commit [1]
`$ git reset --soft HEAD~` # soft -- keep your changes
`$ git reset --hard HEAD^` # hard -- discard changes
- Undo last published commit [1]
`$ git revert HEAD`
- Unstage a file
`$ git reset HEAD `
- Unmodify a file
`$ git checkout -- ` :warning:
- Remove untracked files
`$ git clean -f` :warning:
- Remove untracked directories
`$ git clean -d` :warning:
- Reset branch to remote
`$ git reset --hard origin/`
## Tags
- Create tag
`$ git tag -a -m ""`
- Push all tags
`$ git push origin --tags`
## Cleanup
- Delete all local branches that no longer have a remote :warning:
`$ git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d`
## Glossary
#### Files
- **Commited:** data is safely stored in your local database
- **Modified:** you have changed the file but have not committed it to your database yet
- **Staged:** you have marked a modified file in its current version to go into your next commit snapshot
- **Tracked:** are files that were in the last snapshot; they can be unmodified, modified, or staged
- **Untracked:** files are everything else – any files in your working directory that were not in your last snapshot and are not in your staging area
#### Repository
- **Remote:** Remote repositories are versions of your project that are hosted on the Internet or network somewhere
- **Origin:** Default name Git gives to the server you cloned from
#### Branches
- **master:** name given to default branch.
- **HEAD:** pointer to current branch.
- **fast-forward:** is a type of merge. When you try to merge one commit with a commit that can be reached by following the first commit’s history, Git simplifies things by moving the pointer forward because there is no divergent work to merge together.
- **Remote tracking branches:** are references to the state of remote branches. They act as bookmarks to remind you where the branches in your remote repositories were the last time you connected to them.
- **Rebase:** another type of integrating changes (the other one being `merge`) from one branch into another by taking all the changes that were committed on one branch and replaying them on another.
## Sources
- [1]: Undo a commit and redo http://stackoverflow.com/a/927386/2251156
- [2]: Updating your local branch's tracking reference to the new remote http://stackoverflow.com/a/16220970/2251156