A cheat sheet on Git commands introduced by Corey Schafer's Git Tutorials and some awesome blog posts.
- Basic commands
- Common workflows
- Commit message format
- How to fix mistakes
- How to stash
- How to rebase
- Where to store .gitignore
- How to use git diff for notebooks
git --version
git config --global user.name "first_name last_name"
git config --global user.email "some_number_github_offers+my_github_id@users.noreply.github.com"
git config --listgit filter-branch --env-filter '
WRONG_EMAIL="wrong@example.com"
NEW_NAME="New Name Value"
NEW_EMAIL="correct@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$WRONG_EMAIL" ]
then
export GIT_COMMITTER_NAME="$NEW_NAME"
export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$WRONG_EMAIL" ]
then
export GIT_AUTHOR_NAME="$NEW_NAME"
export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi
' --tag-name-filter cat -- --branches --tagsResource: How can I change the author name / email of a commit? # Using git filter-branch
git commit --amend --author="first_name last_name <some_number_github_offers+my_github_id@users.noreply.github.com>"git config --global credential.helper osxkeychain"The next time you clone an HTTPS URL that requires authentication, Git will prompt you for your username and password. When Git prompts you for your password, enter your personal access token (PAT) instead. Once you've authenticated successfully, your credentials are stored in the macOS keychain and will be used every time."
Resource: Caching your GitHub credentials in Git
ls -la
git init
ls -la
git status
touch .gitignore...adding ignored files...
git add .gitignore
git statusgit status
git add my_file
git add -A // all except ignored ones
git statusgit status
git reset my_file
git reset // all files
git statusgit add -A
git commit -m "Initial Commit"
git status
git loggit clone ../remote_repo.git .
git clone https://github.com/my_github_id/remote_repo.git .git remote -v
git branch -agit diff
git status
git add -A
git commit -m "My new feature description"
git pull origin master
git push origin master- Create a branch for desired feature:
git branch calc_divide
git checkout calc_divide…writing code…
commit -m "Develop function calc_divide"- After commit, push branch to remote:
git push -u origin calc_divide
git branch -a- If other people also approved, merge branch:
git checkout master
git pull origin master
git branch --merged
git merge calc_divide
git branch --merged
git push origin master- Delete the merged branch:
git branch --merged
git branch -d calc_divide
git branch -a
git push origin --delete calc_divide- Configure an upstream for the local repo:
git remote -v
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
git remote -v- Sync the local repo with the upstream:
git fetch upstream
git checkout masterthen either
git merge upstream/masteror
git rebase upstream/master- Push master from the updated local repo to origin at GitHub:
git push origin masteror
git push -f origin master- title (imperative mood) <= 50 chars
- blank line
- body, each line <= 72 chars, blank line between paragraphs
- the reasons why you made the change in the first place
- the way things worked before the change
- what was wrong with that
- the way they work now
- why you decided to solve it the way you did
- Resolves: #IssueNumber
git commit --amend -m "New commit message"It changes the commit hash, so the git history changes too. It’s good when commits are not yet shared with people.
Otherwise, use git revert.
git add my_missed_file
git commit --amend
git log --statPress :wq to save the commit message prompt and exit it.
Let's grab the last commit from master to my-feature-branch:
git checkout master
git logCopy a beginning piece of the hash belonging to the last commit:
git checkout my-feature-branch
git cherry-pick piece_of_hashIt keeps our changes in the staging area.
git reset --soft piece_of_hashIt keeps our changes in the files, but it doesn't add them to staging area.
git reset piece_of_hashWe get rid of all the changes after the commit!
git reset --hard piece_of_hashIt reverses the effects of some earlier commits via a new undoing commit. That way, it adds to the commit history; it neither modifies nor deletes it. Thus, it's a safe way of undoing the commits that have already been shared.
git log
git revert piece_of_hash
git log
git diff piece_of_hash piece_of_new_commit_hash git checkout -- .or
git reset --hardIt deletes all untracked directories and files!
git clean -dfIt stores all commits even the lost ones for around 30 days in trash. It can be a life-saver!
git reflogIt shows commits in the order of when you last referenced them. Copy the hash of the commit that you wish to recover.
git checkout piece_of_hash
git log
git branch restoring-branch
git checkout restoring-branch
git log| Command | Scope | Common use cases |
|---|---|---|
git reset |
Commit-level | Discard commits in a private branch or throw away uncommitted changes |
git reset |
File-level | Unstage a file |
git checkout |
Commit-level | Switch between branches or inspect old snapshots |
git checkout |
File-level | Discard changes in the working directory |
git revert |
Commit-level | Undo commits in a public branch |
git revert |
File-level | (N/A) |
Resource: Git Reset vs Revert vs Checkout reference
It allows us to switch branch without committing them.
git stash save "Worked on the functionality blah"
git stash list
git checkout other-branchgit checkout bloh-branch
git stash listApply and then delete:
git stash apply stash@{some-id}
git stash drop stash@{some-id}Apply and delete simultaneously:
git stash popIt deletes current unsaved changes too!
git stash clearThe golden rule: rebase only your un-forked branches!
A comprehensive resource: Merging vs. Rebasing
It results in a perfectly linear history :)
git checkout feature
git rebase mastergit checkout feature
git rebase -i masterWhen the commit history on your remote repo is different from your rebased local one, you have to force-push your local changes to the remote—which irreversibly overwrites the remote history!
git checkout feature
# Be very careful with this command!
git push origin feature --forcegit rebase -i HEAD~3git merge-base feature master
git rebase -i piece_of_hashgit rebase -i --rootgit checkout shared-feature
git pull origin shared-feature --rebaseResource: How to Write a Git Commit Message
- Global (for cache files)
nano ~/.gitignore_global- Local (for additional files that are specific to the project and that should be ignored on other computers too)
nano ./.gitignore- Usual
git diff- Graphical
nbdiff-webResource: Version control integration