Git

Learning how to use git from trygit, Pro Git and other different sources.

Lesson 3

Udacity – How to Use Git and GitHub

Lesson 3

Github

Syncing Repositories

  • remote repositories vs local repositories
  • create remote
    • + new repositorie
    • uncheck initialize
  • git remote add (name) (url)
    • if there is only one remote, standard to use origin as name
    • url is at the github repository page
  • git remote -v
    • v stands for verbose and means that git remote will output more information
  • git push (remote) (branch)
    • to send changes to the remote
    • remote is the name of the remote (e.g. origin)
    • branch is the name of the branch to push (e.g. master)
  • git remote set-url origin url
    • will reset the url of the git repository
  • git pull origin master
    • when master has more update than the origin, use this command to pull update into remote.

Forking a Repository

  • Fork link back to the original
  • Fork track how many people forked
  • Branch happen in single repository (e.g. local)
  • Clone is taking a existing repository and create an identical one to it (e.g. local or remote)
  • Fork is like clone but only within Github.
  • Click Fork to fork a repository
    • git clone url
      • to clone the repository into local
      • once clone, the local repository is pointing to github
  • Add collaborator
    • go to setting
    • click collaborator
    • add github username

Merging Remote Changes

  • git fetch remote-name
    • fetch all branches from the given remote
  • git fetch
    • fetches all branches from the origin remote
  • git pull origin master
    • is the same as two steps
    • git fetch master
    • git merge master origin/master
  • git branch -a
    • to see all (-a) branches both local and remote branche
    • without -a will only see local branches
  • git push origin master
    • to push change to github
  • Fast-foward merges (using pull)
    • when you merge two commits, where one is the ancestor of the other. Or one commit is reachable by the other.  (e.g. o <-a <- b, merge b into a)
  • git branch branch-name
    • to create a new branch
  • git checkout branch-name
    • to checkout the new branch
  • git push origin branch-name
    • to push remote to a new branch-name
  • Use pull request to see what changes made in the branch
    • click the branch
    • click pull request
    • edit which fork to compare
    • click create pull request
    • click merge pull request is present if there is no conflict
    • a pull request is more like a merge request

Lesson 2

Udacity – How to Use Git and GitHub

Lesson 2

Repositories

  • git init to create a new git repository. There is no commit when create.
  • git log to see what commit has been done at anytime
  • git status show which file has changed since the last commit
  • git add to add file to staging are for next commit
  • git commit will commit the file in staging (-m option to add with a message / comment)
  • git diff without argument compare working directory with staging area
  • git diff –staged compare staging area file with most recent commit in repository
  • git reset –hard to discard any changes in working directory and staging area (cannot reverse this)
  • git checkout master will leave ‘detached head’ state and put you back in the lastest commit.

Branches

  • git branch (no argument) show all branches (* is the one currently checkout)
  • git branch (with an argument) create a new branch
  • git checkout [branch name] to check out a branch
  • git log –graph –oneline [branch..] will show a graph of how each branch connect to each other.
  • git checkout -b [new branch name] is equivalent to run both git branch [new branch name] and then git checkout [new branch name]

Merge

  • if both file have the same line, keep that line
  • if original file have it and one of the file deleted it, then delete it
  • if original file don’t have it and one of the file added it, then add it
  • git merge branchid1 branchid2 to merge branchid2 into branchid1
  • git show commit to show the changes between a commit and its parent
  • git branch -d branchid to delete a branch label
  • git log -n 1 will only show one commit

Lesson 1

Udacity – How to Use Git and GitHub

Lesson 1

File compare

  • fc for Window command prompt
  • diff for Mac and Linux command prompt (-u for unified format)

Git Commands

  • git log – view history of commit (–stat statistic of each file).
  • git diff <id1> <id2>- compare different commit
  • git –version
  • git clone <link> – copy entire history of the repository
  • git config –global color.ui auto (get colored diff output, –global for all git projects)
  • git checkout <id> – to run a previous commit (detached HEAD state, where HEAD is the commit you are currently on).
  • git config –global user.name <user string> (must do this before allow commit)
  • git config –global user.email <email string> (must do this before allow commit)
  • git config –global core.editor <text editor> (setup default text editor. e.g vim)
  • git config –global push.default upstream (?)
  • git config –global merge.conflictstyle diff3 (?)

Guide lines and tutorials

  • One commit per logical change
  • wiki page for installation