zhuang@linux:~/notes/git/git-cheat-sheet/$ cat
Git Cheat Sheet
This is a git cheat sheet.
Getting Help
bash
git help <verb>Config
bash
# view all of your settings
git config --list
# set config field
git config --global user.name "John Doe"
git config --global user.email [email protected]
git config --global core.editor vim
git config --global init.defaultBranch main
# get config field
git config user.name
# set alias
git config --global alias.st statusRemoving Files
bash
# remove from tracked files and working directory
git rm PROJECTS.md
# keep the file in working tree but remove from staging area
git rm --cached READMEViewing the Commit History
bash
git logUndoing Things
bash
# amend your last commit
git commit --amend
# unstage file
git restore --staged file
# unmodify file
git restore fileWorking with Remotes
bash
# lists the shortnames of each remote handle you’ve specified
git remote
# show the URLs that Git has stored for the shortname to be used when reading and writing to that remote
git remote -v
# add remote repo
git remote add origin url
# get data from remote repo
git fetch origin
# push your master branch to origin
git push origin master
# see more information about a particular remote
git remote show origin
# rename remote
git remote rename pb paul
# remove remote
git remote remove paulTagging
bash
git tag -l "v1.8.5*"
# create an annotated tag
git tag -a v1.4 -m "my version 1.4"
# see the tag data along with the commit that was tagged
git show v1.4
# create lightweight tags
git tag v1.4
# tag specified commit
git tag -a v1.2 9fceb02
# push tags to remote
git push origin v1.5
# push all tags to remote
git push origin --tags
# delete tags
git tag -d v1.4
# remove tag from remote
git push origin :refs/tags/v1.4-lw
git push origin --delete v1.4-lwBranching
bash
# create testing branch
git branch testing
# switch branch to testing
git checkout testing
# create new branch and switch to it
git checkout -b testing
# from git version v2.23 can use git switch instead of git checkout
# Switch to an existing branch
git switch testing-branch
# Create a new branch and switch to it
git switch -c new-branch
# Return to your previously checked out branch
git switch -
# show specified branch commit history
git log testing
# show all branches commit history
git log --all
# delete branch hotfix
git branch -d hotfix
# merge hotfix to current branch
git merge hotfix
# see which branches are already merged into the branch you’re on
git branch --merged
# see all the branches that contain work you haven’t yet merged in
git branch --no-merged
# Rename the branch locally
git branch --move bad-branch-name corrected-branch-name
# let others see the corrected branch on the remote
git push --set-upstream origin corrected-branch-name
# delete remote branch
git push origin --delete bad-branch-name
# see what tracking branches you have set up
git fetch --all; git branch -vvc
#include <stdio.h>
int main()
{
int i = 10;
for(int j = 0; j < i; j++)
{
printf("j = %d\n", j);
}
return 0;
}
zhuang@linux:~/notes/git/git-cheat-sheet/$ comments