Collection of git command and practices.
git config --global user.name "Shawn"
git config --global user.email "shawn@cinnamon.is"
git config --global color.ui true
git config --list
# to create a repository
git init # will create a .git folder
git add <file|directory> # add to the staging area
git diff <file|commithash|> # to compare working directory with the HEAD or a specific commithash
git difftool # to use external tool (Vimdiff/Meld/BeyondCompare...)
git diff --staged # to compare the staged area against the HEAD
git checkout . # to reverse all files in current directory
git reset HEAD . # to move files back from staging area to the working directory
git reset --hard HEAD # = `git reset` + `git checkout`: files removed from the staging area and the working directory is taken back to the state of the last commit.
git revert # to undo the commits
git revert HEAD --no-edit
git commit -m "commit message"
echo '*.tmp' > .gitignore # add pattern to ignore in git
# to examize commits
git log # to see the infomation about the commit
git show [commit-hash] # to view the code change for a specic commit
# to view the remote url
git remote -v
# to change the remote url
git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
git remote add origin /s/remote-project/1 # add the remote location with name `origin`
# `git clone` automatically add the location as a remote with name `origin`
# to share the commits
git push
git push origin master # to push the commits in the master branch to the origin remote.
# to Pull the changes from the remote into your master branch.
git pull origin master
git mv
or git rm
for git to perform the action and include update the staging areagit log --grep="#1234"
to grep the log# Use pretty log format
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
# Too long? --> use alias --> from now on, use: `git lg`
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git branch <new branch name> <starting branch> # takes an existing branch and creates a separate branch to work in
git checkout <new branch name> # to switch to a branch
git checkout -b <new branch name> # create and checkout the newly created branch.
# to list all the branchs
git branch # -a: to include remote branches, -v: to include the HEAD commit message for the branch
# push a branch to a remote
git push <remote_name> <branch_name>
# to remove a branch
git branch -d <branch_name>