Brett Klamer

Introduction to Git

A few things that are useful as I’m learning git.

References

Definitions

Commands

Common Tasks

Write a commit message

  1. Be consistent.
  2. Separate subject from body with a blank line.
  3. Limit subject to 50 characters and body to 72 characters.
  4. Capitalize the subject line and don’t end with a period.
  5. Use imperative mood in the subject line
    • Fix … (A bug fix)
    • Document … (Documentation only changes)
    • Add … (Add a feature or new code)
    • Remove … (Remove a feature or old code)
    • Change … (Change how a feature works)
    • Refactor … (A code change that neither fixes bugs or adds features)
    • Style … (Changes that do not affect the meaning of the code: white-space, formatting, etc)
    • Tag version … (When pushing new version)
    • Update … (Updating a text post)
  6. The body should explain the motivation for the change and contrast this with previous behavior.

Reference Chris Beams and Angular

Create a new remote repository from an existing working directory

  1. Set up the .gitignore file to ignore your desired files or folders.

  2. Open a terminal in your working directory.

    git init
    git add -A
    git commit -m 'First commit'
    git remote add origin <url>
    git remote -v
    git push origin master
    

Typical work flow

  1. Edit files in working directory.
  2. git status
  3. git add <files>
    • Use git add -p <file> to enter an interactive environment to selectively choose different ‘hunks’ to include or exclude.
      • y adds the current chunk, n skips the current chunk, and s splits the current chunk into smaller pieces so you can select the smaller component.
  4. git commit -m "message"
    • or leave off -m to pop up a text editor for the message. the first line is the ‘subject’ and the lines below are for further comments.
  5. git push origin master

Reset (delete) the git repository

  1. Delete the .git folder of the local project.
  2. Create a new git repository.
  3. git push --force -u origin master. This will force the remote repository to match the local repository.

Check for differences

Remove files or folders from the index

Undo a commit.

Create .gitignore

Tag a release version

git tag -a v0.1.0 -m 'package version 0.1.0'
git push origin v0.1.0

Split a repository

Suppose your git repository has multiple directories you wish to split into their own git repos. The script from https://github.com/vangorra/git_split will automate this process along with transferring the appropriate git history to the new repository.

Change author name and email

This will leave the commit dates unchanged, but the commit hashes will change.

git filter-branch -f --env-filter "GIT_AUTHOR_NAME='NEW-NAME'; GIT_AUTHOR_EMAIL='NEW-EMAIL'; GIT_COMMITTER_NAME='NEW-NAME'; GIT_COMMITTER_EMAIL='NEW-EMAIL';"
git push origin master --force

Change commit date to author date

The commit hashes will change.

git filter-branch -f --env-filter 'GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE; export GIT_COMMITTER_DATE'
git push origin master --force

Change commit messages

This will change the commit dates and the commit hashes. See my note on reversing commit dates if you do not want them to change. Reference http://stackoverflow.com/a/1186549.

git rebase -i --root # change 'pick' to 'e' to edit
git commit --amend
git rebase --continue

Remove a tracked file or directory after adding to .gitignore

Suppose you previously committed a file or directory and now have added it to the .gitignore. It will still be tracked by git status. If you would like git to forget about this file but are ok with the file staying in the commit history, then run the below.

# for files
git rm --cached <file>
git add .
git commit -m "Removed ignored files"

# for directories
git rm -r --cached <your directory>
git add .
git commit -m "Removed ignored directory"

Solutions for Errors

Increase buffer size

increase the git buffer size if you receive errors trying to upload over https and you have very large files.

# The number of bytes of your largest file in the directory
git config --global http.postBuffer 157286400
Published: 2014-08-01
Last Updated: 2018-04-01