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
An example Git SSH URL would be: git@bitbucket.org:rhyolight/javascript-data-store.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
Post a Comment