Skip to main content

Git


What is Git
By far, the most widely used modern version control system in the world today is Git. Git is a mature, actively maintained open source project originally developed in 2005 by Linus Torvalds, the famous creator of the Linux operating system kernel. A staggering number of software projects rely on Git for version control, including commercial projects as well as open source. Developers who have worked with Git are well represented in the pool of available software development talent and it works well on a wide range of operating systems and IDEs (Integrated Development Environments).


Install Git on Linux
Debian / Ubuntu (apt-get)
Git packages are available via apt:
From your shell, install Git using apt-get:
$ sudo apt-get update
$ sudo apt-get install git

Verify the installation was successful by typing git --version:
$ git --version
git version 2.9.2
Configure your Git username and email using the following commands,
$ git config --global user.name "bvp"
$ git config --global user.email "bvp@gmail.com”

What is a Git repository?
A Git repository is a virtual storage of your project. It allows you to save versions of your code, which you can access when needed. 
Initializing a new repository: git init
To create a new repo, you'll use the git init command. git init is a one-time command you use during the initial setup of a new repo. Executing this command will create a new .gitsubdirectory in your current working directory. This will also create a new master branch. 
This example assumes you already have an existing project folder that you would like to create a repo within. You'll first cd to the root project folder and then execute the git init command.
cd /path/to/your/existing/code
git init
Pointing git init to an existing project directory will execute the same initialization setup as mentioned above, but scoped to that project directory.
git init
Cloning an existing repository: git clone
If a project has already been set up in a central repository, the clone command is the most common way for users to obtain a local development clone. Like git init, cloning is generally a one-time operation. Once a developer has obtained a working copy, all version control operations are managed through their local repository.
git clone
git clone is used to create a copy or clone of remote repositories. You pass git clone a repository URL. Git supports a few different network protocols and corresponding URL formats. In this example, we'll be using the Git SSH protocol. Git SSH URLs follow a template of: git@HOSTNAME:USERNAME/REPONAME.git
where the template values match:
HOSTNAME: bitbucket.org
USERNAME: rhyolight
REPONAME: javascript-data-store
When executed, the latest version of the remote repo files on the master branch will be pulled down and added to a new folder. The new folder will be named after the REPONAME in this case javascript-data-store. The folder will contain the full history of the remote repository and a newly created master branch.
Saving changes to the repository: git add and git commit
Now that you have a repository cloned or initialized, you can commit file version changes to it. The following example assumes you have set up a project at /path/to/project. The steps being taken in this example are:
Change directories to /path/to/project
Create a new file CommitTest.txt with contents "test content for git tutorial"
git add CommitTest.txt to the repository staging area
Create a new commit with a message describing what work was done in the commit
cd /path/to/project
echo "test content for git tutorial" >> CommitTest.txt
git add CommitTest.txt
git commit -m "added CommitTest.txt to the repo"
After executing this example, your repo will now have CommitTest.txt added to the history and will track future updates to the file.
This example introduced two additional git commands: add and commit. Another common use case for git add is the --all option. Executing git add --all will take any changed and untracked files in the repo and add them to the repo and update the repo's working tree.

The git add and git commit commands compose the fundamental Git workflow. These are the two commands that every Git user needs to understand, regardless of their team’s collaboration model. They are the means to record versions of a project into the repository’s history.
Developing a project revolves around the basic edit/stage/commit pattern. First, you edit your files in the working directory. When you’re ready to save a copy of the current state of the project, you stage changes with git add. After you’re happy with the staged snapshot, you commit it to the project history with git commit. The git reset command is used to undo a commit or staged snapshot.
In addition to git add and git commit, a third command git push is essential for a complete collaborative Git workflow. git push is utilized to send the committed changes to remote repositories for collaboration. This enables other team members to access a set of saved changes.Similarly 

Git Commands

Getting & Creating Projects

Command
Description
git init
Initialize a local Git repository
git clone ssh://git@github.com/[username]/[repository-name].git
Create a local copy of a remote repository

Basic Snapshotting

