In this Blog we will see how to configure and use Git to Pull and Push Data.

Assumption:  Git is already installed in your Machine. If not, you can refer to Install Git on Linux, Mac or Windows

Download Project from Git Repository (Github/Bitbucket)

There are 2 ways to use Git => SSH/HTTPS

  • You need to add your SSH key to Github or Bitbucket Account. In order to Pull/Push repository.
  • Your key will be located in homedirectory/.ssh/id_rsa.pub
  • Use this command to print your public key.
cat ~/.ssh/id_rsa.pub


// success
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDJ8Glvp4gasoc4JvfGGMFrN51kfVQUWcWvUhlr+
fS/FV9+UNv3i7y7KIF4BXTDDpc/01+vFaZYMr1hK/4afFrMyq3LQd test@hello.local

// error
No such file or directory
  • If your Output is success i.e you already have key. Just copy that key content and add in Github/Bitbucket accout setting => Access Key. Or you might need to give this key to Admin to add in Accout.
  • If your output is error i.e No such file or directory. It means there is no key with name id_rsa.pub.
  • In order to generate ssh Key you can type ssh-keygen (mac/linux) in terminal and follow instructions.
  • For windows to generate key you can use putty or do manually like this in Command Prompt/Terminal
// Current directory
// make a directory
mkdir .ssh
// mode to directory
cd .ssh
// generate ssh key
  • At the end public and Private key will be generated in homedirectory/.ssh/ folder.
  • Now you can try printing it and add in Github/Bitbucket setting => Access Key
  • You don’t need to configure anything for HTTPS. Only catch is you will have to enter Github/Bitbucket password to Pull/Push each time.
Get Repository URL
  • Now once your method to Pull/Push is defined. We need to get Repository URl.
  • You can ask for it to admin or get it from Github/Bitbucket account if you have access to it.
  • You need to go to specific repository and Right Corner there will be Option to Clone or Download.
  • You can also select type there (SSH/HTTPS)
  • Sample Urls
// Github
https://github.com/test/Hello.git // for HTTPS
git@github.com:test/Hello.git // for SSH

// Bitbucket
https://username@bitbucket.org/companyname/Hello.git // for HTTPS
git@bitbucket.org:companyname/Hello.git // for SSH
  • Now you can Pull Repository.
git clone git@github.com:test/Hello.git
git clone https://github.com/test/Hello.git
  • Code will be cloned in the current folder you are in.

Add a new Repository to Git (Github/Bitbucket)

  • Lets say you have created a new Project and want to add it to Git(Github/Bitbucket).
  • Only thing you need to do is create an Empty Repository in Github/Bitbucket prior to Upload.
  • Once you have added an Empty Repository you can get newly create Repository URL
  • eg:
// Github
https://github.com/test/Hello.git // for HTTPS
git@github.com:test/Hello.git // for SSH

// Bitbucket
https://username@bitbucket.org/companyname/Hello.git // for HTTPS
git@bitbucket.org:companyname/Hello.git // for SSH
  • Now lets say your project is in following location /home/ubuntu/myproject.
  • You need to do following
// will initialise a .git folder in myproject folder
git init

// check what is your repository
git remote -v

// if repository is not added add using this
git remote add origin https://github.com/test/myapp.git

// if repository is different you can edit it
git remote set-url origin https://github.com/test/myapp.git

// add all files
git add .

// add commit msg
git commit -m "First commit"

// finally push files 
git push origin master
  • After push command you will get success msg saying xx files pushed.
  • If you get error check for username and email
git config --global user.name
git config --global user.email
  • If you dont get any output for name and email you need to tell git who is pushing code.
  • You can set using this
git config --global user.name "Mona Lisa"
git config --global user.email "test@gmail.com"

General Cycle to Pull Push Code

  • After you have done all changes do following
git status // see for any change in code

git add . // mark all files for commit
git add -p <file> // for a particular file

git commit -m "Commit msg" // Add Commit msg 
git pull origin master // Pull any changes 
git push origin master // Push your changes

Common Git Commands

// Assuming we are in a git Project directory.

git branch // list of all pulled branch and which is selected branch 
git status // all changed files compared to head revision
git checkout testData // pull a branch with branchname
git checkout master // pull main branch 
git checkout -b newbranch5 // create a new branch and switch to it
git add src/javaapplication1/sdfds.java // add a file to commit 
git push origin newbranch5 // push files to a branch 
git merge master // merge master branch to your current branch
git push origin master // push to main branch
git add . // mark all files for commit
git stash // save all changes for later use and revert code to git index of that branch
git stash apply // apply all changes saved in above command to current code
git mergetool // open all changed files in default merg tool application
git commit -m "Added logic for redirection" // Commit message
git remote set-url origin git@bitbucket.org:test/hello.git // set Repository url
git remote -v // see Repository url
git checkout . // reset all changed file locally 
git init // initialise a git repository
git config --global user.name // see name for pull/push
git config --global user.email // see email for pull/push
git config --global user.name "Mona Lisa" // set name
git config --global user.email "test@gmail.com" // set email
Categories: Git

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.