Skip to content

Latest commit

 

History

History
238 lines (238 loc) · 6.57 KB

File metadata and controls

238 lines (238 loc) · 6.57 KB

Chapter 2 - Git Basics

Git Basics

Getting a Git Repository

  • Two ways to start a git project
    1. Import existing directory into git:
git init
git add * 
git commit -m 'initial project version'
  1. Clone an existing repository
    • retrieve an existing copy of a git repository
git clone git://github.com/schacon/grit.git 'name of local folder to reside in'
  • You can use a number of different protocols: git, http(s), etc.

Recording Changes to the Repository

  • Files can either be tracked or untracked
  • tracked files are files that were present in the last snapshot
    • these files can be unmodified, modified or staged
  • untracked files are everything else

Checking status of your files

  • To check status:
git status

Tracking new files

  • To start tracking an untracked file:
git add <fileToTrack/directory>

Staging modified files

  • git add is also used to stage modified files
  • if you stage a file, modify the same file, and then run git status you will see the file mentioned twice. Once mentioning the staged file to be committed and once for changes that have been made but not staged. You must git add the file again to stage the latest changes.

Ignoring files

  • Create a .gitignore file in the project:
# a comment - this is ignored
# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the root TODO file, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .t

Viewing your staged and unstaged changes

  • To shows exact lines added and removed for a file that has been changed but not staged:
git diff
  • To show changes that you have staged:
git diff --staged

Committing Your Changes

  • Only staged files are committed:
git commit
  • the -m flag allows you to add a commit message inline
  • the -a flag allows you to skip the staging area

Removing files

  • To remove a file from git:
git rm
  • To remove a file from the staging area:
git rm --staged <fileToRemove>
  • You can pass files, directories and file-glob pattern to git rm

Moving files

  • git does not explicitly track file movement
  • You can rename a file as follows:
git mv fileFrom fileTo

Viewing the Commit History

  • default command is:
git log
  • to show diff for each change use a -p option
  • to limit to the last x commits use -{x} were x is a number
  • for a word diff use: –word-diff option
  • to see stats use: –stat
  • to display in different format use: –pretty:
git log --pretty=oneline
git log --pretty=short
git log --pretty=full
git log --pretty=fuller
  • you can define your own format:
git log --pretty=format:%h - %an, %ar : %s

Undoing Things

Changing your last commit

  • You can use the command:
git commit --amend
  • if there are no files in the staging area since your last commit, then only your last commit message will be replaced
  • If you forgot to commit a file then:
git commit -m 'initial commit'
git add forgotten_file
git commit --amend

Unstaging a staged file

  • To unstage a staged file:
git reset HEAD <file>

Unmodifying a Modified File

  • To revert a modified file:
git checkout -- <file>

Working with Remotes

  • remote repositories are repositories that are hosted on some network

Showing Your Remotes

  • to show remote servers (-v flag shows URL associated with that server):
git remote
  • one repository can have more than one remote in which case git remote will list them all

Adding Remote Repositories

  • To explicitly add a remote repository:
git remote add <shortname> <url>
  • after running above you can reference the repository by using short-name instead of the url

Fetching and Pulling from Your Remotes

  • To get data from remote:
git fetch <remoteName>
  • The above will pull all data from the remote since you last fetched the data or since you cloned the repository
  • It will not merge the changes
  • If you want to fetch the data and have it automatically merged run:
git pull

Pushing to Your Remote

  • To push your changes upstream use:
git push <remoteName> <branchName>
  • For example:
git push origin master
  • The above will only work if no one else had pushed their changes after you last did a fetch.

Inspecting a Remote

  • To get useful information about a remote:
git remote show origin

Removing and Renaming Remotes

  • To rename a remote’s shortname:
git remote rename
  • This will change the remote branch name as well
  • To remove a reference:
git remote rm <shortName>

Tagging

  • Git has the ability to tag specific point in history as being important

Listing tags

  • to list tags:
git tag
  • you can filter tags like so:
git tag -l 'step-1*'

Creating Tags

  • Two different types:
    1. Lightweight - Like a branch that doesn’t change. Basically a pointer to a specific commit
    2. Annotated - Stored as full objects in Git database. They’re checksummed, contain the tagger name, email and date, have a tagging message, and can be signed and verified with GNU privacy guard

Annotated Tags

  • To create a tag:
git tag -a v1.4 -m 'my version 1.4'
  • the -a flag will create a tag and the -m is for an inline comment
  • To see tag info:
git show v1.4

Lightweight Tags

  • Nothing more than a commit checksum stored in a file
  • Run git tag without any flags – just specify name of tag:
git tag v1.4-lw

Tagging Later

  • You can tag a specific commit even if you moved past it by specifying the checksum or part of the checksum:
git tag -a v1.5 -m 'my version 1.5' 9fceb02

Sharing Tags

  • by default the git push command does not transfer tags to remote server
  • You must be explicit:
git push origin <tagName>
  • To transfer all tags:
git push origin --tags