Skip to content

Latest commit

 

History

History
358 lines (241 loc) · 7.28 KB

git-cheatsheet.md

File metadata and controls

358 lines (241 loc) · 7.28 KB

Git Cheatsheet


Table of Content

CREATE

New repository

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 .

From existing repository

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

UPDATE

Fetch latest changes from origin

# this doesn't merge them
git fetch

Pull latest changes from origin

# this does a fetch followed by a merge
git pull

Apply a patch that someone sent you

git am -3 patch.mbox
git am --resolve # in case of conflict, resolve the conflict

PUBLISH

Commit all local changes

git commit -a

Commit previously staged changes

git commit -m "descriptive commit message"

Prepare a patch for other developers

git format-patch origin

Push changes to origin

git push <origin> <branch>

Make a version or a milestone

git tag <version_name>
# e.g git tag v7.0.34

STASH

Stash uncommitted changes before moving to another branch

git stash

Give the stash a name for easy reference

git stash push -m

Return local copy of branch back to how it was when you stashed it earlier

git stash pop

BRANCH

Switch to the BRANCH branch

git checkout <branch>

Merge branch B1 into branch B2

git checkout <B2>
git merge <B1>

Create branch based on HEAD

git branch <branch>

Create branch based on another

git checkout <new> <base>

Delete a branch

git branch -d <branch>

REBASE

Rebase with git

git debase origin/master

Which would apply all the changes in master, below your branch, to make your commits grouped together and more organized.

Interactive rebase

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.

REVERT

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

Return to the last committed state

git checkout -F | git reset --hard
# you cannot easily undo a hard reset

Revert the last commit

git revert HEAD # creates a new commit

Revert specific commit

git revert $id # creates a new commit

Fix the last commit

git commit -a --amend

CHANGES

View the changes in the working directory

git status

View the changes between the working directory and the INDEX(staging area)

git diff

View the changes between two arbitrary commit

git diff <commit_id1> <commit_id2>

View the commits history(logs)

git log
git log  --graph --pretty=oneline --abbrev-commit --decorate # nice output

View the commits history in a specific file

# same as 'git log' but show raw format diff output and skips merges
git whatchanged

View who changed what and when in a file

git blame <file>

View a commit identified by ID

git show <ID>

View all local branches

# green asterisk "*" means the current branch
# cyan plus "+" means checked out branches
git branch

Search for patterns

git grep <pattern> [path]

CONFIGURATION

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

SECURITY

Sign Commits with SSH key

Remove sensitive/personal information from GitHub in your commit history e.g passwords or secrets

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.

FYI

origin is the default upstream repository
HEAD is the current branch

Other Resources

Credit

This cheatsheet was originally written by Beau Williams - I found it on his Dotfiles, probaly serving as a quick reminder on git commands.