Common Git Commands

  1. Working with local repositories
    1. First-time setup
    2. Git Aliases
    3. Delete a Local Branch
    4. Modify the username and email in history
    5. Go back to a previous commit
    6. Show all changes in a specific commit
    7. Show all the configuration information and files
    8. git patch
    9. Filter the log by time range and a specific author
    10. configure Git to use a proxy
  2. Working with remote repositories
    1. Set upstream repository
    2. Create a local branch based on a remote master branch
    3. Link your local branch with a remote branch
    4. rebase
    5. Modify the author information in a branch
    6. List remote branches
    7. Push a local branch to a remote server

Working with local repositories

First-time setup

1
2
$ git config --global user.name "John Doe"
$ git config --global user.email [email protected]

Git Aliases

1
2
3
4
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status

Delete a Local Branch

1
$ git branch -D <branch-name>

-D is a shortcut for --delete --force.

Modify the username and email in history

1
git commit --amend --reset-author

Go back to a previous commit

  • Create a new commit, and the content will go back to <commit-hash>.

    1
    2
    git revert --no-commit <commit-hash>..HEAD
    git commit
  • Remove all changes haven’t committed, and go back to <commit-hash>.

    1
    git reset --hard <commit-hash>
  • Discard the latest commit and its changes:

    1
    git reset --hard HEAD~1

Show all changes in a specific commit

1
git show <commit-hash>

Show all the configuration information and files

1
git config --list --show-origin

git patch

1
2
git diff > my_custom_patch_file.patch
git apply patch_file.patch

Filter the log by time range and a specific author

1
git log --pretty=format:"%ad - %an: %s" --after="2016-01-31" --until="2017-03-10" --author="John Doe"

configure Git to use a proxy

Setting a global proxy:

1
git config --global http.proxy http://proxyUsername:[email protected]:port

For example:

1
git config --global http.proxy 'http://127.0.0.1:8118'

If you are using a SOCKS5 proxy:

1
git config --global http.proxy 'socks5://127.0.0.1:7070'

URL specific proxy:

1
git config --global http.https://domain.com.proxy http://proxyUsername:[email protected]:port

To disable the proxy, run command:

1
git config --global --unset http.proxy

To disable the proxy on a specific repository, change to the root directory and run:

1
git config --local --edit

Then reset the http.proxy attribute:

1
2
[http]
proxy =

Working with remote repositories

Set upstream repository

  1. Show remote repositories have already set
1
2
3
$ git remote -v
> origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
> origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
  1. Add a remote repository
1
$ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
  1. Verify
1
2
3
4
5
$ git remote -v
> origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
> origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
> upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
> upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)

Create a local branch based on a remote master branch

If you would like to keep the changes in your own master branch, you can run these commands to create a new local branch based on a remote master branch:

1
2
3
git checkout -b upstream-master
git fetch upstream
git reset --hard upstream/master
1
git push -u <remote> <branch>

rebase

Look up the base of your current branch:

1
git merge-base new-branch main

Outputs something like:

1
66e506853b0366c87f4834bb6b39d341cd094fe9

Run git rebase:

1
git rebase -i 66e506853b0366c87f4834bb6b39d341cd094fe9

Then set the message for your commit, push it to the server:

1
git push -f

Modify the author information in a branch

Create a .sh file with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/sh

git filter-branch --env-filter '

OLD_EMAIL="[email protected]"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="[email protected]"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

After you have successfully updated, push your changes to the remote repository:

1
git push --force --tags origin 'refs/heads/*'

List remote branches

This Git command will show you remote branches. The -r flag here is short for --remotes.

1
git branch -r

Push a local branch to a remote server

1
git push -u origin <branch>