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

Writing a Bash Shell Script

What Are Shell Scripts? In the simplest terms, a shell script is a file containing a series of commands. The shell reads this file and carries out the commands as though they have been entered directly on the command line. The shell is somewhat unique, in that it is both a powerful command line interface to the system and a scripting language interpreter. As we will see, most of the things that can be done on the command line can be done in scripts, and most of the things that can be done in scripts can be done on the command line. Writing Your First Script And Getting It To Work To successfully write a shell script, you have to do three things: Write a script Give the shell permission to execute it Put it somewhere the shell can find it Writing A Script A shell script is a file that contains ASCII text. To create a shell script, you use a text editor . A text editor is a program, like a word processor, that reads and writes ASCII text files. There are...

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...

Shell

Definition of the Shell Shell is an interactive environment which provides an interface to an Operating System. It gathers input from user and execute the commands. Bourne shell(sh)- 1977 The Bourne shell was introduced. The Bourne shell(sh), by Stephen Bourne at AT&T Bell Labs for V7 UNIX, remains a useful shell today (in some cases, as the default root shell). The Bourne shell was developed after working on an ALGOL68 compiler, so its grammar is more along the lines of Algorithmic Language (ALGOL) than other shells. The source code was developed in C. The Bourne shell served two primary goals: Executing UNIX/Linux commands for the operating system,i.e, command line interpreter Writing reusable scripts that could be invoked through the shell,i.e, scripting In addition to replacing the Thompson shell, the Bourne shell offered many other advantages over its predecessors such as control flows, loops, and variables into scripts, providing a more functional language to...