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>
List all configurations:
git config --list
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 restore <file>
Discard all files changes:
git restore .
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' changes:
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.
Stash let you create a snapshot of your changes and save it on a stack. All changes will then be reset to the last commit, and you can do something else safely:
git stash (save "<message>")
git stash (save "<message") -u # also stash untracked files
View all stashes:
git stash list
When you want to restore the stashed changes:
git stash pop # applies the most recent stash and removes it from the stash stack
git stash apply # applies the most recent stash but keeps it in the stash list
git stash apply stash@{n} # applies a specific stash by index
git stash drop stash@{n} # deletes a specific stash from the list
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 keygit 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>
View remote repository:
git remote -v
Remove remote repository:
git remote remove <name>
Add remote repository:
git remote add origin <your-repo-url>
Managing pull requests on GitHub's web interface is difficult: you either merge a pull request as a whole or reject it. A better workflow is to fetch it to a local branch, resolve possible conflicts, review/test it, and then merge it into the main branch and push. The following is an example of merging a PR:
Create a local branch from the main branch and fetch the PR branch into it:
P.S. This code can be copied from the command-line instructions button at the end of the PR page.
git checkout -b PrimeX-06-NJAU main
git pull git@github.com:PrimeX-06/college-beamer.git NJAU
If there are conflicts, add --no-ff to the previous command:
git pull git@github.com:PrimeX-06/college-beamer.git NJAU --no-ff
Run git status to see the conflicted files. Edit them to resolve the conflicts, use git diff --check to confirm all conflicts are resolved, and then stage and commit the changes:
vim <conflicted file>
git diff --check
git add -A
git status
git commit
P.S. Before git commit, all changes are shown as staged/unstaged changes, including the PR's changes and your edits. Review them carefully.
Run additional tests/agentic reviews. If there are further edits, stage and commit the changes again.
Merge the PR branch to the main branch and push it to the remote repository:
P.S. This code can be copied from the command-line instructions button at the end of the PR page.
git checkout main
git merge --no-ff PrimeX-06-NJAU
git push origin main