Yao Lirong's Blog

Introduction to Git Command

2019/02/10

Creating repository

  • git init: create a repository
  • git add File_Name: add “File_Name” to repository
  • git add . : add all files
  • git commit -m "message": commit changes and tell others what changes have been made
  • git commit -m "Title" -m "Description ..": commit with a short title then long description

Way-back Machine

  • git status: tell you which files have been changed
  • git diff: check what content exactly has been changed in each file

Time Travelling

  • commitID: git uses commit ID, a hex number calculated by SHA1 to record your commit history

  • HEAD: HEAD is the current version, HEAD^ is the previous, HEAD^^ is the one before the previous, HEAD~100 is the last 100.

  • git log: check the commit history

  • git reflog: check the command history

  • git reset --hard CommitID : Going back to the “Commit ID” version

    (e.g. git reset --hard HEAD^ : going back to the previous version)

  • When you go back, the “future version” will no longer appear in “git log“. However, you can use “git reflog“ to trace “commit ID” from the future

Working Directory and Repository

Undo Changes

  1. messed up with working directory: use git checkout -- File_Name to discard changes in working directory and make “File_Name” to go back to the latest “committed” or “added” version
  2. messed up with working directory but don’t want to delete all the changes: git stash saves your local modifications away and reverts the working directory to match the HEAD commit.
  3. messed up with working directory and added it to stage: use git reset HEAD File_Name to discard changes in stage but keep “File_Name” in working directory changed, therefore going back to situation 1
  4. committed the mess to master branch: use the Time Traveling technique in the previous section

Deleting Files

If you want to delete files that are already committed to the master branch:

  1. delete the file in working directory: rm File_Name
  2. delete the file from git / restore the file
    1. delete the file from git: git rm File_Name & git commit
    2. restore the file: git checkout -- File_Name

Remote Repository

Change Git Remote URL

1
2
git remote set-url <remote_name> <remote_url> # remote name is usually "origin"
git remote -v # verbose print info to check changed successfully

Change Remote Branch a Local Branch is Tracking

1
2
3
4
git branch <local_branch_name> --set-upstream-to <remote_name>/<remote_branch_name>
git branch yao --set-upstream-to=origin/yao
# or you can ignore local branch name if you're currently on that branch
git branch --set-upstream-to=origin/yao

Push to Different Remote Branch

1
2
git push <remote_name> <branch_name>
git push origin master

Managing Branch

Creating and Deleting Branch

  • 查看分支:git branch
  • 创建分支:git branch <name>
  • 切换分支:git checkout <name>
  • 合并某分支到当前分支:git merge <name>
  • 删除分支:git branch -d <name>

Configuration

  • show configuration: git config -l --local git config -l --global

  • modify configuration:

    • For this current repo:

      1
      2
      git config user.name "Your Name Here"
      git config user.email your@email.com
    • For global settings:

      1
      2
      git config --global user.name "Your Name Here"
      git config --global user.email your@email.com
  • Quick look up a specific attribute: git config user.name

  • If you are using HTTPS remote, it will ask for your username and password each time you push/pull from the remote. You can then cache your credentials with the following command for 7200 seconds (2 hour). If ignore the part in the quote, the credentials will be saved forever.

    1
    git config --global credential.helper 'cache --timeout 7200'

Others

  • Keep a local copy of files, don’t update with server change: git update-index --skip-worktree <path-name> from Stackoverflow

  • When use GitHub, use https to track remote branch. SSH works weird when you have multiple accounts on your local machine.

  • Revert an accidental pull request: the following are 2 equivalent ways

    1
    2
    git reset --merge
    git merge --abort
CATALOG
  1. 1. Creating repository
  2. 2. Way-back Machine
    1. 2.1. Time Travelling
    2. 2.2. Undo Changes
    3. 2.3. Deleting Files
  3. 3. Remote Repository
    1. 3.1. Change Git Remote URL
  4. 4. Change Remote Branch a Local Branch is Tracking
    1. 4.1. Push to Different Remote Branch
  5. 5. Managing Branch
    1. 5.1. Creating and Deleting Branch
  6. 6. Configuration
  7. 7. Others