A list of Git commands I frequently used.
- Initialize a local repository:
git init
- Change the branch name to match the branch name on GitHub or error will occur
git branch -m master main
- Staged files
git add .
- Staging specific files
git add <file1> <file 2> <file 3>
- Unstaging specific files
git reset <file1> <file 2> <file 3>
- Unstaging all files
git reset
- Staging specific files
- Check does files staged correctly
git status
- Commit Files
git commit -m "<your message here>"
- Check if in the right branch
git branch
- Connect to remote server (origin = remote name)
git remote add origin <GitHub repository url>
- Check if connect to the correct remote server
git remote -v
- Always pull before push (origin = remote name, main = local branch)
git pull origin main
- If the files on remote are incosistent with local repository (Example: there is a README.md on remote server that local repository does not have)
git pull origin main --allow-unrelated-histories
- If the files on remote are incosistent with local repository (Example: there is a README.md on remote server that local repository does not have)
- Push to remote server (origin = remote name, main = local branch)
git push origin main
-
Clone a repository from remote server
git clone <GitHub repository url>
-
Create new branch
git branch <Branch name>
-
Switch new branch
git checkout <Branch name>
-
Push a new branch that is not yet exists in the remote server (origin = remote name)
git push -u origin <branch-name>
-
Merge branch
- fetch latest changes from remote server
git fetch
- switch to target branch you want to merge into (ex: main)
git checkout main
- merge you source branch(ex: Development) into the target branch
git merge Development
- if merge conflicts occurs, you have to resolve the conflicts first, then stage and commit all the files (Development = source branch , main = target branch)
git add . git commit -m "Merge <source-branch> into <target-branch>"
- push to remote server (origin = remote server , main = local target branch)
git push origin main
- fetch latest changes from remote server
-
Review commit histories
git log
- Review n commit histories
git log -n <count>
- Review n commit histories
-
Undo(Revert to) last commit
- First, undo the last commit using the command line:
git reset --hard HEAD~1
- then, force push the branch to the remote repository(make sure to have the '+' before branch name):
git push origin +<BranchName>
- First, undo the last commit using the command line:
-
Review file differences between local directory and staged area
git diff <File name>
-
Review file differences between staged area and remote server
git diff --staged <File name>
-
Ignore certain files/directories in local repository to be push to remote repository
- create a .gitignore in local repository directory and open it
touch .gitignore
vi .gitignore
-
Undo an accidental deleted files/directories after git pull request
- Use git reflog to track all changes
git reflog
- switch to the desired Head index ( n = desired index)
git checkout HEAD@{n}
-
How to merge branch excluding certain files
- For example if I want to merge Development in main branch, first Merge Development into main Without Committing:
git merge Development --no-commit --no-ff
- Reset the Files You Don't Want to Include:
- If you want to exclude test1.js, test2.js, package.json, and package-lock.json:
git reset HEAD test1.js test2.js package.json package-lock.json
- Remove these files from the working directory:
git checkout -- test1.js test2.js package.json package-lock.json
- Commit and Finish the Merge:
git commit -m "Merged Development into main, excluding tests and package files"
- Push Changes to the Remote Repository:
git push origin main
- For example if I want to merge Development in main branch, first Merge Development into main Without Committing: