Skip to content

yunji0387/GitCommands

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 

Repository files navigation

Git Commands

A list of Git commands I frequently used.


Example of uploading a local repository to remote server.

  1. Initialize a local repository:
    git init
  2. Change the branch name to match the branch name on GitHub or error will occur
    git branch -m master main
  3. 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
  4. Check does files staged correctly
    git status
  5. Commit Files
    git commit -m "<your message here>"
  6. Check if in the right branch
    git branch
  7. Connect to remote server (origin = remote name)
    git remote add origin <GitHub repository url>
  8. Check if connect to the correct remote server
    git remote -v
  9. 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
  10. Push to remote server (origin = remote name, main = local branch)
    git push origin main

Other useful commands

  • 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

    1. fetch latest changes from remote server
         git fetch
    2. switch to target branch you want to merge into (ex: main)
         git checkout main
    3. 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>"
    1. push to remote server (origin = remote server , main = local target branch)
         git push origin main
  • Review commit histories

     git log
    • Review n commit histories
       git log -n <count>
  • 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>
  • 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

    1. create a .gitignore in local repository directory and open it
     touch .gitignore
     vi .gitignore
    1. add files/directories to be ignored, and save it (esc -> :wq)
      • Image
    2. finally push .gitignore to remote repository
  • Undo an accidental deleted files/directories after git pull request

    1. Use git reflog to track all changes
     git reflog
    1. switch to the desired Head index ( n = desired index)
     git checkout HEAD@{n}
  • How to merge branch excluding certain files

    1. 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
    2. 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
    3. Remove these files from the working directory:
      git checkout -- test1.js test2.js package.json package-lock.json
    4. Commit and Finish the Merge:
      git commit -m "Merged Development into main, excluding tests and package files"
    5. Push Changes to the Remote Repository:
      git push origin main

About

A list of Git commands I frequently used.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published