38
git for beginners Arulmurugan - Developer

Git for beginners

Embed Size (px)

DESCRIPTION

A beginners tutorial on using git with bitbucket

Citation preview

Page 1: Git for beginners

git for beginners

Arulmurugan - Developer

Page 2: Git for beginners

git

● Source code management software

● DVCS (Distributed Version Control System)

● hg - Similar tool

● CVS, Bazaar, SVN

Page 3: Git for beginners

Installation

● Ubuntu

sudo apt-get install git

● Windows

Page 4: Git for beginners

Ubuntu and Windows

● Commands here can be executed in the respective OS as defined below

● Ubuntu

○ Can use their system terminal

● Windows

○ Select git bash in your Right-click context menu

Page 5: Git for beginners

git Hosting

Bitbucket Github

Unlimited public and private repositories Unlimited Public repositories

Cost based on number of users Cost based on number of repositories

Supports git and hg Supports git only

● Codeplane, Githost, Assembla, Cloudforge, Gitorious

● Bitbucket

Username - arulmurugan

E-Mail - [email protected]

Page 6: Git for beginners

ssh-key Setup

● ssh-keygen #Command to generate public key● Copy contents of id_rsa.pub

Page 7: Git for beginners

Bitbucket - Create Repo

Page 8: Git for beginners

An empty repo

Page 9: Git for beginners

Repo - User Management● To manage who access your repository

Page 10: Git for beginners

Git configurationBasic Configurations:● git config --global user.name "second_user_name"● git config --global user.email "second_user_email"

● git config --global push.default "matching"

● git config --global color.status auto● git config --global color.branch auto

● git config --list

Optional Configurations:● git config --global branch.master.remote origin● git config --global branch.master.merge refs/heads/master

Page 11: Git for beginners

.gitignore

● Can be configured to ignore certain files and directories

● Git ignores empty directories by default

Page 12: Git for beginners

My first repo● cd ~/git/myproject/

#Open your project directory in terminal

● git init#Initiating a git repository

● git remote add origin [email protected]:arulmurugan/dummy.git#Adding remote repository URL

● git add .#Adding files to be committed

● git commit -m "Initial commit"#Committing the added files and changes

● git push origin master#Pushing the commits to remote repository

Page 13: Git for beginners

Repo with Initial commit

Page 14: Git for beginners

git status

● Will show the list changed and new files which are yet to be committed

Page 15: Git for beginners

git add & git commit

● git add .#To add all untracked files

● git add test2.html#To add specific files

● git commit -am "commit message"#To commit all changed files

● git commit -m "commit message" test1.html#To commit specific files

Page 16: Git for beginners

git add & git commit (Contd...)

Page 17: Git for beginners

Bitbucket - Source and Commits

Page 18: Git for beginners

git pull

● To pull changes pushed by other users from remote repo to local repo

Page 19: Git for beginners

Merge conflicts

● A merge conflicts occurs, if two people have modified the same content.

Page 20: Git for beginners

git mergetool

● http://meldmerge.org/

Page 21: Git for beginners

Status and History● git diff

#Shows differences in uncommitted files / last commit

● git log#Shows history of commits in the current branch

● git log --oneline --abbrev-commit#Shows one line commit history with short commit id

● git log --graph --pretty --oneline --abbrev-commit#Shows the history as graph

● git diff-tree --name-only -r <commit_id>#Shows files changed in the commit

● git show <commit_id>#Shows changes in the commit

Page 22: Git for beginners

History of file● git log [filename]

#Shows commits for the file

● git log -p [filename]#Shows commits for the file with changes

● git log --follow -p [filename]#Shows commits for the file including renames

● git blame [filename]#Shows author and commit per line of the file

● git blame -L 1,3 [filename]#Shows author and commit from line 2 to line 3

● git commit --amend -m "More changes - now correct"#To amend changes to previous commit before pushing

Page 23: Git for beginners

Remote repositories

● git remote#Show the existing remote repos

● git remote show origin#Show the details of remote repo origin

