GIT Commands

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.

Install GIT
For window:
Install GIT software from one of the below url.
https://git-scm.com/download/win
https://www.sourcetreeapp.com/

For Ubuntu:
$ sudo apt-get install git

Configure GIT account on your system
Open git command console and run two commands. Replace email and username of your git in the command.
git config –global user.email “email address”
git config –global user.name “password”

SSH Key Generate
ssh-keygen -t ed25519 -C “jha.sanjeev.in@gmail.in”
ssh-keygen -o -t rsa -b 4096 -C “jha.sanjeev.in@gmail.in”

Clone GIT branch
This is the first command to initialize the project

Go to github
Go to branch from where you want to clone
Click on ‘clone or download’ button
Copy ‘clone with https’ URL e.g. https://github.com/……../…….git
Create a directory on your system and go to the new directory
Run below command:
$ git clone https://github.com/……../…….git

GIT checkout
You can switch one branch to another branch
eg. git checkout dev[branch name]
$ git checkout design

GIT Add
You can add file to GIT
$ git add .

GIT commit
$ git commit -m ‘added files’

GIT Push
Push the branch on git :
$ git push origin [name_of_your_new_branch]
eg. git push origin dev

GIT Pull
Pull the changes from git to your system :
$ git pull

GIT Remove
$ git rm filename
Please don’t remove file or folder without git commands.

GIT Status
This is the very very useful command, so you can use this command before using any command to know exactly where are you. To heck updated code and also confirmation of which branch you stand
$ git status

List Branches
You can see all branches created by using :
$ git branch

Create the branch
Create the branch on your local machine and switch in this branch :

$ git checkout -b [name_of_your_new_branch]
$ git checkout -b development

Push the branch on git:
$ git push origin [name_of_your_new_branch]
$ git push origin development

Git command for testing with sequence
– git init
– git clone
– git checkout branch_name
– Add test file like test.txt
– git add .
– git commit -m ‘add test file’
– git push origin branch_name
– Put username and password of git when asked
– Check new added test.txt file on branch of git

GIT Stash
Stashing takes the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time.

To check stashes
$ git stash list

and shows all the files and changes for respective id
$ git stash show stash@{0}

and list with the date of the stash creation
$ git stash list –date=local

To apply the git stash,
$ git stash apply stash@{n}
$ git stash apply stash@{0}

Create a new repository
git clone git@gitlab.com:sanjeev.jha/example.git
cd b2c
touch README.md
git add README.md
git commit -m “add README”
git push -u origin master

Push an existing folder
cd existing_folder
git init
git remote add origin git@gitlab.com:sanjeev.jha/example.git
git add .
git commit -m “Initial commit”
git push -u origin master

Push an existing Git repository
cd existing_repo
git remote rename origin old-origin
git remote add origin git@gitlab.com:sanjeev.jha/example.git
git push -u origin –all
git push -u origin –tags

Thanks