Working with local repositories First-time setup Git Aliases Delete a Local Branch Modify the username and email in history Go back to a previous commit Show all changes in a specific commit Show all the configuration information and files git patch Filter the log by time range and a specific author configure Git to use a proxy Working with remote repositories Set upstream repository Create a local branch based on a remote master branch Link your local branch with a remote branch rebase Modify the author information in a branch List remote branches 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:
Show all changes in a specific commit
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"
Setting a global proxy:
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:
Working with remote repositories Set upstream repository
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)
Add a remote repository
1 $ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
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
Link your local branch with a remote branch 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:
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
.
Push a local branch to a remote server 1 git push -u origin <branch>