Developing software with git

Introduction to git

git is a powerful distributed content versioning tool. You will always have your own local copy of the repository, and can share changes between other copies of the repository. I find a nice workflow management to be http://nvie.com/posts/a-successful-git-branching-model/ The basic idea is that the master branch should always be stable and tags are created on the master branch. A develop branch is where all the development happens. It is good to get into a commit early, commit often mindset and provide meaningful and useful log messages.

Working with git

Useful aliases

alias gitco=git checkout
alias gitbr=git branch
alias gitad=git add
alias gitci=git commit
alias gitst=git status

Following the workflow

Assuming a workflow as mentioned above, you will have at least two branches at any given time: master, develop If you have only these two branches, and you start to develop some particular feature, it is best to create a new branch off of the develop branch
gitco develop
gitco -b new-feature

You have finished working on your new feature branch and want to merge it back into the develop branch

The following will check out your develop branch, pull the changes from the remote you have called origin It will then merge the local branch new-feature and keep the full commit history (using the --no--ff (no fast-forward) option)

gitco develop
git pull origin
git merge --no-ff new-feature

At this point, you may have to fix merge conflicts. Follow the instructions and inspect the conflicting files. Manually modify them to match the desired version of the file.

If you no longer need the new-feature branch, you can delete it (either from the local repository only, or also from your remote)

gitbr -d new-feature
gitbr -D new-feature

If you have created a local branch v-important, and then made modifications on the parent branch release-v1 that you would like in the other local branch v-important you can do a rebase on v-important, rather than a merge. Suggestions seem to be strongly in favour of do not use rebase for changes that have been pushed elsewhere, so keep the rebase local unless you know what you're doing

gitco v-important
git rebase release-v1

Common operations

  • show all local branches which are not pushed to a remote (from here)
    git log --branches --not --remotes --simplify-by-decoration --decorate --oneline
       
  • show all local commits not on remote branches (from here)
    git cherry -v origin/somebranch
       

Special situations

  • splitting a subdirectory into a new git repository - split-em-up.sh

  • removing a subdirectory from all branches in a git repository - remove-subdir.sh
    • From your working repository (this will push all local branches and tags to your remote origin, if that is not what you want, you can omit the final steps and replace them with selective push commands, rather than the loop or --all and --tags commands):
      git filter-branch -f --tree-filter 'rm -rf subdirname' --tag-name-filter cat -- --all
      
      git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
      git reflog expire --expire=now --all
      git gc --prune=now
      
      git fetch origin
      git fetch origin --all
      
      #loop over local branches and push them individually, can be modified to skip some if you like
      for branch in `git branch | grep -v '\*'`
      do
          echo "git checkout ${branch}"
          git checkout ${branch}
      
          echo "git push origin ${branch} --force-with-lease"
          git push origin ${branch} --force-with-lease
      done
      
      git push origin --force-with-lease --all #push all local branches
      git push origin --force-with-lease --tags #push local tags, comment 
            

Undoing mistakes

Miscellaney

-- JaredSturdy - 2015-09-24

Edit | Attach | Watch | Print version | History: r5 < r4 < r3 < r2 < r1 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r5 - 2016-09-22 - JaredSturdy
 
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    Main All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback