Isaac Sloan - Everything a Web Designer needs to know about Git!
Banner700

Everything a Web Designer needs to know about Git!

Step 1: Delete your Git GUI!

Don't use Tower because somehow you'll manage to break everything in magical ways that make no sense to me.

Step 2: Assume things!

Well I'm going to assume some things. If these things aren't true, assuming they are won't actually help on your part.

  • You're using a real operating system like OSX or Linux
  • Git is already installed on your system
  • The Git Repo already exists somewhere
  • You have a general idea how to navigate in the terminal
  • You have permission to access the remote repo either with ssh keys or a username
  • You have a folder that you keep your projects in: I'll refer to it as ~/workspace

Step 3: Cloning the Repo.

cd ~/workspace
git clone git@github.com:[owner]/[project].git #You can copy the url from your project page on github, gitlab, bitbucket, etc.
cd [project]

Step 4: Create a new Branch!

Never edit on master! Editing on master is like sleeping on train tracks: It might seem warm and comfortable but it will lead to your destruction! Before you start on any new feature or bug fix start a new branch. Short, but descriptive, names work best.

git checkout -b breaking_asset_pipeline

This will create a new branch and switch to it. If you want this branch on the remote repo you need to push it with the --upstream option. NOTE: If you don't need other computers to see this branch there's no reason to push it remote.

git push -u origin breaking_asset_pipeline

Note you can switch branches at any time with the checkout command:

git checkout [branch]

Step 4: Changing the content and existence of files.

If you add or delete a file on purpose you'll want to add that to the repo like so:

git add path/to/file #for single file
git add -A #for all files in the current directory

If you edit a file and it doesn't break everything you'll probably want to commit the changes:

git commit -am "successfully broke part of the asset pipeline. Still working on writing invalid css. The javascript was easy to ruin :)"

Please note that this only commits the code to your local branch. If you want it on the remote branch be sure to push it there:

git push

PRO TIP: If your unsure what files have been changed or just want to double check you can/should use the status command:

git status
# On branch production
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   Gemfile
#   new file:   Gemfile.lock

Step 5: Switching, Merging and Deleting Branches.

If you're satisfied with the content of your branch you'll probably want to merge it into a more permanent branch at some point. This is simply done by checking out the desired branch and merging your branch into it.

git checkout beta
git merge breaking_asset_pipeline

When a feature has been finished and merged in you may want to delete it.

# To delete it from your local box.
git branch -D breaking_asset_pipeline 
# To delete it from the remote repo.
git push origin --delete breaking_asset_pipeline
# OR
git push origin :breaking_asset_pipeline

Step 6: Reverting and Resetting.

Try not to commit bad code, but if you do it's not the end of the world, unless of course you break the master branch of the 'planets/earth' repository. That would be very very bad! Probably even worse than breaking the internet.

git revert a6e02fceb # Will reverse commit number by creating a new commit.
git revert HEAD # Will create commit to reverse most recent commit.
git revert HEAD~3 # Will revert commit 3 commits ago.

If you want to delete all changes since a commit and remove it from history you can do that to.

git reset --hard a6e02fceb
git push -f [origin master] # This will make it your most recent commit.

Step 7: Combining Commits (Squashing)

git rebase -i HEAD~3 # Will bring up the last 3 commits in an interactive vim shell.

Step ∞: Write Alias's to make your life easier.

I personally use these:

alias ga='git add -A'
alias gs='git status'
alias gm='git merge'
alias gmm='git checkout master; git merge'
alias gcm='git commit -a -m'
alias gu='git pull'
alias gp='git push'
alias gcp='git cherry-pick'
alias gc='git checkout'
alias grh='git reset --hard'
alias gcb='git checkout -b'
alias gss='git stash save'
alias gsa='git stash apply'
alias gsl='git stash lists'
September 05, 2013
osxgitlinux
comments powered by Disqus