A Cup of Git Latte

Quick reference for some frequently used Git commands.

Set up a new repository

git init
git pull https://github.com/registercosmo/Cheatsheet.git
git remote add origin https://github.com/registercosmo/Cheatsheet.git
git add .
git status
git commit
git commit -m "comments"
git push -u origin master

Sync with the remote repository

# To pull changes to workspace (= fetch + merge)
git pull origin master

# To fetch lastest changes from other developers to local repo
git fetch 

# To merge the local repo and the workspace
git merge origin master

Fix the previous commit

git commit --amend

Undo changes

# unstage changes but keep the local changes in the working space
git reset HEAD

# discard all the changes in the stage and working space
# and match the working space the commit B
git reset --hard B

# move the HEAD to commit B, no modification will be made
# to the stage and the working space
git reset --soft B

Untrack files

git rm --cached 

Set up remote repository

# set up a new remote repo (origin)
git remote add origin 

# change URL for a remote repo (origin)
git remote set-url origin 

# list all the remote repositories URLs
git remote -v

About configs

# configs for all repositories
git config --global user.name # check current global user.name
git config --global user.name  # set global user.name
git config --global user.email # check current global user.email
git config --gloabl [email protected] # set global user.name

# configs for the current local repository
git config user.name # check current local user.name
git config user.name  # set local user.name
git config user.email # check current local user.email
git config [email protected] # set local user.name

# list all the configs
git config --list

# Remove the configs
git config --global --unset-all user.name # global

# Change the configs
git config --global --replace-all user.name  # global

About branches

# fetch remote branches from Github
git remote add origin  # we have to set origin first
git pull # it will pull(fetch + merge) all available branches from repositories

# check info for all available branches
git branch

# create a new branch
git branch  

# switch to a specific branch
git checkout  

# create and switch to a new branch
git checkout -b 

# check the current branch
git status

# rename a branch
git branch -m  

# merge a specific branch to the current branch
git merge 

# delete a branch locally
git branch -d 

# delete a branch remotely
git push origin :

About diff

# inspect all the modifications between workspace and stage
git diff

# inspect all the modifications between stage and local repo
git diff --cached

About resolving conflicts

git add 

Advanced topics

# get the SHA-1 key for a specific contents
echo 'test content' | git hash-object -w --stdin

# get the content of a SHA-1 key
git cat-file -p 

# get a object type for a SHA-1 key
git cat-file -t 

你可能感兴趣的:(A Cup of Git Latte)