Saturday, August 10, 2013

Git

What is it?

  • Version control system
  • Distributed - Every one has a full local repository and full history, can avoid network latency when committing changes
  • Distributed repositories are synchronized using pulls and pushes
  • Branching is really quick in Git
  • Branch can stay at local repository or can be pushed to the remote Git

Installer

Setup Commands

# to help on a git command
git help <commandname>

# set user name and email
git config --global user.name "Your Name Here"
git config --global user.email "your_email@example.com"

# who we are user.name, user.email
git config -l

Repository Commands

# cloning a repository from a remote server
git clone <url>

# create new (empty) repository in the current directory
git init

Check-in/Commit Commands

# information about our local copy, is there anything to commit?
# what branch are we on?
git status

# pull vs fetch
# git fetch updates your remote-tracking branches under refs/remotes/<remote>/.
#    Does not change any of your own local branches under refs/heads
# git pull does a git fetch followed by a git merge
#     git pull brings a local branch (refs/heads) up-to-date with its remote version,
#    while also updating other remote-tracking branches.

# get latest
git fetch
git fetch origin
git fetch origin/master

# To add new file to repo
# Add existing file to staging area (must be added to staging area before commit)
git add <filename>

# Add all files
git add .

# diff of working copy
git diff

# diff of copy in staging area
git diff --cached
git diff --staged  # same as --cached

# commit changes that were added to staging area only
# commit is done to local repository only
# each commit gets its own version number - which is the hash of repository
#  first 8 digits of hash are shown
git commit -m "comment"

# add all changes to the staging area and commit to repository
git commit -a

# add all changes to the staging area and commit to repository with a comment

git commit -a -m "comment"

# clears the staging area
git reset

# revert all changes
# clears the staging area and undo local changes in the working copy
git reset --hard

#revert to certain commit
git reset --hard AEFXH

#revert to head of remote branch
git reset --hard origin/master

# shows history
git log

# deleting file from working tree and source control
git rm <filename>
git commit -a -m "A file was deleted"
git push origin branchname

# deleting file from source control only (leave in working tree)
git rm -cached <filename>
git commit -a -m "A file was deleted"

git push origin branchname

# removing a folder and all its contents
git rm -r <folder>

Interacting with an existing Remote Git Server

# add remote server called origin
git remote add origin <https://-connection-path>
# verify
git remote -v
git remote show origin

# to remove a remote server

git remote remove origin

# to push changes to remote server

git push origin master

# if push fails, someone has made changes we need to fetch and apply

# fetch changes by others; origin is the name of the remote
# fetch, rebase, and push if on the same branch
# do a fetch and rebase (resolve conflicts) then push INSTEAD of a pull
#   easier to change your own code to merge
git fetch origin

# applies to local copy i.e. updates the working copy from the master branch of origin remote
# undoes (rewind) our changes, gets remote changes, then applies our changes
git rebase origin/master

# send to the origin

git push origin

# if cannot rebase due to merge conflict, both versions are available to resolve the conflict
# manually
git add <filename>
git rebase origin/master -- continue

git push origin

Creating a new Remote Git Server

# In this example, we are using directory on a USB drive a  proxy for remote server
# to push changes to different server or another disk repository
# commits all day, then push at end of day push
# On the USB drive, create a directory and create a bare Git repos
# Back up to Flash drive
git init --bare

# create bare repository directory on the Server
git init bare shared

# In local git repo, add a remote
# Flash drive or another Git server are called remotes
# can have multiple remotes to push/pull
# from your machine, tell Git about the remote
git remote add myusbback F:\gitproj

# to push changes
# master is the name of the branch on the usb
# in git must have a branch; master is created by default; but myusbback was bare
git push myusbback master

#subsequent push (master was previously created, no need to specify now)
git push myusbback

Branches

# what branch are we on


git status

# shows list of local branches; the current branch has an asterisk; example, * master
git branch

# shows list of branches, local and remote; 
# the current branch has an asterisk; example, * master
git branch -a

# can create a new branch from any branch
# switch to the base branch
# create a new branch
# declare a new branch
git branch <anewbranch>

# switch to a new branch
git checkout branchname

# pushing a branch; origin is the alias of the remote repos
#  first time must specify branch name

git push origin branchname

# push all branches (specify name if want only a specific branch to be pushed)

git push origin

# pull between branches
# git pull does a git fetch followed by a git merge
# pull tries to apply changes on top our already changed files
# won't be able to always push because in wrong order
git pull
#  reapply your work on top of the incoming changes
git pull --rebase

# pull with rebase
git pull --rebase <remote> <branch>

equivalent to

git fetch <remote>
git rebase <remote>/<branch>


# to delete a branch, switch to a different branch and then delete
#  branch must have been merged to another before delete
git branch -d branchname

# force delete with prior merge from local repository using capital D
git branch -D branchname

# To remove branch from remote (notice the colon before the branch name)
git push origin :branchname
or
git push <remote_name> --delete <branch_name>

# to merge, switch to destination branch
git checkout master

# merge changes from master to branchname
git merge branchname
# update the remote (origin)
git push origin