Table of contents
  1. Show all the tracked files or ignored files
  2. Change the editor to vi(vim) when git commit
  3. Using git to show the difference of file in different tags or branches
  4. Delete a Git branch locally and remotely:
  5. change the branch name from “master” to “main”
  6. git checkout
  7. Pretty Git branch graphs
  8. Working with Remotes
  9. git push -f origin master
  10. git push your local repository to a new github repository
  11. password authentication
  12. untrack some file after commit command

Show all the tracked files or ignored files

  1. Show all the tracked files: git status --untracked-files=all
  2. Show all the ignored files: git status --ignored

Change the editor to vi(vim) when git commit

Sometimes when you write the command line “git commit” to write some message, the nano editor will pop out. If you don’t like to use this editor, you can convert it to vi(vim) editor as shown below

git config --global core.editor "vim"

OR 

git config --global core.editor "vi"

Using git to show the difference of file in different tags or branches

$ git diff tag1 tag2 

$ git diff tag1 tag2 -- some/file/name

Delete a Git branch locally and remotely:

* `git push -d <remote_name> <branch_name>` ,and `<remote_name>` may be origin.删除remote端brach
* `git branch -D branch_name` : delete local branch

change the branch name from “master” to “main”

when your branch name is master, you can use the following command to change it to main.

$ git branch -m master main

git checkout

git checkout id_number | tag_name

git checkout tags/<tag_name>

Pretty Git branch graphs

throw the following two aliases in ~/.gitconfig file:

[alias]
lg1 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
lg = !"git lg1"

Working with Remotes

  • git remote: It lists the shortnames of each remote handle you’ve specified
  • git remote -v: shows you the URLs that Git has stored for the shortname to be used when reading and writing to that remote
$ git remote -v
origin  https://github.com/schacon/ticgit (fetch)
origin  https://github.com/schacon/ticgit (push)
  • git remote add <shortname> <url>:增加新的remote端
$ git remote add pb https://github.com/paulboone/ticgit
  • $ git fetch <remote>:从remote端下载文件到本地。下载的文件为本地不存在的文件
  • $ git push origin master: 把本地文件上传到remote
  • $ git remote remove paul: 删除remote paul。

git push -f origin master

当在本地创建一个新的respotory,然后想上传到已有的github respotir 中,可以强行上传使其融合。

-f: force

git push your local repository to a new github repository

  1. Create a new repository in your github
  2. commit your local respository. If you haven’t inited your local respository. Init your local repository: git init
  3. rename the branch name from ‘master’ to ‘main’ : git branch -m master main
  4. git remote add <remote-name> <remote-github-url>
  5. pull the remote repository locally git pull <remote-name> main --allow-unrelated-histories Here --allow-unrelated-histories is essential. Otherwise you’ll encourter a git error: “fatal: refusing to merge unrelated histories”
  6. commit the changes and push to the remote github: git push <remote-name> main
  7. The first time you use pull or push may need to do the autorization.

password authentication

当你不能pull或者push的时候需要重新使用token 认证。参照上面的步骤进行。

Create Personal Access Token on GitHub From your GitHub account, go to Settings → Developer Settings → Personal Access Token → Generate New Token (Give your password) → Fillup the form → click Generate token → Copy the generated Token, it will be something like ghp_sFhFsSHhTzMDreGRLjmks4Tzuzgthdvfsrta

untrack some file after commit command

There are some situations that you’d like to untrack some files after you have commited. E.g. you’d like to untrack the database original.db. then use the following command to remove it from the tracked files.

git rm --cached /\*.db or git rm --cached path-to/original.db