Course Week01 Day05

Made it throught the week!

  • Custom indexes when iterating through an array:
    • [:foo, :bar, :baz].to_enum.with_index(1).each do |elem, i|
  • Depth vs breadth first searching
    • if you have a tree-like structure, and are filling an array with the children of nodes…
      • you may use that array as last in, first out (stack), maybe with pop , to do a depth first search
      • or, you may use that array as first in, first out (queue), maybe with shift, to do a breadth first search
  • counterpart of the any? enumerator is none? , good for when you need to make sure say, that no items in an array are divisible by 5
  • When you have a method that may take a block, its very slick to use a default block with the ||= notation instead of using if block_given?

Git Weekend

  • Orienting yourself
    • git diff will show specific information about differences between committed and unstaged changes (git diff --staged to get difference between commited and staged)
    • can compare anything: git diff HEAD^^ or git diff HEAD~5 or git diff HEAD^^^..HEAD^ or name the SHAs, or name the branches (no dots required in this case), or time ranges with –since
    • git blame filename
  • Unstaging
    • since HEAD refers to last commit on current branch/timeline
      • git reset HEAD filename to unstage
    • restore a file to its previous state
      • git checkout -- filename
        • the -- is needed to ensure so that you don’t checkout a branch accidentally
  • shortcuts
    • git commit -a -m "comment" will add and commit tracked files
    • git checkout -b branch-name
    • might want to make an alias: git config --global alias.alias_name \ "git command" or short two letter-like ones like: git config --global status
  • Regret a commit? (Don’t do this if you’ve pushed already)
    • reset, move to commit before HEAD (note the caret), want to move further back? Add more consecutive carets
      • git reset --soft HEAD^ rewinds
      • git reset --hard HEAD deletes
    • amend, with an optional message to override the last
      • git commit --amend -m "new comment"
  • Branches/Merging
    • after merging a branch to the master, it’s possible to delete the old one
      • git branch -d branch-name
      • won’t work if there are unmerged commits there, need -D instead of -d
    • if it isn’t a fast forward merge
      • recursive merge
        • happens when there have been changes on the master branch after an aux branch was made, before it was merged
        • git makes a merge commit automatically when merging (visible in log, but doesn’t list any files)
    • two people have worked at the same time from a remote repo, person1 has already pushed, person2 wants to push too, but will be rejected with the simple git push. Solutions:
      • git pull && git push
        • will make a merge commit in log (happens often, can get messy, avoid with rebase)
      • OR: checkout to the aux branch, then:
        • git rebase master to add master branch commits onto that aux branch
        • then checkout to master, and do a fastforward merge
    • two people have worked on the same file at the same time, person1 has already pushed
      • person2 will fail to git pull, and will see the merge conflict in git status
      • manually edit the file (your own edits will be under HEAD), git commit -a (merge commit in log), then git push
      • OR, rebase style:
        • git fetch and then…
        • git rebase
          1. moves changes to master which are not in origin/master to a temporary area
          2. runs all the origin/master commits, one at a time
          3. runs all commits in the temporary area. Conflicts will result here
        • when running into conflicts, can manually fix those, and add them, then run git rebase --continue , you’ll notice at this point (git status) that you’re not on any particular branch
    • git branch -r will list all the remote branches (that you got with git pull)
      • you can checkout to these branches
      • can get more information on branches with git remote show origin
    • if someone has deleted a remote branch that you have been working on,  and you try to push
      • you will see “Everything up to date”
      • git remote show origin will show its “stale”
      • git remote prune origin removes stale references
  • Sharing remotely
    • git remote add origin http:
      • bookmarking, giving the name origin to a remote branch (but could technically give any name)
      • can add multiple remotes
      • git remote -v will show remote repositories (-v for verbose)
    • Deleting
      • to remove remote repo: git remote rm name_of_remote
      • git push origin :name_of_remote_branch to delete remote branch
    • git pull
      1. Fetches/syncs local repo with remote one (same as git fetch). This doesn’t update any local code
      2. merges origin/master (i.e. remote) with master (i.e. local), (same as git merge origin/master), code modification happens here
    • git push -u remote_repo_name local_branch_name
      • the -u flag saves the remote repo and local branch for the next time you push
      • git push itself updates the origin/master branch
      • can push any branch this way
    • Cloning
      • can pass in a name when you clone, otherwise takes the name it had in the repo
      • automatically adds the origin remote with the url you cloned from
      • automatically checks out the initial branch
    • Excluding files
      • add it as a line in .git/info/exclude
      • can use patterns: *.mp4
      • use a .gitignore file, which contains lines like: logs/*.log
      • Untrack
        • git rm --filename (doesn’t affect working directory)
  • Tagging
    • work as references to commits
    • git tag to show all
    • git tag -a tag-name -m "tag description"
    • git push --tags must be used to push tags
    • can git checkout tag-name to checkout to a commit with a certain tag
  • Heroku
    • heroku create gives ssh address, and adds a remote repo (called heroku)
    • only deploys branch named master. git push heroku-staging local_branch:remote_branch (remote_branch should be master)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s