notes_git
1.3k7NOTESGit2021-11-17

to avoid being ask Enter passphrase for key

auto-launching-ssh-agent-on-git-for-windows

rebase

  • git rebase -i HEAD~<number> or git rebase -i <commit number> for using other commands, just change the pick word to the command name
  • edit multiple commit messages:
    • git rebase -i HEAD~<number> or git rebase -i <commit number> modify pick to edit, then save and exit
    • git commit --amend -m ""
    • git rebase -continue to the next commit
  • insert commit: run rebase command to the location, and change the commit to edit whose location is right before the location you want to insert. then you will be at the insert location and able to make a new commit or pick a commit.
  • squash commits, change commits you want merged to squash or fixup(this will discard the commit messages). (only consecutive commits can be squashed, while the commits order in editor is changeable)

tag

  • git tag -d <tag name>
  • git push origin :refs/tags/<tag name> git push origin --delete <tag name> push the delete operation to the remote
  • git tag -a <tag name> <commit number> -m <tag commit message>
  • git show <tag name> to display the tag info along with the commit info

basic

Config user name and email git config  --global user.name / user.email “”
git config --list check git config list
pwd show
git init
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
  • git show-branch <branch 1> <branch 2>

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 entire 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

restore

  • git restore . discard local changes

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 "" amend the last commit mesg
  • git push –u origin [branch name] push local file to remote
  • git push origin [branch name] push the last altered file to remote when local made a submit
  • git push -u origin [branch name] -f update forcibly
  • git push origin [branch name] submit the content that altered by branch
  • git add --patch <filename> break down the given file into thunks, and choose thunks to commit. input ? to get commands description
  • git push origin <commit>:<branch name> push a given commit to a branch

branch

  • git branch -r --list
  • git branch -a all the branches
  • git branch --show-current
  • git branch -m <new name> alter branch name
  • git branch -d <name> delete branch name locally
  • git branch -D <name> delete branch name remotely
  • git clone -b <branch> <remote_repo>
  • git checkout -b [branch name] create(if the branch doesn’t exist) and switch to the branch

restore a deleted branch

  • git reflog find the last sha of the deleted branch, and git checkout -b <branch_name> <sha> get back

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 checkout -b [branch name] create(if the branch doesn’t exist) and switch to the branch

  • git checkout [branch name] switch to a branch only

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

  • git checkout . clean un-staged 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
  • git cherry-pick -v <branch name 1> <branch name 2>
  • git cherry-pick <commit start>^..<commit end> pick multiple commits

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 branches no common bases
  • git merge-base <target branch> <refer branch> compare the two branches and show the common base

clean

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

pull

  • git pull --all

fetch

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

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