- Working Directory: where you'll be doing all the work: creating, editing, deleting and organizing files
- Staging Area: where you'll list changes you make to the working directory
- Repository: where Git permanently stores those changes as different versions of the project
Workflow
Creating a git repository:
- Initialize git in your development folder
Commands: git init- Configure the git to set email and user name for the repository
Commands:
git config –global user.name “<my user name>”
git config –global user.email “<my email id>”
Other related commands:
git config –list
git status- Add the files to git staging folder/create a snapshot
Command: git add <file name>- Commit all changes
Commands: git commit –m “<custom message to identify the change>”
: git commit –a –m “<custom message>”
//add and commit in a single line- Steps 3 and 4 should be repeated when you make changes to the files. Check status of git file to see whether commit is required or not.
- Tag a release with a version number, when needed.
Commands: git tag –a <version number> –m “<custom message>”
Modifying/Viewing the repository
- The switching between versions can be done using checkout command
Command: git checkout <tag# or commit #>- To revert the changes back to certain commit #, you need to first go to Master
Command:
1. git checkout master
2. git revert <commit #>- To revert back uncommitted changes, use the following commands:
git reset –hard // Cleans all uncommitted changes
git clean –f // Deletes all untracked files
git reset –hard //Running again will revert everything back to previous commit- To merge changes to master:
1. Checkout master:
Command: git checkout master
2. Now, merge your branch:
Command: git merge <branch name>
3. Delete the branch after merging
Command: git branch –d <branch name>- Another way of integrating changes from one branch to another is Rebasing
Command: git rebase <branch name>
Creating/Merging Branches
- To check\list branches;
Command: git branch- To create branch:
Command: git branch <branch name>
Shortcut: git checkout –b <new branch> //creates and checks out to the new branch- To switch to a branch:
Command: git checkout <branch name>- To merge multiple branches:
Command: git checkout branch1 // (or branch2) and then
git merge branch1 branch2
Commands: Quick Reference
git init : initialize git in a project
git status : inspects the contents of the working directory and staging area
git add <filename>: add/update file to staging
git add . : add/update all files in that folder to staging
git add <filename_1> <filename_2> : add/update multiple files
git commit –m “your message” : commit permanently stores changes from staging area inside repository
git log: Track the history
git log –oneline: Give only one line history for each commits
git log <file name> : This provides the history of a specific file
git checkout HEAD filename: Discards changes in the working directory
git reset HEAD filename: Unstages file changes in the staging area
git reset SHA: Can be used to reset to a previous commit in your commit history
git branch: Lists all a Git project's branches.
git branch branch_name: Creates a new branch.
git checkout branch_name: Used to switch from one branch to another.
git merge branch_name: Used to join file changes from one branch to another.
git branch -d branch_name: Deletes the branch specified.
git clone: Creates a local copy of a remote.
git remote -v: Lists a Git project's remotes.
git fetch: Fetches work from the remote into the local copy.
git merge origin/master: Merges origin/master into your local branch.
git push origin <branch_name>: Pushes a local branch to the origin remote.
git config –list: list all properties in config file
ls –la : List all files including hidden ones
pwd: Displays the preset working directory
git config core.autocrlf false: Avoid replacing LF with CRLF in Windows Environment.