an introduction to git

31
Introduction to GIT Muhil

Upload: muhil-vannan

Post on 28-Jul-2015

198 views

Category:

Software


0 download

TRANSCRIPT

Introduction to GIT

Muhil

What is GIT

• Version control system

• decentralised by nature but can be used as centralised by convention Repo

Repo

Repo

Git Lifecycle• Create

• Create a local repo or clone from existing

• Create a remote repo for centralised sharing (also called bare repositories)

• configure repository details, gitignore, remote repos

• Collaborate

• fetch,pull,commit,push changes

• branching

• merging vs rebasing

• Maintain

• iterate collaboration further sprints with tagging versions and releases

• optimize

Create

Bare Repo vs Repo

Bare repo Repo

No working copy working copy checked out in the directory

cannot edit directly edit commit and push changes

usually used for centralised sharing for repositories

used for development on local boxes

git init --bare {directory name}

git init {directory name}

Remote Repository Services

• Used to host Bare repositories

• Main usage is as centralised repos for sharing

• Can be an online service

• Bitbucket, Github, AWS CodeCommit

• Can be a local service

• Gitlab

Create a local repo• One time operation

• To create a repository in current directory

• git init

• To create a repository in specified directory

• git init {directory name}

• This creates and checks out a empty repository to which files can be added.

• a hidden directory called .git in the specified or working directory as applicable

Add a remote repo• If the local repo is a new repo then we might need to add the remote repo setup for

sharing

• git remote add {remotename} {remote path/URL}

• example

• git remote add origin https://github.com/user/repo.git

• we can have multiple remote repos to a local repo

• change a remote URL

• git remote set-url {remotename} {new remote url}

• rename a remote

• git remote rename {old name} {new name}

• list all remotes

• git remote -v

Clone a repo• A repo can be cloned from a local machine or from a remote location to a

specified location

• the command structure is

• git clone <repo path> <local directory name>

• this can be done over ssh / https for remote repos and just by giving the path when cloning in the same machine.

• if no local directory name is specified it clones into the current working directory as with the above examples

• If need to clone to a specific directory , then need to specify the folder name at the end of the command

• git clone https://github.com/username/repo.git /path/to/custom-directory-name

Configuring a Git repo

• The command structure usually is

• git config <option> <parameter> <value>

• the option can specify scope which can be local, global or system

• local is for that repo, whereas global is for that user and system is system wide

• git config --global user.email <email>

• git config --global user.name <name>

• local overrides global, global overrides system

• local config is in .git/config

• global is in ~/.gitconfig

• system is in /etc/gitconfig

Configuring a Git repo

• We can do a variety of manipulations with git config command

• some examples

• Tell Git who you are

• git config --global user.name "John Smith"

• git config --global user.email [email protected]

• Select your favourite text editor

• git config --global core.editor vim

• Ignore file permission changes for committing

• git config --global core.fileMode false

.gitignore• this is used to ignore any files/folder from the repository

• a .gitignore file can have a rule ignoring a specific file or all files with an extension.

• It can be used even to ignore itself (i don’t recommend it though)

• example entries in the gitignore file

• debug.log

• *.log

