- 1. Create
- 2. Publish
- 3. Stash
- 4. Branch
- 5. Rebase
- 6. Revert
- 7. View Changes
- 8. Configuration
- 9. Security
- 10. Other Resources
- 11. Credit
git init
create a new repository in current directory while git add .
add all latest changes to the next commit.
cd ~/projects/project01
git init
git add .
git clone
clone a repositroy from a remote.
Note
Remote repositories can be on your local machine. It is entirely possible that you can be working with a “remote” repository that is, in fact, on the same host you are. The word “remote” does not necessarily imply that the repository is somewhere else on the network or Internet, only that it is elsewhere.
git clone ~/existing/repo ~new/repo
git clone you@host:dir/project.git # default protocol is ssh
# this doesn't merge them
git fetch
# this does a fetch followed by a merge
git pull
git am -3 patch.mbox
git am --resolve # in case of conflict, resolve the conflict
git commit -a
git commit -m "descriptive commit message"
git format-patch origin
git push <origin> <branch>
git tag <version_name>
# e.g git tag v7.0.34
git stash
git stash push -m
git stash pop
git checkout <branch>
git checkout <B2>
git merge <B1>
git branch <branch>
git checkout <new> <base>
git branch -d <branch>
git debase origin/master
Which would apply all the changes in master, below your branch, to make your commits grouped together and more organized.
git rebase -i HEAD~2
Which runs your debase in interactive mode for the most recent 2 commits (which is really helpful for squashing and renaming your commits), making it more readable.
For an easy revert if you just made a mistake (perhaps you forked a repo, then ended up pushing to the original instead of to a new one):
git reset --hard <commit_hash>
# e.g git reset --hard 71c27777543ccfcb0376dcdd8f6777df055ef479
Everything since then will be deleted once you push again. To do that, the next step would be:
git push --force
git checkout -F | git reset --hard
# you cannot easily undo a hard reset
git revert HEAD # creates a new commit
git revert $id # creates a new commit
git commit -a --amend
git status
git diff
git diff <commit_id1> <commit_id2>
git log
git log --graph --pretty=oneline --abbrev-commit --decorate # nice output
# same as 'git log' but show raw format diff output and skips merges
git whatchanged
git blame <file>
git show <ID>
# green asterisk "*" means the current branch
# cyan plus "+" means checked out branches
git branch
git grep <pattern> [path]
Set the author name and email to be used for all commits in current repo.
git config user.name "Your Name"
git config user.email "[email protected]"
Set the author name and email to be used for all commits by the current user globally.
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Create a shortcut or an alias for a Git command.
git config --global alias.<alias-name> <git-command>
# e.g git config --global alias.last 'log -1 HEAD --stat' # show last commit
Set the default text editor used by Git commands for all users on the machine.
# <editor> arg should be the command that launches the desired editor (e.g. vi).
git config --system core.editor <editor>
Open the global configuration file in a text editor for manual editing.
git config --global --edit
Tip
for password, it might be easier to change the it, if you can..
First, change the repository visibility to private
. This way, you're sure no one else sees the file while you're working on deleting it.
Then run the following command. You have to include the path to the file and not just the file name. In my case, secretFile.json
is inside of a folder named config
.
git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch config/secretFile.json" \
--prune-empty --tag-name-filter cat -- --all
Warning
The command above deletes the file from your local repository too, so ensure you have copied the content to a notepad or whatever you use before running the command.
Then create and add your file to .gitignore
so you don't accidentally push it again to GitHub. You can do that with:
echo "super-secrets-file" >> .gitignore
git add .gitignore
git commit -m 'add file to .gitignore'
Once you are satisfied with your changes, you can then push them to GitHub:
git push origin --force --all
And that's it! Your repo history is clean without any trace of your sensitive information.
origin is the default upstream repository
HEAD is the current branch
This cheatsheet was originally written by Beau Williams - I found it on his Dotfiles, probaly serving as a quick reminder on git commands.