Git notes
9245NOTESGit2021-11-17

Config user name and email git config  --global user.name / user.email “”
git config --list check git config list
pwd show the catalog of repo
git init chan’ge the catalog to a repo which git can manage
SSH binding computer ssh-keygen -t rsa -C "your email@example.com"

  • git config --remove-section gui reset gui settings

diff

  • git diff show all the changes with detail
  • git diff --name-only show all the changed file name
  • git diff --name-status show only names and status of changed files
  • git diff --unified=0 git diff -U0 only show the changed lines

show

show is used for a certain commit, while log is for getting a history

  • git show commit number display the changes of a given commit

log

git log --name-status show history list with file name and status
git log -p <file path> show history list of the given file
git log --follow -p <file path> show the enitre history(including rename ) of the given file
git log -L <start line number>,<end line number>:<file path> show a history list of the limited lines of the given file
git log <commit number> -L <line number>,<line number>:<file path> show a history list of the limited lines of the given file of the given commit

git log --all show all the branch commits
git log --author=<author name> show a history list made by the given author
git fetch --all && git log <origin/branchName> get the repo state from remote and show the given branch commit history
git status
git log --date=relative|local|default|iso|iso-strict|rfc|short|raw to change date formats

git log --grep=helloworld search for commit tips
git log -S 'helloworld' --source --all search for commit code
git log -G “^(\s)function foo[(]){$” –source –all

revert && reset

  • git revert "commit number" revert to a given commit

  • git reset "commit number" revert to a given commit, and the commits after the given commit will be deleted

  • git reset HEAD^

  • git reset --hard origin/feat-windows setting branch to match remote exactly

  • git push origin +<commit>:master revert to a certain commit, and force push to the remote

remote

  • git remote -v remote list
  • git remote add/rm origin [repo name] add or remove a remote repo address
  • git remote set-url origin [repo name] update the existing remote when add a remote that say which already exist
  • git fetch download from remote

push

  • git add [file] && git commit -a || git commit -m “message”&& git push
  • git commit --amend -m "" ammend the last commit mesg
  • git push –u origin master push local file to remote
  • git push origin master push the last altered file to remote when local made a submit
  • git push -u origin master -f update forcibly
  • git push origin [branch name] submit the content that altered by branch

branch

  • git branch -r --list
  • git branch -a all the branchs
  • git branch --show-current
  • git branch -m <new name> alter branch name
  • git branch -d <new name> delete branch name locally
  • git branch -D <new name> delete branch name remotely
  • git clone -b <branch> <remote_repo>

checkout

  • git checkout -d [branch name] delete a branch locally, so need push to remote

  • git checkout -D [branch name] delete a branch locally, to force deleting the branch without checking merged status

  • git push [repo name] --delete [branch name] delete a branch both locally and remotely

  • git checkout [branch name] switch to a branch

  • git checkout -b [branch name] switch to a branch, if the repo doesn’t have the branch then the branch will be created and switched to

  • git checkout . clean unstaged changes made in the current branch

  • git checkout <file path> clean the change of a specific file in the current branch

  • git checkout <branch name> <file path> to revert a single file according to branch

  • git checkout <commit number> <file path> to revert a file according to previous commit

  • get a file from another branch or another commit and then put it into another path

    • git checkout <branch name> <file name>git checkout <commit num> <file name> get file
    • git mv <old file name> <new file name> copy and move
    • git reset <old file name> reset the original file
  • git push --set-upstream origin [branch name] To push the current branch and set the remote as upstream

cherry-pick

  • git cherry-pick <commit -number> to apply a specific commit to the current branch
  • git cherry-pick -n <commit -number> to apply a specific commit to the current branch without commit automatically

merge

  • git merge <branch name or a commit num> to merge with the given branch or commit
  • git merge <branch name> --allow-unrelated-histories to merge two branchs no common bases

clean

  • git clean -f remove untracked files
  • git clean -fd remove untracked directrons

pull

  • git pull --all

fetch

  • git fetch --all get all the change of all the branchs

config

  • git config --global gui.encoding utf-8 set gui encoding as utf-8

Exit editor

press i to enter insert mode, ESC (exit the inset mode)
write :WQ (write & quit) and press enter