Skip to content

Chapter_2

Chris McIntosh edited this page Nov 19, 2019 · 1 revision

Chapter 2

1 - Getting a Git Repository

  • git init will turn a project into a git repository
  • creates the .git subdirectory
  • git add adds files to be tracked by git
  • git clone pulls down all the repo data from the server
    • can be used to restore a server in the worst case
  • Git can use http(s) or git protocols
    • git protocol uses SSH

2 - Recording Changes

  • Tracked files are all that git knows about
  • UnTracked files are everything else in the staging area
  • git status checks the status of each file in the repo
  • git add begins tracking the new file(s)
    • new files are staged
  • Once files are modified they will be in the changed but not staged section of the status
  • A specific version of a file is staged, so it can be both staged and un-staged at different versions if you make changes to a file after staging it but before commiting the stage
  • git status --short is a shortform status
    • Two columns, staging and working directory
    • A means added, M modified, ?? Means untracked file
  • .gitignore tells git what files to ignore. Won't show up as untracked
    • Blank lines or # lines are ignored
    • Glob's work
    • start a line with / to avoid recursing
    • end line with / to specify directories
    • Negate a pattern with !
    • file follows a heirarchy like the .gitconfig file
  • git diff shows the current diff of the modified changes against the stage (or last commit if the stage is empty)
  • git diff --staged shows the diff of the staged changes against the last commit
  • git commit commits your stage and lets you view the changes and create a commit message
  • git commit -m allows you to specify the commit message on the CLI
  • git commit -a automatically stages all tracked files with changes before commiting
    • Allows you to skip the git add step if desired
  • git rm removes a files from filesystem and git tracking
  • 'git rm --cachedremoves file from staging area but not working tree (so we can keep it locally but likely add it to.gitignore`)
  • git mv moves and renames files

3 - Viewing History

  • git log shows commits in reverse chronological order
  • git log --patch shows the changes in the commit
  • git log -<n> limits the output to the last N commits
  • git log --stat shows abbreviated stats for each commit
  • git log --pretty=<format> allows you to specify commit format for parsing in scripts
  • git log --graph shows a graph of merge history, useful with --pretty
  • git log --since=<time period> limits to commits since time period
    • Can be a real date or a relative date like "2 years ago"
  • git log --until=<time period> as above only commits until the time period
  • git log --author=<author> limits commits to certain authors
  • git log --grep=<grep string> lets you search commit messages
  • git log -S <string> lets you search for commits that changed the number of occurences of that string (usually a function)
  • git log <options> -- <path> lets you limit your search for commits to a subdirectory

4 - Undoing

  • Undoing is dangerous, sometimes irreversable
  • git commit --ammend lets you change a commit
  • git reset HEAD <file> unstages a staged file
  • git checkout -- <file> throws away working directory changes
  • Most things can be recovered, but nothing can be recovered if it never made it into a commit

5 - Remotes

  • git remote -v shows remotes with url's
  • lets you pull (and sometimes push) with many different collaborators
  • git remote add <name> <url> lets you add a remote for fetch and push
  • git fetch <remote> gets the data from a remote
  • git fetch only downloads the remote data into your local repo, it doesn't merge it
  • git pull does a fetch and a merge
  • git push <remote> branch pushes your changes up to remote
  • git remote show <remote> shows you info about the remote
  • git remote rename <remote> <new name> renames a remote
  • git remote rm <remote> removes a remote

6 - Tagging

  • git tag lists tags
  • git tag -l <string> searchs for tags
  • tag types
    • lightweight, basically just a branch pointing at a specific commit that can't change
    • annotated ** full objects in database ** contain tagger name, email, date, and message ** recommended way to tag things
  • git tag -a <tag name> -m <tag message> tags the current commit as an annotated tag
  • git show <tag name> shows the tag info
  • git tag <tag name> creates a lightweight tag
    • git show will only show the commit that is referenced the LW tag
  • git tag -a <tag name> <commit sha1> tags the specified commit
  • tags are not pushed to master automatically
  • git push <remote> <tag name> pushes the specified tag to the specified remote
  • git push <remote> --tags push all tags to remote
  • git tag -d <tag name> delete the specifed tag
  • git push <remote> :refs/tags/<tag name> push null (before the colon) to the remote tag name, deleting it
  • git push <remote> --delete <tag name> delete the tag on the remote
  • git checkout <tag name> checkout the tag in a detached HEAD state
    • A state that is not on a branch
    • If you make a commit it lives nowhere but your local repo and can't be pushed
  • git checkout -b <branch> <tag name> checkout the tag to a new branch so you can make and push changes if desired

7 - Aliases

  • git config --global alias.<alias name> <command> creates the alias for the specified command
  • Run non git commands with a preceding ! IE: git config --global alias.visual '!gitk'

8 - Summary

  • Useful commands
    • git add <file|directory>
    • git clone <remote URL>
    • git status
    • git status --short
    • git diff
    • git diff --staged
    • git commit -m <message>
    • git commit -am <message>
    • git rm
    • git rm --cached
    • git log
    • git log --patch
    • git log -<n>
    • git log --stat
    • git log --pretty=<format>
    • git log --graph
    • git log --since=<time period>
    • git log --author=<author>
    • git log --grep=<grep string>
    • git log -S <string>
    • git log <options> -- <path>
    • git commit --ammend
    • git reset HEAD <file>
    • git checkout -- <file>
    • git remote -v
    • git fetch <remote>
    • git pull
    • git push <remote>
    • git remote show <remote>
    • git tag
    • git tag -l <string>
    • git tag -a <tag name> -m <message>
    • git push <remote> <tag name>
    • git push --tags <remote>
    • git tag -d <tag name>
    • git push <remote> --delete <tag name>
    • git config --global alias.<alias name> <command>