User name:
git config --global user.name "<Your Name>"
Email:
git config --global user.email "<email@example.com>"
Main branch name:
git config --global init.defaultBranch <name>
P.S. Change main branch name of an existed repository:
git branch -m <old name> <new name>
If it has been pushed to remote:
git push <remote> --delete <old name>
git push -u <remote> <new name>
git init
git add <file>
P.S. Use -f
to force stage a file even if it's set to be ignored in .gitignore
.
git restore --staged <file>
P.S. Unstage all files' cahnges:
git reset
git restore .
git restore <file>
P.S. The difference is git restore --staged <file>
affects the staging area by removing the file from it, but leaves the working directory unchanged. git restore <file>
affects the working directory by discarding changes in the specified file (to match the version in the staging area or the last committed state if the file is not staged), but leaves the staging area unchanged.
git rm --cached <file>
P.S. Without the --cached
flag, the file will not only be removed but also be deleted.
git commit -m "<commit>"
P.S. To write multiple lines of commit message, use -m
multiple times:
git commit -m "<line1>" -m "<line2>" -m "<line3>"
git reset --hard <commit>
P.S. If without --hard
, the changes will be kept in the working directory but the version rollback to the specified commit.
P.S. The current commit can be referred to as HEAD
. HEAD^
refers to the last commit, HEAD^^
refers to the second last commit, and so on.
View all branches:
git branch
Create new branch:
git branch <branch>
Switch branch:
git switch <branch>
Delete branch:
git branch -D <branch name>
git merge --no-ff -m "<commit>" <branch>
Resolving a merge conflict using the command line - GitHub Docs
Merge conflicts occur when competing changes are made to the same line of a file, or when one person edits a file and another person deletes the same file.
When you run git merge
and there are conflicts:
git add
) and commit them with a message like "resolve merge conflict"
.<<<<<<<
to locate the conflicts. It will be something like:If you have questions, please
<<<<<<< HEAD
open an issue
=======
ask your question in IRC.
>>>>>>> branch-a
git add -A
git commit -m "resolve merge conflict"
git add <file>
git rm <file>
rm <file>
git commit -m "resolve merge conflict"
ssh-keygen -t rsa -C "<email>"
~/.ssh/id_rsa.pub
. Noted that the private key ~/.ssh/id_rsa
should be kept secret.Settings
> SSH and GPG keys
> New SSH key
git clone <link>
P.S. Only clone the latest version of the main branch:
git clone --depth=1 <link>
P.S. When cloning a repository, the remote repository is automatically linked and named origin
. However, to link existing local repository to a remote repository:
git remote add <name> <repository link>
<name>
is usually origin
. To view all remote repository:
git remote -v
Push changes to remote repository:
git push (<remote> <branch>)
P.S. When push for the first time, use -u
to set the default remote repository and branch:
git push -u <remote> <branch>
P.S. When the local repository is roll backed, use -f
to force push:
git push -f
Pull changes to remote repository:
git pull
Fetch a new branch from remote repository:
git fetch <remote> <remote_branch>:<local_branch>