Git commands

Frequently used git commands

  1. Create a git repository

  2. Set the name and email of current git repo user
    Globally

    1
    2
    $ git config --global user.name <USER_NAME>
    $ git config --global user.email <EMAIL>

    Locally

    1
    2
    $ git config --local user.name <USER_NAME>
    $ git config --local user.email <EMAIL>
  3. To see the existing configurations, use command

    1
    $ git config --list

    To get a specific configuration, use command ‘git config ‘, e.g.

    1
    $ git config user.name
  4. To get help, use command like this, ‘git help ‘, e.g.

    1
    $ git help config
  5. Check logs.

    1
    2
    3
    4
    # Check last 4 git logs by printing each log in one line
    git log --oneline --graph -4
    # Check last one log, normally
    git log -1
  6. If you add a new file which hasn’t been tracked before in the repository,
    first you have to put this file into the so-called “staged area”, using command below,

    1
    git add <FILE_NAME>

    If you have already modified a file which has been tracked before in the repository,
    you also have to put file into the so-called “staged area”, using the command below (indeed same as the command above),

    1
    git add <FILE_NAME>

    After you have put the file (newly added or already be tracked in repo), you put file(s) into (local) repository using the command below,

    1
    git commit
  7. If you modify a file, you can tell the difference by the command below,

    1
    git diff

    If you have already put the file(s) into the staged area, you will see nothing by ‘git diff’.
    This time, you should use the command below to tell the differences,

    1
    2
    git diff --cached 
    git diff --staged --> git version >= 1.61

    Use a tool to check the differences of files,

    1
    2
    3
    4
    # full
    git difftool --tool=tkdiff
    # short
    git difftool -t <tool_name>
  8. To see all the differences in current branch and master branch, use command below to output those differences,

    1
    git format-patch -M master -o <OUTPUT_DIR>
  9. To patch the current branch with *.patch files, use command,

    1
    git am ~/<SOME_DIR>/*.patch
  10. To create a new branch from current master branch, use command,

    1
    git checkout -b <BRANCH_NAME> master
  11. To remove/delete a branch (git branch), use command

    1
    git branch -d <BRANCH_NAME>

    If there’s anything not fully merged, git will stop the deletion. Use force command to do it (if confirmed to delete),

    1
    git branch -D <BRANCH_NAME>
  12. One way to remove untracked files is,
    (1) To see all the untracked files,

    1
    git clean -n

    Note here the option “-n” is very important, it just shows files untracked, no deleting
    (2) Clean if all the files listed above are supposed to be removed,

    1
    git clean -f
  13. To remove unstaged files, use commands below,
    (1) For a specific file use:

    1
    git checkout <PATH_TO_FILE_TO_REVERT>

    (2) For all unstaged files use:

    1
    git checkout -- .
  14. Rename a branch

    1
    git branch -m <OLD_BRANCH_NAME> <NEW_BRANCH_NAME>
  15. Rebase a branch
    (1) Switch to a branch which need rebase

    1
    git checkout <DEV_BRANCH>

    (2) Rebase

    1
    git rebase master
  16. Show origin information

    1
    2
    3
    $ git remote

    $ git remote show origin
  17. Move a file

    1
    git mv <OLD_FILE> <NEW_FILE>

    By moving files with git, we notify git about two things
    (1) The hello.html file was deleted.
    (2) The lib/hello.html file was created.
    Both facts are staged immediately and ready for a commit. Git status command reports the file has been moved.

  18. Git aliases
    Equals to ‘git checkout’

    1
    $ git config --global alias.co checkout

    Equals to ‘git branch’

    1
    $ git config --global alias.br branch

    Equals to ‘git commit’

    1
    $ git config --global alias.ci commit

    Equals to ‘git status’

    1
    $ git config --global alias.st status
  19. Create a new branch in local and then push it to the remote (GitHub) and then track it

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    # 1. Create a new branch in local
    git branch <NEW_BRANCH_NAME>
    # 2. Get the remote repo name
    git remote
    # 3. Push the new branch to remote repo
    git push <REMOTE_REPO_NAME> <NEW_BRANCH_NAME>
    # Usually remote repo name is 'origin'
    git push origin <NEW_BRANCH_NAME>
    # 4. Connect this new local branch with the new branch just pushed to the remote repo
    git branch --set-upstream-to=origin/<NEW_BRANCH_NAME>
    # 5. After that, modify code and push as usaul
    git add -A
    git commit -m "XXXXX"
    git push (or use git push origin <NEW_BRANCH_NAME>)
  20. Remove files and restore files

    Reference website git 删除文件与恢复

    • A file was deleted locally by shell commands other than git commands, but it was not added to the stage area, use checkout option to restore

      1
      2
      3
      4
      # Remove a file locally by shell commands
      rm <FILE>
      # Restore the file
      git checkout -- <FILE>
    • A file was deleted locally by shell commands other than git commands, and it was added to the stage area, first use reset command to rollback the file to the status of locally removed, and then use checkout option to restore

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      ##### 1st situation
      # Remove a file locally by shell commands
      rm <FILE>
      # Add to staged area (deleted)
      git add <FILE>
      # Rollback
      git reset HEAD <FILE>
      # Restore the file
      git checkout -- <FILE>

      ##### 2nd situation
      # Remove a file locally by git command which will add it to staged area (deleted)
      git rm <FILE>
      # Rollback
      git reset HEAD <FILE>
      # Restore the file
      git checkout -- <FILE>
    • A file was deleted locally either by shell commands or git commands, and it was not only added to the stage area, but also committed to local repository, then we need to use reset --hard <ID> to rollback (ID is got by using git log command)

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      ##### 1st situation
      # Remove a file locally by shell commands
      rm <FILE>
      # Add to staged area (deleted)
      git add <FILE>
      # Commit
      git commit -m "Delete a file"
      # Get the version ID by using the log option
      git log --pretty=oneline
      # Rollback
      git reset --hard <ID>

      ##### 2nd situation
      # Remove a file locally by git command which will add it to staged area (deleted)
      git rm <FILE>
      # Commit
      git commit -m "Delete a file"
      # Get the version ID by using the log option
      git log --pretty=oneline
      # Rollback
      git reset --hard <ID>

    • A file was deleted locally either by shell commands or git commands, and it was not only committed to the local repository, but also push to the remote repository (e.g., Github), then following the steps above, and the use git push -fto push the restored files back to the remote repository. Note here -f is a must, because git doesn’t allow lower versions override higher versions.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      ##### 1st situation
      # Remove a file locally by shell commands
      rm <FILE>
      # Add to staged area (deleted)
      git add <FILE>
      # Commit
      git commit -m "Delete a file"
      # Get the version ID by using the log option
      git log --pretty=oneline
      # Rollback
      git reset --hard <ID>
      # Push to remote
      git push -f

      ##### 2nd situation
      # Remove a file locally by git command which will add it to staged area (deleted)
      git rm <FILE>
      # Commit
      git commit -m "Delete a file"
      # Get the version ID by using the log option
      git log --pretty=oneline
      # Rollback
      git reset --hard <ID>
      # Push to remote
      git push -f
Author

Pyrad Selong

Posted on

2022-04-22

Updated on

2022-05-16

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.