Git Commands - hqzhang/cloudtestbed GitHub Wiki

#Git bitbucket API

project="hqzhang"
repo="ansibletest"
workbr="new-branch"
mergebr="master"
repoPR=https://localhost:8081/rest/api/1.0/project/$project/repos/$repo/pull-requests
jq== "python -c 'import json,sys: print(json.load(sys.stdin)["vvalue"][0]["id"]'"

get-pull-request-id(){
    curl -u $user:$pass -X GET ${repoPR}?state=OPEN | jq -r 'values[0].id'
}

get-pull-request-version(){
    curl -u $user:$pass -X GET ${repoPR}?state=OPEN | jq -r 'values[0].version'
}

get-pull-request-merge(){
    curl -u $user:$pass -X GET ${repoPR}/$prid/merge | jq -r 'canMerge'
}

create-pull-request(){
     curl -u $user:$pass -X POST -H "Content-Type: applicatin/json" $repoPR \
     --data '
     { "title": "PR-testing",
       "description": null,
       "state":  "OPEN", "open": true,
       "closed": false,
       "fromRef":{ "id": "'"refs/heads/$workbr"'",
                  "repository": { "slug": "'"$repo"'",
                                  "name": null,
                                  "project": {"key": "'"$project"'"}
                 }}
        "toRef": { "id": "'"refs/heads/$mergebr"'",
                  "repository": { "slug": "'"$repo"'",
                                  "name": null,
                                  "project": {"key": "'"$project"'"}
                   }
                }
     "locked": false,
     "reviewers": []}
    '
}

merge-pull-request(){
    curl -u $user:$pass -X POST -H "Content-Type: applicatin/json" $repoPR/$prid/merge?version=$version
}

Git advanced command

git tag -a v1.0.0.    # like create a branch but also generate package
git push origin v1.0.0. # can view in branch and release tab

gh release create v1.2.3 # create release from git tag

git fetch          # just get remote change info

git pull.          # git fetch plus git merge

git merge mybranch  # merge mybranch to master at master and a new dummy commit is added

git branch mybranch # create new branch,   git checkout -b mybranch is better

git checkout -b mybranch # create branch and switch to it
git push -u origin mybranch # update to remote

git checkout mybranch    # switch branch

git add -u .             # add modified files to stage add and NEVER use git add . command

git commit -a  -m msg    # save change to local repo

git stash.               # temp save current enviromet and git stash pop when come back
git stash pop            # when you are back to branch

git rebase master        # at branch cool-feature
git merge cool-feature.  # at master merge and no dummy commit is added

git sqash. 
git checkout main && git pull && 
git checkout mybranch && git rebase -i main &&
git push force-with-least

git checkerrypick commit-id # like patch to pick which commit will merge to current space.

start a working area (see also: git help tutorial)
   clone     Clone a repository into a new directory
   init      Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add       Add file contents to the index
   mv        Move or rename a file, a directory, or a symlink
   restore   Restore working tree files
   rm        Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect    Use binary search to find the commit that introduced a bug
   diff      Show changes between commits, commit and working tree, etc
   grep      Print lines matching a pattern
   log       Show commit logs
   show      Show various types of objects
   status    Show the working tree status

grow, mark and tweak your common history
   branch    List, create, or delete branches
   commit    Record changes to the repository
   merge     Join two or more development histories together
   rebase    Reapply commits on top of another base tip
   reset     Reset current HEAD to the specified state
   switch    Switch branches
   tag       Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch     Download objects and refs from another repository
   pull      Fetch from and integrate with another repository or a local branch
   push      Update remote refs along with associated objects



#Git Credential Settings

0. user/pass settings
i) client side:

1. ssh git settings
i) client side: 
ssh-keygen -t rsa 
into ~/.ssh/id_rsa,id_rsa.pub
ii) server side:
copy id_rsa.pub into server into ~/.ssh/authorized_keys
for github, add id_rsa.pub into https://github.com/settings/keys


  1. Trigger Jenkins Job by git
1. vim post-push
.githooks
post-push
#!/bin/bash
set -x
job=$(basename `pwd`)
job=alphatest3
user=hongqi
token=1133079f045e85425d214a8ac75e58c804
url="localhost:8080"
echo "Trigger a build"
curl -I -X POST http://${user}:${token}@${url}/job/${job}/buildWithParameters
2. export PATH='pwd':$PATH
3. alias gpush(){ git add $1 && git commit -m update && git push && post-push'
4. gpush somefile
  1. create repo in github
   login https://github.com/hqzhang
  1. download repo from github
   git clone http://github.com/hqzhang/cloudtestbed
  1. save the change on local
   cd cloudtestbed
   touch newfile (create new file or change a existing file)
   git add newfile            (add the file into stage)
   git commit -m "add new file" (save the change with log)
  1. upload change into repo
   git push origin localbr:remotebr (save change to remote and create a branch remotebr if unavailable)
   git push origin  localbr (localbr=remotebr)
  1. update local repo
   git fetch origin ( just download)
   git merge origin/localbr( merge download with localbr)
   git pull origin remotebr (download and merge remotebr to local)
  1. view change and branch
   git branch   (display local br)
   git branch  -r (display remote br)
   git status   (after change/commit)
   git log      (after commit)
  1. git other commands
   git reset --hard HEAD~2 (clean recently 2 change log)
   git commit --amend      (save the change and use same log)