Git Commands

Getting & Creating Projects

To set the author Name and Email to be used with commits

git config
   Usage:  git config –global user.name "[name]"  
   Usage:  git config –global user.email "[email address]"  

Initialize Git repository local

git init

Create local copy of remote repository

git clone ssh://git@github.com/[username]/[repository-name].git

Basic Snap shooting

Check git status

git status

Adding the file to staging area

git add [file-name.txt]

Adding all new and changed files to the staging area

git add -A 

To commit the changes

git commit -m "[commit message]"  

To Remove the file (or folder)

git rm -r [file-name.txt] 

Branching & Merging

List branches (the asterisk denotes the current branch)

 git branch 

To list all the branches (i.e : local and remote)

 git branch -a 

To create a new branch

git branch [branch name]

To delete a branch

git branch -d [branch name]

To delete a remote branch

git push origin --delete [branch name]

To create new branch and switch to it

git checkout -b [branch name] 

To clone remote branch and switch to it

git checkout -b [branch name] origin/[branch name]

To rename a local branch

git branch -m [old branch name] [new branch name] 

To switch to a branch

git checkout [branch name]

To switch to the branch last checked out

git checkout -

To discard changes to a file

git checkout -- [file-name.txt]

To merge a branch into the active branch

git merge [branch name]

To merge a branch into a target branch

git merge [source branch] [target branch]

To stash changes in a dirty working directory

git stash

To remove all stashed entries

git stash clear

Sharing & Updating Projects

To push a branch to your remote repository

git push origin [branch name]

To push changes to remote repository (and remember the branch)

git push -u origin [branch name]

TO push changes to remote repository (remembered branch)

git push

To delete a remote branch

git push origin --delete [branch name]

To update local repository to the newest commit

git pull

To pull changes from remote repository

git pull origin [branch name]

TO add a remote repository

git remote add origin ssh://git@github.com/[username]/[repository-name].git

To set a repository’s origin branch to SSH

git remote set-url origin ssh://git@github.com/[username]/[repository-name].git

Inspection & Comparison

To view changes

git log

TO view changes (detailed)

git log --summary

T0 view changes (briefly)

git log --oneline

To Preview changes before merging

git diff [source branch] [target branch]

Leave a Reply

Your email address will not be published.Required fields are marked *