● git clone [email protected]:arulmurugan/dummy.git#To clone remote repository

Page 24: Git for beginners

git stash

● Allows to save the current uncommitted changes and checkout the last committed revision.

● Allows you to pull in the latest changes without conflicts.

● Afterwards you can restore the stashed changes, which will apply the changes to the current version of the source code.

Page 25: Git for beginners

git stash (Contd...)● git stash save "stash message"

#To save uncommited changes as stash

● git stash list#To view list of saved stashes#stash@{0}: On master: Title changed

● git stash apply stash@{0}#To apply the particular stash

● git stash drop stash@{0}#To drop particular stash

● git stash clear#To drop all saved stashes

● git stash pop#To apply most recent stash and delete it from list of stashes

Page 26: Git for beginners

git stash (Contd...)

Page 27: Git for beginners

Reverting changes● git clean - To remove newly added files

○ git clean -n#To see what would happen

○ git clean -f#To remove the files

● git checkout - To revert changed and deleted files○ git checkout .

#To revert all changed files

○ git checkout [filename]#To restore or revert the file

○ git checkout <commit_id>#To checkout a particular commit

Page 28: Git for beginners

Reverting changes (Contd...)● git reset [filename]

#To remove a file added by git add before pushing

● git checkout HEAD -- [dir_name]#To recover an accidentally deleted directory in repo

● git revert <commit_id>#To revert a particular commit

● git reset --hard HEAD~1#To delete last 1 commit

● git reset --hard <sha1-commit-id>#To delete upto a particular commit

● git push -f#Push with force to push commit deletions

Page 29: Git for beginners

Reverting changes (Contd...)

● git reflog#Shows a history of the complete changes of your current branch based on the HEAD revision

● git reset --hard <commit_id>#To revert to a commit shown in git reflog

Page 30: Git for beginners

Branches

● Branches are copies of the files from a certain commit.

● These branches can be changed independently from each other.

● The default branch is called master.

● Untracked files remain unchanged and are available in the new branch. This allows you to create a branch for unstaged and uncommitted changes at any point in time.

Page 31: Git for beginners

Branches (Contd...)● git branch

#List available branches

● git branch -a#List available branches including remote branches

● git branch -r#List remote branches only

● git branch <branch_name>#To create new branch

● git checkout <branch_name>#To switch to a branch

● git checkout -b <branch_name>#To create a branch and switch to it

Page 32: Git for beginners

Branches (Contd...)● git checkout -b <branch_name> master~1

#Create a new branch based on master without last commit

● git branch -d <branch_name>#To delete a branch

● git push origin <branch_name>#To push branch to remote

● git push origin --delete <branch_name>#To delete a remote branch

● git checkout -b <branch_name> origin/<branch_name>#To create a tracking branch

● git merge <branch_name>#To merge specified branch with current branch

Page 33: Git for beginners

Retrieving files

● git show [reference]:[filename]#To see contents of a file# [reference] can be a branch, tag, HEAD or commit ID

● git show [reference]:[filename] > [filename_copy]#To copy a file

● git log -- [filename]#To see commit history of a file, even if deleted

Page 34: Git for beginners

Alias

● Allows you to setup your own git command

● git config --global alias.st 'git status'#Set command "st" for git status#Now git st can be used instead of git status

● git config --global alias.acm '!git add . -A && git commit'#Set command "acm" for git add . and git commit#Now you can use git acm "message" for commits#Not supported in windows*

Page 35: Git for beginners

Submodules - Repos inside repos

● Allows you to include another Git repository into a Git repository

● git submodule add [URL to Git repo]#To add submodule to your repo

● After cloning a repo with submodules○ git submodule init

#Creates local configuration file for submodules○ git submodule update

#To clone submodules

Page 36: Git for beginners

● Rebase

● Patches

● Git server

Missed Out:

Page 37: Git for beginners

References:

1. http://www.vogella.com/articles/Git/article.html

2. http://git-scm.com/book/

3. http://www.kernel.org/pub/software/scm/git/docs/

Page 38: Git for beginners

Thank you

[email protected]