git config -l
# setup user info globally
git config --global user.name "username"
git config --global user.email "username@email.com"
# setup user info locally (specific git repository)
git config user.name "username"
git config user.email "username@email.com"
# edit global git configuration
git config --global --edit
# set default branch name of git initialization
git config --global init.defaultBranch <name>
git config --global credential.helper cache
git init
git add file_name # add a file
git add * # add all files
git add fil* # add all files starting with 'fil'
git status
git commit # commit in editor
git commit -m "your message" # commit with message
git commit -am "your message" # add & commit tracked files
git log # see commit history
git log -p # see commit history including changes
git log --stat # see commit history including line(s) changed and file names
git log --graph --oneline # show log graph of a branch
git log --graph --oneline --all # show log graph of all branches
git diff # all unstaged changes
git diff file_name # unstaged changes of a specific file
git diff --staged
git rm filename
git rm --cached <file>
git rm -r --cached <folder> # for directory
git mv old_file new_file
git checkout filename # revert unstaged changes from specified file
git checkout -- . # revert unstaged changes from all files
git reset HEAD filename
git reset HEAD~2 # revert the last two commits
git reset --hard HEAD~1 # revert the last commit and discard the changes
git reset HEAD -p # specify the changes you want to reset
# should avoid amending pushed commits
git commit --amend # modify and add changes to the most recent commit
git commit --amend -m "your message"
git reset HEAD~
or
git reset HEAD~1
git revert commit_id # create a new commit to revert the old changes
git branch branch_name
git checkout -b branch_name # create a new brach & switch to new created branch
git checkout branch_name
git branch
git branch -M <name>
Delete a local branch:
git branch -d <branch_name>
Delete a remote branch:
git push <remote_name> -d <branch_name> # remote_name is normally origin
- switch with creating new branch
git switch -c "new_branch_name"
- switch without creating new branch
git stash save
git checkout branch
git stash pop
git merge branch_name # merge active branch with branch_name
git merge --abort # abort a conflicting merge
git add remote remote_repo_url
git remote -v
git remote show origin # get more info about remote repo
git push -u origin branch_name # push a new branch to a remote repo
git push # push changes to a remote repo
git push -f # force a push request
git fetch # download the latest changes from a remote repo
git pull # download the latest changes from a remote repo and merge them with local branch
git pull -r # download the latest changes from a remote repo and rebase them with local branch
git branch -r # check remote branches
git pull -r
# Resolve conflicts which changes you want to add
git add .
git rebase --continue
git rebase branch_name