Git Workflow
Sep 30, 2016
[Huy Le]
4 minute read
WIKI LAYOUT

Merging vs. Rebasing #

The two essentially do the same thing. the difference is that rebase makes the history linear. This is good when you are developing on your own branches, but not good when we you working on public branch, Unable to resolve reference to #pitfalls from wiki/git-workflow.mdmore here

To merge or rebase master into a feature

  git checkout feature
  git merge master
  # or.. a oneliner
  git merge master feature # say it in your head, git merge master into feature

Rewriting history #

keeping a clean history is important This code snippets will allow you to interactively update commits for a particular branch.

  git rebase -i HEAD~2

for this case, we are looking at interactive rebase of the previous two commits, you get something like this. This is a script that git will run and stop at each command

  pick 34c10c4 one of my message
  pick a04f701 my other message

  # Rebase 8505511..a04f701 onto 8505511 (2 commands)
  #
  # Commands:
  # p, pick = use commit
  # r, reword = use commit, but edit the commit message
  # e, edit = use commit, but stop for amending
  # s, squash = use commit, but meld into previous commit
  # f, fixup = like "squash", but discard this commit's log message
  # x, exec = run command (the rest of the line) using shell
  # d, drop = remove commit
  #
  # These lines can be re-ordered; they are executed from top to bottom.
  #
  # If you remove a line here THAT COMMIT WILL BE LOST.
  #
  # However, if you remove everything, the rebase will be aborted.
  #
  # Note that empty commits are commented out

by updating the lines in between this ’todo’ document

  pick 34c10c4 one of my message
  x git commit --amend -m "my new message"
  pick a04f701 my other message
  x git commit --amend -m "my other new commit message"

the x is the exec or the command for running shell script This will updates the commit messages. Similar to any other commands

*Note about the other commands when a command is picked, it will run the ’todo’ file. This means that when a command like reword is chosen, the script will be - squash requires a previous commit..

By rebasing a branch, their merges from other branches are destroyed.

Getting update from main repo #

  git remote add upstream https://linkhere.com
  git fetch upstream
  git checkout master
  git merge upstream/master

Clear git cache #

  # Remove all cached files to ensure there are no .gitignore files being tracked
  git rm --cached -r .

  # Track the files that should be tracked
  git add .

Cleaning vs. Checkout vs. Reset #

  • Cleaning will remove all files that are not tracked in the directory

    • git clean -f - it will leave the git ignore files alone
    • git clean -xdf - careful to use this, it will remove ALL GITIGNORE FILES
  • Reset --hard will not remove untracked files, but git clean will

  • Checkout - will bring the files from specific commit into the working tree

    • git checkout . - checkout all the files, basically rewrite the files to the previous tracked commit

Git Submodules #

Submodules are used to track other git projects that are dependencies.

When cloning a project with submodules, use the following code to pull submodules git submodule update --init --recursive

this init the submodule,.this force the submodule to pull from the saved commit hash

  git submodule init  # this init the submodule
  git submodule update --force # this force the submodule to pull from the saved commit hash

if use this, the git submodule gets the latest commit

  git submodule update --init

antoehr read - http://stackoverflow.com/questions/17017335/no-submodule-mapping-found-in-gitmodules-for-path-and-missing-gitmodules-file

another read - http://stackoverflow.com/questions/1030169/easy-way-pull-latest-of-all-submodules

Editing git submodules #

important note - make sure you do git checkout master before you start hacking! editing submodules will be the same as any other git repo.

Git Worktree #

Allow parallel development

Extracting date from when file first added #

  git log --format=%aD content/essentials/cpp.md | tail -1

https://longair.net/blog/2010/06/02/git-submodules-explained/

Good way to re-branch old branch to a better new branch #

http://stackoverflow.com/questions/2763006/change-the-current-branch-to-master-in-git

House keeping and cleaning up #

Once in awhile, branches has to be prune and cleaned up; both on remote and local repo. http://railsware.com/blog/2014/08/11/git-housekeeping-tutorial-clean-up-outdated-branches-in-local-and-remote-repositories/

Pitfalls #

  • Dont use git rebase on public branches
    • If you try to push rebased master branch, it will not allow you due to commit conflicts
      • try to revert and merge instead of git push --force
        • git push --force is good for local cleaning up

discovered - git-extras #

Track Specific Filetype #

Stackoverflow here

# Blacklist everything
*
# Whitelist all directories
!*/
# Whitelist the file you're interested in. 
!*.cocci