Monday, April 11, 2016

Git

Pull
get latest code and merge
git pull

Fetch
get the latest code, require manually merge
this is better than pull, since we can check diff and decide to merge or not
git fetch
git log -p master..origin/master
git merge origin/master

Config
git config --global user.name "Yushan Lu"
git config --global user.email "lushan6999@gmail.com"

Create/Init a repository
git init

Add file
git add hello.txt

Commit
git commit -m "Commit1"

See log:
git status
git diff
git log
gitk

Change commit message
git commit --amend -m "Changes commit message"

Show remote repositories
git remote

Switch to branch
checkout master

See local branches
git branch

See local and remote branches
git branch -a

New branch
git branch newMyBranch


Stash
git stash


Push Conflict

We will get conflict error when the remote branch pushed by others
git push
So we can first pull
git pull origin master
Then use mergetool, choose meld or any editor to manually merge codes.
git mergetool
Then commit again, and push
git commit -m "Merged codes"
git push

Rebase
Git Rebase has two features
  • Combine multiple commits to one
    We can have multiple commits, but changes for one feature should stay in one commit, so the history is clean

    See log:   git log
    Merge last 3 commits:      git rebase -i HEAD~3
          Options:  pick|reword|edit|squash|fixup|exec|
                          or directly delete that line means cancel this commit
    Then :  git rebase (--continue | --abort | --skip | --amend)

    Do not commit again during this procedure, makes life easier.
  • Add one branch as a patch to another branch
    Different to merge, this will make cleaner commits history and keep change like within one route

    rebase the master: git rebase master


    example:
  • commit
    commit ...

    rebase commits for same feature(if necessary)

    stash un-commited changes (if necessary)
    git rebase master
    git push

Patch

Patch is good to use for large group of developers.
Developer generate a patch, then send the patch to project manager, or other developers to review.
Manager can apply patch.
If patch rejected, developer rebase the change and fetch to latest code, then generate the patch again.
Manager then apply the patch finally.

Suggest to use format-patch instead of git diff


Create one/more patch, based on commits
git format-patch origin/master
generated three patches
0001-Add-A.patch
0002-Add-B1.patch
0003-A-change.patch




Apply Patch

apply patch
git am 0001-Add-A.patch
or
git am --ignore-space-change --ignore-whitespace 0001-Add-A.patch

git add .
git commit -m "Apply patch"





Create a repository

mkdir mmm

cd mmm

git init

touch test.txt

git add .

git commit -m "create test.txt"


Clone from a repository, create a new branch

cd ..

mkdir m1

cd m1

git clone ~/servergit/mmm .

git branch m1

git checkout m1


Change the file

echo "fix from m1" >> test.txt

git add .

git commit -a -m "fix1"


Create a patch for master branch

git format-patch -m master

Apply patch on master branch

git checkout master

git am 0001-fix1.patch

git add .

git commit -m "apply fix"

























No comments:

Post a Comment