Friday, July 1, 2016

git tips


* git cherry-pick  -- a range of commit
git cherry-pick ebe6942^..905e279


4. Author submits change commit to Gerrit for review
4.1 submit a change (a commit)
4.1.1 before submitting your patch run git rebase to avoid conflict
$ git pull --rebase
4.1.2 easy way if you're under SDK
$ repo upload
4.1.3 another way using git push
$ git push ssh://git.ebmajor.com:1234/oss/system/feeds/branch_name HEAD:refs/for/repo_branch_name
$ git push ssh://git.ebmajor.com:1234/oss/system/feeds/branch_name <local_repo_branch_name>:refs/for/repo_branch_name
ssh://git.ebmajor.com:1234 it's common part
oss/system/feeds/branch_name project name.
project names can be found here
https://git-poh-scl.ebmajor.com/cgit.cgi/?s=name
HEAD:refs/for/repo_branch_name branch name, "repo_branch_name", used at repo init.
$ git push ssh://git.ebmajor.com:1234/wifi/poh-wireless HEAD:refs/for/repo_branch_name
ssh://git.ebmajor.com:1234 it's common part
wifi/poh-wireless project name.
project names can be found here
https://git-poh-scl.ebmajor.com/cgit.cgi/?s=name
HEAD:refs/for/repo_branch_name branch name, "repo_branch_name", used at repo init.
4.1-1 submit single change
$ git push ssh://git.ebmajor.com:1234/oss/system/feeds/branch_name <commit SHA1>:refs/for/repo_branch_name

git log --oneline --pretty=format:"%ad - %an: %s" --after="2019-06-01" --until="2019-06-30" --author="Peter"

6. Extra commands good to know
* make changes on top of current patch
$ git commit --amend
* display branch's history and has lots of options
$ git log
$ git log --oneline //display in short
$ git log --author="author name" //search by author
$ git log --grep="strings" //search by string
* display a file's history
$ git log xxx.c
* display the file's change history line by line
$ git blame xxx.c
* delete untracked files and directory
$ git clean -df
$ git clean -dfx
$ git clean -dffx
* save changes temporarily and clean current branch
$ git stash save --keep-index
* re-store saved changes
$ git stash pop
* delete everything that not saved as a commit
$ git reset --hard HEAD
* delete very top commit
$ git reset --hard HEAD~
* delete top third commit
$ git reset --hard HEAD~3
* rollback your commit
$ git revert sha1
* display the history of git commands
$ git reflog
* make a git branch clean (go back) to specific git command history
$ git reflog // look for sha1 to go back
$ git reset --hard sha1
* remove a branch that does not need any more.
$ git branch -D "branch name"
* remove the branch that I'm on currently
$ repo abandon "branch name"
or
$ git checkout HEAD^
$ git branch -D "branch name"
* remove all the branches that local patches are already merged
$ repo prune
* display all the branches including remote repositories
$ git branch -a
* ???
$ git rev-parse
* sync current branch to <branch name>
$ git reset --hard <branch name>
* sync to specific branch and specific sha1
$ git rebase <branch name or commit ID>
$ git rebase --onto master 169a6
* to sync to a particular Release Image
repo init -b refs/tags/LINUX_RELEASE_repo_branch_name_1.01 -u ssh://git.ebmajor.com:1234/releases/manifest/stak.git -m versioned.xml
* create a branch on tag
$ git tag -l
$ git checkout -b local_branch tags/tagname
* adding author manually
$ git commit --amend --author 'First Last <user@ebmajor.org>'
* generate a patch file from a commit
$ git format-patch HEAD^
$ git format-patch -1 <sha1>
$ git format-patch --start-number 41 -1 <sha1>
* cherry pick single commit from another branch
$ git cherry-pick <sha1>
* cherry pick multiple commits from another branch
$ git checkout -b newbranch 62ecb3 (sha1 on local-branch)
$ git rebase --onto ath-next 76cada^
* amend commit of old patches
$ git rebase -i HEAD~5 (up to the commit we want to edit)
    This command will pop-up dialog like below.
"pick e7e285a UPSTREAM: :
pick 685c90f UPSTREAM:
pick e0f9f10 UPSTREAM:
pick 19f5562 UPSTREAM:
pick a94c23a FROMLIST:
# Rebase cfb53a9..a94c23a onto cfb53a9"
Replace "pick" with "reword" before each commit message you want to change.
"     reword e7e285a UPSTREAM:
reword 685c90f UPSTREAM:
Save and close the commit list file.
In each resulting commit file, type the new commit message, save the file, and close it.

* man git-format-patch

* add git user on local directory only (without "--global")
git config user.name "Your Name"
git config user.email "you@example.com"

* reset author/committer
git config --global user.email "xxx@xxx.com"
git commit --amend --reset-author

* show changes before sha1 (git blame prior to sha1)
git blame sha1^ -- src/options.cpp (git blame fe25b6d^)

* git logs on removed file
git log --all --full-history -- "**/thisfile.*" git log --all --full-history -- <path-to-file>

* git add removed file
git add -u .