• img/*

• !img/logo.png

• Highly useful for ignoring user uploaded content from filling up the repo and keep the repo manageable

Collaborate

Committing changes• We first need to stage any changes before

committing.

• this can be done for all files changed

• git add .

• for specific files by

• git add {specific file name}

• To commit the changes

• git commit -m ‘message for commit’

To track and update changes from/to remote

• check status of current local repo

• git status

• to update tracking from remote repo

• git fetch

• to pull changes from remote repo

• git pull {remote name} {branch name}

• to push changes

• git push {remote name} {branch name}

• git push --all

• git push --dry-run

Amending, Reverting and Resetting a commit

• To amend a commit

• git commit --amend

• reverting

• when reverting it undoes a commit’s changes and creates a new commit

• git revert <commit>

• safer

• resetting

• resets your working copy to a particular commit undoing any other changes you have done

• git reset [--soft --hard] <commit>

• like in rebasing dangerous as public shared commits can get modified causing issues

• to discard any working copy changes resetting this is useful

• git reset --hard

Committing a deleted file

• Sometimes you need to delete a file from the repo

• This doesn’t get automatically staged when you do git add .

• Need to specify that you need to stage deleted files as well before committing

• git add -u .

Branching• A variety of strategies available

• create branches for bugfixes, development of new features and any and all changes to codebase

• create new branch

• git branch {branch name}

• git checkout {branch name}

• these can be done with a single command as well

• git checkout -b {branch name}

• switch to an existing branch

• git checkout {branch name}

• list branches

• git branch (for local only)

• git branch -r (for remote only)

• git branch -a (for both)

Merging• The branches need to be merged to the

master branch after testing for release.

• to merge

• switch to the branch that is to be merged into (ex) master

• git checkout master

• merge the branch (ex) hotfix1223

• git merge hotfix1223

• to merge Pull requests can also be used for an administrator to verify the changes in Remote repos

• when merging we might run into conflicts

Merge Conflicts• Do a git status to find which file is

conflicting and open it

• A conflicting file will look like the example on the side

• A conflict occurs when there are changes from two branches on the same line

• in the example the footer line has been changed on master and on the hot fix branch

• All to more reason to NEVER do changes on the master branch directly

• Also conflicts may occur when merging a branch from a different developer to your own branch

<<<<<<< HEAD:index.html

<div id="footer">contact : [email protected]</div>

=======

<div id="footer">

please contact us at [email protected]

</div>

>>>>>>> hotfix1223:index.html

Fixing Conflicts<<<<<<< HEAD:index.html to=======

is the block on the master/ current branch

======= to >>>>>>> hotfix1223:index.html

is the block on the branch to merged

• these need to be replaced with the text as required.

• the replacing text can be from the current branch or from the branch being merged or a combination of both

• make sure

<<<<<<< HEAD:index.html

=======

>>>>>>> hotfix1223:index.html

are removed fro the code as depicted in the right column

• save and commit

<<<<<<< HEAD:index.html

<div id="footer">contact : [email protected]</div>

=======

<div id="footer">

please contact us at [email protected]

</div>

>>>>>>> hotfix1223:index.html

<div id="footer">

please contact us at [email protected]

</div>

replace with

Fixing Conflicts• There are graphical tools for fixing merge

conflicts as well

• git mergetool

• you might need to configure the tool if not already

• git config --global merge.tool {mergetool name}

• kdiff3 and extmetge are very good tools

Rebasing• Rebasing is the process of

moving a branch to a new base commit

• Useful to do locally

• NOT SAFE for public history repositories, as the history might be modified and cause issues with other developers

• The golden rule of git rebase is to never use it on public branches

• I would recommend Merge instead of rebase just to eliminate this issue so as to never mistakenly change it

Maintain

Tagging• With git you can tag an commit for easy finding

and downloading

• also useful for tagging release versions

• git tag -a {tag} -m ‘message'

• git tag -a {tag} {commit number}

• To checkout a branch and a tagged commit

• git checkout -b [branchname] [tagname]

Tagging• to list tags

• git tag

• push tags to remote

• git push {remote name} --tags

• delete a tag

• git tag -d {tag}

• git push {remote name} --tags

Stashing

• Files not ready to be committed can be stashed.

• git stash

• git stash list

• git stash apply

Optimize a Git repo

• used to optimise git repo and clean up loose objects

• sometimes run along with usual git commands

• can also be run manually

• 'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune] [—force]

GUI tools for git

• Sourcetree - mac & windows

• Tower - mac

• GitEye - linux

• Github for windows

• Github for mac

Things to note• Git doesn’t maintain a folder structure if

nothing underneath it is gitted.

• files called empty should be added to the folder and the repo for this purpose

• File permissions are usually gitted along with the files and if you want to ignore any changes to file permissions the repo needs to be configured

Useful Documentation Links

• https://help.github.com/articles/good-resources-for-learning-git-and-github/

• https://www.atlassian.com/git/tutorials/setting-up-a-repository

• http://git-scm.com/doc

• https://git-scm.com/downloads/guis