Scared of GIT?

GIT has become one of those tools in e business that you can't ignore anymore. So I suggest you learn to use the basics of the idea called GIT. For my tester friends, as you enter the automation phase, git is going to come around sooner or later. So watch out

Let's start with initializing a repo (assuming git is installed on your computer).


Before you start, just create a folder. Example "Octobox" (I am following the github office training algo). Open command prompt at that location. Now follow the commands specified here one by one. 



git init


> git init

Initialized empty Git repository in /.git/

Success!

After the above command, your directory has a hidden directory called .git which has all the info git is going to maintain.

git status


> git status

# On branch master
#
# Initial commit
#
Nothing to commit (create/copy files and use "git add" to track)

Success!

Now add a file called octabox.txt

Try "git status" again and see what changed.

# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#  octocat.txt
nothing added to commit but untracked files present (use "git add" to track)

Success!

git add


$ git add octocat.txt

Nice job, you've added octocat.txt to the Staging Area.

Quick Definition: What is staging area?

Staging Area:
A place where we can group files together before we "commit" them to Git.

Note: Another way to do the same is :

1) git add -A .   #This adds all files in current dir and also stages the files to be deleted.
2) git reset filename  #If you want to cancel the addition of a file, or all .

Let's do a git status once again.

git status

# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#  new file:   octocat.txt
#

Success!

git commit 


git commit -m 'My first commit'

git log

To see all that you have done till now, you can see a log of actions taken. Let's stop now and understand what happened here.
1.    We had a project folder (A simple empty folder).
2.    We added some files to it.
3.    We decided we want this folder in github.
4.    So we ran "git init " command.
5.    Then we decided to add the txt files inside..so we executed "git add ..." command.
6.    Then we committed with a message saying we have added some files.
7.    All this happened locally on our PC. The github website still doesn't know about this.

How to add a folder on you local PC as a new repo in gihub.

Next, the situation is, you have a friend (or more people) who want to work on the same project. Or let’s say, you want to hire people to work on your project. So they must have access to your project. Ina nut shell, remote access.
So you decide to put the project files a remote repository somewhere. So our NEXT STEP is to create a remote repo using our current local repo (The FOLDER you had).

git remote add

This command takes a remote name and a repository URL. 

Syntax : git remote add origin https://github.com/user/reponame.git

Example:  git remote add origin https://github.com/arindam31/DjangoOfficialPrac.git

Now push hard


git push -u origin master

Other important everyday command that you will need to use in your work.


How to revert to an old commit both on local and remote



git reset --hard HEAD~1

git push -f origin master

How to get all branches of a git project


git fetch

How to delete a branch from local computer


git branch -d name

How to delete a remote branches


git push origin --delete branch

How to shift changes from one branch to another



git stash 
git checkout branch
git stash pop


How to create a remote branch from a new local branch



git push -u origin new_branch

Note: -u means:  --set-upstream

Refer this. 

How to see all the existing remote branches

  git branch -r

How to fetch a specific remote branch 



This involves multiple steps. Do as follows. 

1) First get a list of all remote branches

2) Create a branch of that exact remote branch name

Popular posts from this blog

Why should you visit Kashmir sooner?

How to run a Python program over the internet?

Rajasthan: The Best backpacking destination in India