Command
Description
git status
Check status
git add [file-name.txt]
Add a file to the staging area
git add -A
Add all new and changed files to the staging area
git commit -m "[commit message]"
Commit changes
git rm -r [file-name.txt]
Remove a file (or folder)

Branching & Merging

Command
Description
git branch
List branches (the asterisk denotes the current branch)
git branch -a
List all branches (local and remote)
git branch [branch name]
Create a new branch
git branch -d [branch name]
Delete a branch
git push origin --delete [branchName]
Delete a remote branch
git checkout -b [branch name]
Create a new branch and switch to it
git checkout -b [branch name] origin/[branch name]
Clone a remote branch and switch to it
git checkout [branch name]
Switch to a branch
git checkout -
Switch to the branch last checked out
git checkout -- [file-name.txt]
Discard changes to a file
git merge [branch name]
Merge a branch into the active branch
git merge [source branch] [target branch]
Merge a branch into a target branch
git stash
Stash changes in a dirty working directory
git stash clear
Remove all stashed entries

Sharing & Updating Projects

Command
Description
git push origin [branch name]
Push a branch to your remote repository
git push -u origin [branch name]
Push changes to remote repository (and remember the branch)
git push
Push changes to remote repository (remembered branch)
git push origin --delete [branch name]
Delete a remote branch
git pull
Update local repository to the newest commit
git pull origin [branch name]
Pull changes from remote repository
git remote add origin ssh://git@github.com/[username]/[repository-name].git
Add a remote repository
git remote set-url origin ssh://git@github.com/[username]/[repository-name].git
Set a repository's origin branch to SSH

Inspection & Comparison

Command
Description
git log
View changes
git log --summary
View changes (detailed)
git diff [source branch] [target branch]
Preview changes before merging



Comments

Popular posts from this blog

Introduction to Data Structures

Visit My Data Structure Blog for Programs... It is important for every Computer Science student to understand the concept of Information and how it is organized or how it can be utilized. If we arrange some data in an appropriate sequence, then it forms a Structure and gives us a meaning. This meaning is called Information . A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. Data may be organized in many different ways. The logical model of a particular organization of data in a computer is called data structure. The choice of the model based on two considerations. It should be reflect the data in the real world. It should be simple that one can effectively process the data when necessary. E.g. Array, linked list, stack, queue, tree, graph Data structure can be classified into two: Linear: A data structure is said to be linear if its elements form a sequence E.g. Array, linked list, stack, queue Non-Linear: A dat

Basic Linux Commands For Beginner's

Basic Linux Commands for Beginners Linux is an Operating System’s Kernel. You might have heard of UNIX. Well, Linux is a UNIX clone. But it was actually created by Linus Torvalds from Scratch. Linux is free and open-source, that means that you can simply change anything in Linux and redistribute it in your own name! There are several Linux Distributions, commonly called “distros”. A few of them are: Mint Ubuntu Linux Red Hat Enterprise Linux Debian Fedora Kali Linux is Mainly used in Servers. About 90% of the Internet is powered by Linux Servers. This is because Linux is fast, secure, and free! The main problem of using Windows Servers are their cost. This is solved by using Linux Servers. Forgot to mention, the OS that runs in about 80% of the Smartphones in the World, Android, is also made from the Linux Kernel. Yes, Linux is amazing! A simple example of its security is that most of the viruses in the world run on Windows, but not on Linux

KTU-FOSS LAB Solutions

Write shell scripts to show the following  ( you can write menu driven programs)  Currently logged user and his logname   ( logname, id -un, echo $USER)  Your current shell ( echo $SHELL)  Your home directory ( echo $HOME)  Your operating system type (echo $OSTYPE)  Your current path setting ( echo $PATH)  Your current working directory ( echo $PWD )  Show Currently logged  users ( w or who -H)      Show only the user name of logged users in the host ( users)      Details of last login ( last mec  ;where mec is the user id )  About your OS and version, release number, kernel version                                                 ( uname -a or  cat  /proc/version)  Show all available shells ( cat /etc/shells )  Show mouse settings (cat  /sys/class/input/mouse*/device/name )  Show computer CPU information       CPU details      ( cat /proc/cpuinfo | more )       Show information on  CPU architecture ( lscpu)       Number of Processo