Git commands you can show off for 100 years
Life is short, make a big splash!
To undo a git switch
git switch -
How to delete all local files blocking you pull, unstash and merge
git clean -n
git clean -f
First -n
will dry run to check and list files can be delted.
While -f
will forcely to delete local files
hall-of-frame by commit numbers
git shortlog -s | sort -n -r
In short, shotlog
will group and list all commits by author. By default, it will list each commits by author, while -s
will only show the number of commit.
Or you can use even a simpler version to sort authers by commit number
git shortlog -s -n
How to to list of what branch for a given commit
$ git log --author todd --grep e2e
$ git name-rev 78a4c4c340de70c726844e97233c58fa4738fea9
78a4c4c340de70c726844e97233c58fa4738fea9 remotes/origin/feature/term-deposit-protractors~1
git log tips
There are various parameters calling git log
- git log –oneline
- git log –stat To show numer of lines added or remvoed
- git log -p OR git log –patch To show file changes contents
- git log –graph –oneline
- git log -S”Search_Keyworkd” –oneline
- git log – file1.java file2.kt
- git log –grep=”JIRA-123”
- git log –author=”Tom|Jerry”
- git log –after=”yesterday”
- git log -3 to list last 3 commits
git show
When the show method is used, it displays all the default information but for only one commit. When a commit SHA is not specified the latest commit is displayed and the output is the same as that displayed by the patch -p command. git show
To list changed files and also changes number of lines
git show --stat
To search keywords in js files only
$ find . -name '*.js' | xargs grep -r 'SearchKeywords'
One line command to add and commit file
git status --short | awk '{split($0, a);print a[2]}' | xargs git add && git commit -m 'summit status'
to show files commited but not pushed
git diff --stat --cached origin/feature/BRANCH_NAME
to view file content changed
git show PATH/FILE.sql
One line to fetch, checkout newly created branch
git fetch && git for-each-ref | grep 'AO-106' | awk '{split($0,a);print a[3]}' | awk "{split($0,a, '/');print a[-1]}"
To get changed files NOT
contains keyword
$ git status | grep -v "node_"
-v is for reverse
Cherry-picking
Cherry-picking in Git is designed to apply some commit from one branch into another branch
.
It can be done if you eg. made a mistake and committed a change into wrong branch, but do not want to merge the whole branch. You can just eg. revert the commit and cherry-pick it on another branch.
To use it, you just need git cherry-pick hash, where hash is a commit hash from other branch.
Difference among git merge and git rebase
TL;NR
Git rebase and merge both integrate changes from one branch into another. Where they differ is how it’s done. Git rebase moves a feature branch into a master. Git merge adds a new commit, preserving the history.
Rebase
Git rebase compresses all the changes into a single “patch.” Then it integrates the patch onto the target branch. Unlike merging, rebasing flattens history. It transfers the completed work from one branch to another. In the process, unwanted history is eliminated.
Advocates of Git rebase like it because it simplifies their review process.
Merge
It takes the contents of a source branch and integrates it with a target branch.
Differences among Git and subversion
The differences between git and other source control (like subversion) is Git have to merge conflict before upload to server, while subversion will merge conflicts at server side
following pages are searchable in google
- alice
- github
Errors
branch diverged
~|master ⇒ git status On branch master Your branch and ‘origin/master’ have diverged, and have 1 and 6 different commits each, respectively. (use “git pull” to merge the remote branch into yours)
The git pull command provides a shorthand way to fetch from origin and rebase local work on it:
$ git pull –rebase
To check out and rebase master to current branch
git rebase HEAD master
undo a commit
git revert <commit hash>
Your branch is ahead of ‘origin/master’ by xx commits
Go to Intellij, select git
view and chose log
, then select previous version before those xx commits, then chose Reset current branch to here
and with hard
as option.
Git commit
List files of a given commit
git show commit_id
git show --pretty="" --name-only bd61ad98
Updates were rejected because the tip of your current branch is behind
git push --set-upstream origin master
To https://github.com/CloudsDocker/cloudsdocker.github.io.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/CloudsDocker/cloudsdocker.github.io.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
···
git push To https://github.com/CloudsDocker/cloudsdocker.github.io.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to ‘https://github.com/CloudsDocker/cloudsdocker.github.io.git’ hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: ‘git pull …’) before pushing again. hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details. todds-MacBook-Pro:cloudsdocker.github.io todzhang$ git pull fatal: refusing to merge unrelated histories
Solution
git -c credential.helper= -c core.quotepath=false -c log.showSignature=false merge origin/main --no-stat -v