Monday, March 17, 2014

git

Using git

Edit

Simple git branching model

Edit

Main branches:

  • master - branch where the production code is stored (we can tag releases)
  • develop - branch where the development code is stored
git branch            # shows our branches and marks the one we're currently working on
git checkout develop  # switching to branch 'develop'
git checkout master   # switching to branch 'master'
git checkout -b develop --track origin/develop  # creates local branch develop tracking origin/develop (remote develop branch)
git push -u origin new_feature              # pushes your local branch new_feature to the remote repository 
Edit

Feature branches

When starting implement a new feature, create new branch from the develop branch:
git checkout -b myfeature develop
Should branch off from: develop
Must merge back into: develop
Merge feature branch with develop:
git checkout develop         # switching to branch 'develop'
git merge --no-ff myfeature  # merging develop with myfeature branch
git branch -d myfeature      # deletes branch myfeature 
git push origin develop      # push changes to remote branch
Edit

Hotfix branches

Two ways:
  • we create separated branches for hotfixes or working (then we can easily merge to different branches: master, develop, feature braches)- recommended
  • we can work on main branches - it's not recommended
If we decide to follow first approach we're doing exactly same thing as for new feature:
git checkout -b hotfixes-1.2 develop
git checkout master            # switching to branch 'master'
git merge hotfixes-1.2         # merging master with hotfixes-1.2 branch
git push origin master         # push changes to remote branch
git merge hotfixes-1.2         # merging develop with hotfixes-1.2 branch
git push origin develop        # push changes to remote branch
Edit

Conflicts

When me merge to branches we can have some conflicts and before we do anything we have to:
  1. resolve conflicts
  2. commit changes
Edit

Basic Git commands

(Using simple language - if you're looking for more technical details: https://confluence.atlassian.com/display/STASH/Basic+Git+commands)
git clone <repo_url>                                   # clones the repo
git branch                                             # shows local branches
git branch -a                                          # shows local & remote branches
git checkout <branch_name>                             # switches branch
git checkout -b <local_branch> --track origin/<remote_branch> # creates local branch tracking given remote branch and switch to its
git status                                             # shows file paths that have differences between last (HEAD) commit and current index
git diff                                               # shows differences (one-by-line) that have differences between last (HEAD) commit and current index
git add .                                              # includes all files to commit 
git add <path_to_file1> <path_to_file2> ...            # includes given file to commit
git commit -m "Commit message..."                      # commits changes with given message
git pull                                               # merges with remote repository 
git commit -a                                          # commits and opens a editor (shows changes and asks to put commit message)
git push                                               # updates remote repository
git log                                                # shows the changes (commits) history (log)
git stash                                              # moves current changes to "temporary bucket" 
git stash apply                                        # moves changes from "temporary bucket" to current branch
git stash clean                                        # cleans the "temporary bucket" 
Edit

Common flow

1. If you are working on new feature
git branch                   # make sure you're working on develop branch
git checkout -b new_feature  # create new branch for feature you're going to implement 
2. If you want to commit some changes:
git status                                     # check in what files you made changes 
git diff                                       # check and review changes one by line
git add <file1> <file2> ...                    # decide what files you want to include to commmit
git add .                                      # ...or include all of them
git commit -m "First version of new feature."  # commit changes
3. If you want to include new feature to local develop
git checkout develop  # switch branch to develop
git status            # make sure there is no uncommitted changes
git merge new_feature # include changes for new_feature to develop branch
...resolve conflicts if they exist...
git commit -a         # commit merge changes
4. If you want to push changes from local develop to remote develop
git checkout develop                  # switch branch to develop
git pull origin develop               # pull changes from remote develop
...resolve conflicts if they exist...
git commit -a                         # commit 
git push origin develop               # push your changes to remote develop
5. If you want to make some changes (and update remote repo) working on develop branch (without creating new branch)
git checkout develop                  # switch branch to develop
...make same changes...               
git status                            # make sure there is no uncommitted changes
git diff                              # check and review changes one by line
git add <file1> <file2> ...           # decide what files you want to include to commit
git add .                             # ...or include all of them
git commit -m "my quick changes"      # commit changes
git pull origin develop               # pull changes from remote develop
...solve conflicts if they exist...
git commit -a                         # commit changes
git push origin develop               # push your changes to remote develop

Edit

To read:


git stash apply stash@{1}
git reset head@{1}
git checkout develop --path
git log
git reflog
git rebase master

No comments:

Post a Comment