Git
This runbook covers following
- Important Git concepts
- Important Git commands
- Git Installation on Server Ubuntu 14.04LTS
- Git Installation on Development Machine OSX Yosemite
- Creating Repository on Github
- Filling Github repository first time
- Check out and Committing to Github using Development Machine
- Sync on Server
- Some Useful Operations to play with Git
Important Git concepts
Here are the basic terms you should familiarize yourself with before embarking on your journey.
Repository / Repo : This is the project's source code that resides on github.com's servers. You cannot modify the contents of this repository directly unless you were the one who created it in the first place.
Fork : Forking a project will create a copy of the original repository that you can modify as you please. Forked projects will appear in your own github.com account.
Cloning : this will clone an online repository to your hard drive so you may begin working on your modifications. This local copy is called your local repository.
Branch : A branch is a different version of the same project. In the case of T2DMIT, you will see 2 branches : the master branch and the development branch.
Remote : A remote is simply an alias pointing to an online repository. It is much easier to work with such aliases than typing in the complete URL of online repositories every single time.
Staging Area : Whenever you want to update your online repository (the one appearing in your github.com account), you first need to add your changes to your staging area. Modifying files locally will not automatically update your staging area's contents.
Important Git commands
Fetch : git fetch will download the current state (containing updated and newly created branches) of an online repository without modifying your local repository. It places its results in .git/FETCH_HEAD.
Merge : git merge will merge the modifications of another branch into the current working branch.
Pull : git pull is actually a combination of git fetch and git merge. It fetches the information from an online repository's branch and merges it with your local copy.
Add : Whenever you modify a file in your local repository or create a new file, that file will appear as unstaged. Calling git add allows you to specify files to be added to your staging area.
Commit : A commit records a snapshot of your staging area, making it ready to be pushed to an online repository.
Push : git push will take all of your locally committed changes and upload them to a remote repository's branch.
Don't worry if these words aren't familiar to you just yet, going through the actual process will (hopefully!) make everything clear.
Git Installation on Server Ubuntu 14.04LTS
ssh into Server using hostname and credentials, enter following commands to install Git.
• sudo apt-get update
• sudo apt-get install git
- git config --global user.name "Your Name"
- git config --global user.email "youremail@domain.com"
To see everything is fine hit following command:
Git Installation on Development Machine OS X Yosemite
Most of the time Mac comes with Git installed, just check the version by following command:
git - -version
git version 2.3.2 (Apple Git-55)
Creating Repository on Github
Goto to Github main page, Goto Repositories, click New
Repository name: "your repository name"
click "Create Repository"
Filling Github repository first time
- SSH to server, change directory to project
git init
touch .gitignore
- for case of django you don't want to share settings.py file to each developer, add following to .gitignore avoid versioning of undesired files
vi .gitignore
*.pyc
*.swp
/projectName/settings.py
git add .
- commit to local repository of server
git commit -m 'First commit’
- Add url for Github repository we created
- Verify that remote is added successfully
git remote -v
- Push the project to Github repository
git push -f origin master
Check out and Committing to Github using Development Machine
First time checking out is called cloning in git
You don't have to manually add remote after cloning, its added automatically
Now make a small change in any file and enter following commands
git pull origin master
git commit -am "This is my first commit"
git pull origin master
sudo git push origin master
Follow the above command when doing development
Sync on Server
git pull origin master
Some Useful Operations to play with Git
/* Set up Git Configuration */
git config --global user.email "you@yourdomain.com"
git config --global user.name "Your Name"
git config --global core.editor "vi"
git config --global color.ui true
/* See Git configuration */
git config --list
/* To initialise a local repository */
git init
/* Add a file to the repo */
git add
/* commit the change to git */
git commit -m "Message goes here"
/* see the commits */
git log
/* Git has a 3 Tier Architecture: Working - Staging - Repo
Changes to files are put in a Checksum SHA-1 hash 40digit value containing parent hash, author and message.
HEAD is the latest commit of the checked out branch */
/* Basic Commands */
git status /* the command 'git status' tells which files are not added or committed from Working to Staging to Repository */
git commit -m "" /* Commits and changes to all files that are in Staging into Repo */
git diff /* show changes between Working and Local Repo, no file supplied shows all files */
git diff --staged /* shows changes between Staged and Local Repo */
git rm file.txt /* will remove file from working then git commit -m "" to also remove from Repo */
git rm --cached file.txt /* leaves copy of file in Working but removes from Staging and Repo */
git mv /* rename or move files - then git commit -m "" to move to Repo */
git commit -am "text goes here" /* adds all files straight to Repo from Staging if they have changes - meaning they skip git add */
git checkout -- file.txt /* restore Repo file to Working Directory using current branch */
git reset --soft HEAD^ /* restore repo file to staging */
git reset HEAD file.txt /* Move a Stage file out of Stage back to Working */
git commit --amend -m "message" file.txt /* Change last commit to Repo (only last one can change) */
/* Reverting --soft --mixed --hard will go back to previous commits*/
git log /* gets the sha1s so you can see the coomits where you want revert back to */
git reset --soft sha /* changes Repo but not Staging or Working */
git reset --mixed sha /* changes Repo and Staging but not Working */
git reset --hard sha /* changes all 3 Tiers */
git clean -f /* remove untracked files from Working */
.gitignore /* ignores files to track in Working / track the .gitignore file */
Global Ignore /* create in home folder */
.gitignore_global
/* Add in */
.DS_Store
.Trashes
.Spotlight_V100
git config --global core.excludesfile ~/.gitignore_global /* add to gitconfig */
/* Stop tracking changes */
git rm --cached file.txt /* leaves copy in Repo and Working */
/* Track Folders changes
Add an invisble file to a folder like .gitkeeper then add and commit */
/* Commit Log */
git ls-tree HEAD
git ls-tree master
git log --oneline
git log --author="Neil"
git log --grep="temp"
/* Show Commits */
git show dc094cb /* show SHA1 */
/* Compare Commits
Branches */
git branch /* Show local branches * is the one we are on */
git branch -r /* Shows remote branches */
git branch -a /* Shows local and remote */
git branch newbranch /* creates a new branch */
git checkout newbranch /* switch to new branch */
git checkout -b oldbranch /* creates and switches to new branch */
/* Diff in Branches */
git diff master..otherbranch /* shows diff */
git diff --color-words master..otherbranch /* shows diff in color */
git branch --merged /* shows any merged branches */
/* Rename Branch */
git branch -m oldname newname
/* Delete Branch */
git branch -d nameofbranch
/* Merge Branch */
git merge branchname /* be on the receiver branch to merge the other branch */
/* Merge Conflicts between the same file on 2 branches are marked in HEAD and other branch */
git merge --abort /* Abort basically cancels the merge */
/* Manually Fix Files and commit
The Stash */
git stash save "text message here"
git stash list /* shows whats in stash */
git stash show -p stash@{0} /* Show the diff in the stash */
git stash pop stash@{0} /* restores the stash deletes the tash */
git stash apply stash@{0} /* restores the stash and keeps the stash */
git stash clear /* removes all stash */
git stash drop stash@{0}
/* Remotes
You can push and fetch to the remote server, merge any differences - then push any new to the remote - 3 branches work remote server branch, local origin master and local master
Create a repo in GitHub, then add that remote to your local repo */
git remote add origin https://github.com/neilgee/genesischild.git /* origin can be named whatever followed by the remote */
git remote /* to show all remotes */
git remote show origin /*to see remote URL*/
git remote remove origin /* to remove remote */
git remote rm origin /* to remove remote */
/* Push to Remote from Local */
git push -u origin master /* push to remote(origin) and branch(master)
/* Cloning a GitHub Repo - create and get the URL of a new repository from GitHub, then clone that to your local repo, example below uses local repo named 'nameoffolder' */
git clone https://github.com/neilgee/genesischild.git nameoffolder
/* Push to Remote from Local - more - since when we pushed the local to remote we used -u parameter then the remote branch is tracked to the local branch and we just need to use... */
git push
/* Fetch changes from a cloned Repo */
git fetch origin /* Pulls down latest committs from remote origin/master not origin, also pull down any branches pushed to Repo
Fetch before you work
Fetch before you pull
Fetch often */
/* Merge with origin/master */
git merge origin/master
git pull /* you can also do git pull which is = git fetch + git merge
Checkout/Copy a remote branch to local */
git branch branchname origin/branchname /* this will bring the remote branch to local and track with the remote */
/* Delete branch */
git branch -d branchname
/* Checkout and switch branch and track to remote */
git checkout -b nontracking origin/nontracking
/* Remove remote branch */
git push origin --delete branch