* merge specific files/folders from different git repository
* mac80211_plus_ebmajor: mac80211 branch to merge ebmajor (mac80211 based)
* ebmajor_to_be_merged: ebmajor branch from ebmajor.git to be merged to mac80211
1. add another git repository to current working git repository (add ath10k git to mac80211 git)
mac80211$ git remote add ebmajor git://git.kernel.org/pub/scm/linux/kernel/git/ebmajor.git
mac80211$ git remote add ebmajor ~/works/ebmajor
2. apply the remote git repository
mac80211$ git remote update ebmajor
3. check remote git branch if it merged to working repository
mac80211$ git branch -a
4. create local branch from ebmajor git which to be merged
mac80211$ git checkout -b ebmajor_to_be_merged remotes/ebmajor/ebmajor_to_be_merged
4. create a local branch based on mac80211 git (create branch from mac80211 git)
mac80211$ git checkout -b mac80211_plus_ebmajor remotes/origin/master
5. checkout target remote branch with folder/file name specific (checkout ebmajor git on mac80211 branch)
mac80211$ git checkout ebmajor_to_be_merged -- drivers/net/wireless/ebmajor/
* it gets ebmajor files from ebmajor git and overwrites ebmajor files on mac80211 git
6. run git commit
git commit -s
7. update remote branch (ebmajor_merge_to_mac)
$git remote update

* repo sync verbose mode
$repo --trace sync

* repo sync quickly
“-c” flag. repo will only download the revision (branch) that is specified in the manifest, not all the branches that are on the remote server.
$repo sync -c

git checkout mac80211_master
git read-tree --prefix=ebmajor/ -u ebmajor_master:drivers/net/wireless/ebmajor/

* Add gerrit review URL

* git bisect
git bisect start net/mac80211/ drivers/net/wireless/ebmajor/ ; git bisect bad ; git bisect good v4.6

* remove cached file
git rm --cached xxx

* remove local branches no longer on remote
git remote prune origin

* add hooks for Change-Id
cd project_folder (which .git folder exist)
scp -p -P 29418 peter.oh@ebmajor.com:hooks/commit-msg .git/hooks/

* git log lines
git log -L 15,23:filename.txt
means "trace the evolution of lines 15 to 23 in the file named filename.txt".

* git log to removed lines
git log -c -S'missingtext' /path/to/file

git log -S <string> path/to/file
git log --cc -S'missingtext' /path/to/file

* git clone with submodule
git submodule update --init --recursive

* do repo sync with submodule download
1. in manifest, specify the attribute sync-s="true"
for example <default sync-s="true"/> to apply to all projects
or for a specific project, <project sync-s="true"/>
2. repo sync --fetch-submodules

* force update for 2nd commit
git push github +HEAD

* git log from specific date
git log --after="2013-11-12 00:00" --before="2013-11-13 00:00"
git log --since="2014-02-12T16:36:00-07:00"
git log --after="2014-02-12T16:36:00-07:00"
git log --before="2014-02-12T16:36:00-07:00"
git log --since="1 month ago"
git log --since="2 weeks 3 days 2 hours 30 minutes 59 seconds ago"
git log --date=local --after="2014-02-12T16:36:00-07:00"

* merge 2 different branches on different remotes
------
step 1 - check in existing codes
------

packages/qca-rfs$ git checkout -b github_master remotes/origin/release/collard_cc_cs

peter@peter-linux-dell:~/works/src/qsdk_packages/qca-rfs$ cat .git/config
[remote "origin"]
url = https://source.codeaurora.org/quic/qsdk/oss/lklm/qca-rfs
fetch = +refs/heads/*:refs/remotes/origin/*
[remote "github"]
url = git@github.com:ebmajor/qca-rfs.git
fetch = +refs/heads/*:refs/remotes/github/*
[branch "github_master"]
remote = origin
merge = refs/heads/release/collard_cc_cs

packages/qca-rfs$ git push -u github github_master

------
step 2 - merge branch on different remote
------

git clone git@github.com:ebmajor/qca-rfs.git

git remote add caf https://source.codeaurora.org/quic/qsdk/oss/lklm/qca-rfs

git checkout github_master

git pull caf release/dandelion_ath10k //pull "release/dandelion_ath10k" from caf to "github_master"
// don't need "fetch src"

github/qca-rfs$ cat .git/config
[remote "origin"]
url = git@github.com:ebmajor/qca-rfs.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "github_master"]
remote = origin
merge = refs/heads/github_master
[remote "caf"]
url = https://source.codeaurora.org/quic/qsdk/oss/lklm/qca-rfs
fetch = +refs/heads/*:refs/remotes/caf/*

git push -u origin HEAD (or git push -u origin github_master)

----------
* git merge a local branch to another local branch
----------
local-branch-1 (local-branch-2 will be merged to here)
local-branch-2

1. git checkout local-branch-1
2. git merge local-branch-2
--------------

* create a new branch from local branch reference
git checkout -b poh_local_branch_from_local local_branch_org (no origin/)

=====
git merge except some commits
=====
master$ git merge maint~4
master$ git merge -s ours maint~3
master$ git merge maint

* squash multiple commits to one commit

 $ git rebase -i develop

 upstream branch should be the branch that current local branch is based on.
 If my local branch name is "peter/ipq40xx/develop" which is based on upstream "develop" (remote/origin/develop)
 then run git rebase -i develop.

$git rebase -i develop

 This will open an editor window, with a bunch of commits listed prefixed by pick.
 change all of pick to squash except the first commit.
 This tells git to keep all of those changes, but squash them into the first commit.

* git diff between branches
$ git diff branch_1..branch_2 (2 dots)
   to produce the diff between the tips of the two branches.
$git diff branch_1...branch_2 (3 dots)
   to find the diff from their common ancestor to test

3 comments: