Git For Beginners

From UABgrid Documentation
(Difference between revisions)
Jump to: navigation, search
m
m (Collaboration using Git)
 
(18 intermediate revisions by one user not shown)
Line 1: Line 1:
[https://en.wikipedia.org/wiki/Git Git] is a version control system for tracking changes in computer files and coordinating work on those files among multiple people.
+
[https://en.wikipedia.org/wiki/Git Git] is a [https://en.wikipedia.org/wiki/Distributed_version_control distributed version control system] for tracking changes in computer files and coordinating work on those files among multiple people.
  
 +
== Installation ==
 +
 +
If you are using Linux or Mac , git should be installed by default. If fo any reason it hasn't been installed, you can install them using the given instructions.
 +
=== Linux ===
 +
If you’re on Fedora (or any closely-related RPM-based distribution, such as RHEL or CentOS), you can use dnf:
 +
<pre>
 +
$ sudo dnf install git-all
 +
</pre>
 +
If you’re on a Debian-based distribution, such as Ubuntu, try apt:
 +
<pre>
 +
$ sudo apt install git-all
 +
</pre>
 +
 +
=== Mac ===
 +
On a Mac, [Homebrew] can be used to install git.
 +
<pre>
 +
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
 +
brew install git
 +
</pre>
 +
 +
=== Windows ===
 +
To install Git on Windows, you can download the executable [http://git-scm.com/download/win here] or Github Desktop from [https://desktop.github.com/ here].
  
 
== Configuration ==
 
== Configuration ==
Line 16: Line 38:
 
== Initializing a git repository ==
 
== Initializing a git repository ==
  
To initialize a new git repository, run:
+
* To initialize a new git repository, run:
 
<pre>
 
<pre>
 
[ravi89@login001 Tutorial_June_2018]$ git init test
 
[ravi89@login001 Tutorial_June_2018]$ git init test
Line 23: Line 45:
 
</pre>
 
</pre>
  
To make an already existing directory, a git repo, run:
+
* To make an already existing directory, a git repo, run:
 
<pre>
 
<pre>
 
cd EXISTING_DIRECTORY
 
cd EXISTING_DIRECTORY
Line 32: Line 54:
 
Once you have made changes to the files in a git repository, you can review your edits using following commands.
 
Once you have made changes to the files in a git repository, you can review your edits using following commands.
  
To list all new or modified files to be commited:
+
* To list all new or modified files to be commited:
 
<pre>
 
<pre>
 
[ravi89@login001 Tutorial_June_2018]$ git status
 
[ravi89@login001 Tutorial_June_2018]$ git status
Line 47: Line 69:
 
</pre>
 
</pre>
  
To show file differences that have not yet been staged for a commit:
+
* To show file differences that have not yet been staged for a commit:
 
<pre>
 
<pre>
 
[ravi89@login001 Tutorial_June_2018]$ git diff
 
[ravi89@login001 Tutorial_June_2018]$ git diff
Line 61: Line 83:
 
</pre>
 
</pre>
  
To see the file differences for file that have been staged, use: '''git diff --staged'''
+
* To see the file differences for file that have been staged, use: '''git diff --staged'''
  
== Commit a file ==
+
== Creating a version for your project ==
To commit a file you first need to add the file where you have made changes, i.e. stage the file:
+
* To commit a file you first need to add the file where you have made changes, i.e. stage the file:
 
<pre>
 
<pre>
 
git add CHANGED_FILE
 
git add CHANGED_FILE
Line 70: Line 92:
 
This snapshots/stages the file in preparation for versioning.
 
This snapshots/stages the file in preparation for versioning.
  
Next commit these changes  to record file snapshots permanently in version history
+
* Next commit these changes  to record file snapshots permanently in version history
 
<pre>
 
<pre>
 
git commit -m "YOUR_COMMIT_MESSAGE"
 
git commit -m "YOUR_COMMIT_MESSAGE"
Line 78: Line 100:
 
Browse and inspect the evolution of project files
 
Browse and inspect the evolution of project files
  
To list version history for the current branch
+
* To list version history for the current branch
 
<pre>
 
<pre>
 
[ravi89@c0027 Tutorial_June_2018]$ git log
 
[ravi89@c0027 Tutorial_June_2018]$ git log
Line 101: Line 123:
 
</pre>
 
</pre>
  
To list version history for a particular file in your project:
+
* To list version history for a particular file in your project:
 
<pre>
 
<pre>
 
[ravi89@c0027 Tutorial_June_2018]$ git log --follow test1
 
[ravi89@c0027 Tutorial_June_2018]$ git log --follow test1
Line 112: Line 134:
 
</pre>
 
</pre>
  
To output metadata and content changes of the specified commit:
+
* To output metadata and content changes of the specified commit:
 
<pre>
 
<pre>
 
[ravi89@c0027 Tutorial_June_2018]$ git show 5d71077
 
[ravi89@c0027 Tutorial_June_2018]$ git show 5d71077
Line 133: Line 155:
  
 
'''NOTE:''' In the above example,  we have used first 7 characters of the commit hash, instead of the entire commit hash. Minimum number of characters required for it to be useful is 4, but it is recommended to use 6-8 characters for higher likelihood of uniqueness.
 
'''NOTE:''' In the above example,  we have used first 7 characters of the commit hash, instead of the entire commit hash. Minimum number of characters required for it to be useful is 4, but it is recommended to use 6-8 characters for higher likelihood of uniqueness.
 +
 +
== Git branches ==
 +
* To list all local branches in the current repository
 +
<pre>
 +
[ravi89@c0021 Tutorial_June_2018]$ git branch
 +
* master
 +
[ravi89@c0021 Tutorial_June_2018]$
 +
</pre>
 +
 +
* Create a new branch
 +
<pre>
 +
[ravi89@c0021 Tutorial_June_2018]$ git branch new_branch
 +
[ravi89@c0021 Tutorial_June_2018]$ git branch
 +
* master
 +
  new_branch
 +
[ravi89@c0021 Tutorial_June_2018]$
 +
</pre>
 +
 +
* Switch to the specified branch and updates the working directory
 +
<pre>
 +
[ravi89@c0021 Tutorial_June_2018]$ git checkout new_branch
 +
Switched to branch 'new_branch'
 +
[ravi89@c0021 Tutorial_June_2018]$ git branch
 +
  master
 +
* new_branch
 +
[ravi89@c0021 Tutorial_June_2018]$
 +
</pre>
 +
 +
* Combine the specified branch’s history into the current branch
 +
<pre>
 +
git merge [branch]
 +
</pre>
 +
'''Example:'''
 +
<pre>
 +
[ravi89@c0021 Tutorial_June_2018]$ git status
 +
# On branch new_branch
 +
# Changes not staged for commit:
 +
#  (use "git add <file>..." to update what will be committed)
 +
#  (use "git checkout -- <file>..." to discard changes in working directory)
 +
#
 +
# modified:  test
 +
#
 +
no changes added to commit (use "git add" and/or "git commit -a")
 +
[ravi89@c0021 Tutorial_June_2018]$ git add test
 +
[ravi89@c0021 Tutorial_June_2018]$ git commit -m "Commit to new_branch"
 +
[new_branch e6a1591] Commit to new_branch
 +
1 file changed, 1 insertion(+), 1 deletion(-)
 +
[ravi89@c0021 Tutorial_June_2018]$ git checkout master
 +
Switched to branch 'master'
 +
[ravi89@c0021 Tutorial_June_2018]$ git merge new_branch
 +
Updating 65d2065..e6a1591
 +
Fast-forward
 +
test | 2 +-
 +
1 file changed, 1 insertion(+), 1 deletion(-)
 +
[ravi89@c0021 Tutorial_June_2018]$
 +
</pre>
 +
 +
* Delete the specified branch.
 +
<pre>
 +
git branch -d [branch-name]
 +
</pre>
 +
<pre>
 +
[ravi89@c0021 Tutorial_June_2018]$ git branch
 +
* master
 +
  new_branch
 +
[ravi89@c0021 Tutorial_June_2018]$ git branch -d new_branch
 +
Deleted branch new_branch (was e6a1591).
 +
[ravi89@c0021 Tutorial_June_2018]$ git branch
 +
* master
 +
[ravi89@c0021 Tutorial_June_2018]$
 +
</pre>
 +
 +
== Suppress Tracking ==
 +
To remove a file from tracking in git, create a .gitignore file in the project directory.  It suppress accidental versioning of files and paths matching the specified patterns.
 +
 +
To list all ignored files in  the project, run:
 +
<pre>
 +
git ls-files --other --ignored --exclude-standard
 +
</pre>
 +
 +
== Collaboration using Git ==
 +
To collaborate on a project with your teammates, you can use any of the following Git repository managers:
 +
 +
[https://gitlab.rc.uab.edu/ Gitlab hosted by Research Computing ]
 +
 +
[https://github.com/ Github ]
 +
 +
[https://about.gitlab.com/ Gitlab ]
 +
 +
 +
All of them provide issue tracking features. To get more information about using Gitlab, click [https://www.tutorialspoint.com/gitlab/index.htm here].
 +
 +
== Useful Links ==
 +
* '''Cheat Sheet'''
 +
 +
A lot of the content on this page has been taken from this [https://services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf cheatsheet]
 +
 +
* To access classes provided by Software Carpentry on Git, click [https://swcarpentry.github.io/git-novice/ here].

Latest revision as of 11:58, 20 June 2018

Git is a distributed version control system for tracking changes in computer files and coordinating work on those files among multiple people.

Contents

[edit] Installation

If you are using Linux or Mac , git should be installed by default. If fo any reason it hasn't been installed, you can install them using the given instructions.

[edit] Linux

If you’re on Fedora (or any closely-related RPM-based distribution, such as RHEL or CentOS), you can use dnf:

$ sudo dnf install git-all

If you’re on a Debian-based distribution, such as Ubuntu, try apt:

$ sudo apt install git-all

[edit] Mac

On a Mac, [Homebrew] can be used to install git.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install git

[edit] Windows

To install Git on Windows, you can download the executable here or Github Desktop from here.

[edit] Configuration

To configure user information for all local repositories use the following commands:

  • Set the name you want attached to your commit transactions.
 git config --global user.name "[name]"
  • Set the email you want atached to your commit transactions
git config --global user.email "[email address]"

[edit] Initializing a git repository

  • To initialize a new git repository, run:
[ravi89@login001 Tutorial_June_2018]$ git init test
Initialized empty Git repository in /data/user/ravi89/HPC_Training/Tutorial_June_2018/test/.git/
[ravi89@login001 Tutorial_June_2018]$
  • To make an already existing directory, a git repo, run:
cd EXISTING_DIRECTORY
git init

[edit] Review changes

Once you have made changes to the files in a git repository, you can review your edits using following commands.

  • To list all new or modified files to be commited:
[ravi89@login001 Tutorial_June_2018]$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	test
nothing added to commit but untracked files present (use "git add" to track)
[ravi89@login001 Tutorial_June_2018]$
  • To show file differences that have not yet been staged for a commit:
[ravi89@login001 Tutorial_June_2018]$ git diff
diff --git a/test b/test
index 19e2dd9..d04e379 100644
--- a/test
+++ b/test
@@ -1 +1,3 @@
 Show git status
+
+Demo git diff
[ravi89@login001 Tutorial_June_2018]$ git status
  • To see the file differences for file that have been staged, use: git diff --staged

[edit] Creating a version for your project

  • To commit a file you first need to add the file where you have made changes, i.e. stage the file:
git add CHANGED_FILE

This snapshots/stages the file in preparation for versioning.

  • Next commit these changes to record file snapshots permanently in version history
git commit -m "YOUR_COMMIT_MESSAGE"

[edit] Git History

Browse and inspect the evolution of project files

  • To list version history for the current branch
[ravi89@c0027 Tutorial_June_2018]$ git log
commit 69314c321f5e56eb3f39d7c6588db3994493cbd9
Author: ravi89 <ravi89@uab.edu>
Date:   Mon Jun 18 16:48:18 2018 -0500

    Testing branch and graph

commit 5d710775910a9fbea8d06a50ced9f5d59e893589
Author: ravi89 <ravi89@uab.edu>
Date:   Mon Jun 18 16:32:42 2018 -0500

    Second commit

commit 39313b92a2cb796dae1d53bc44d0ab4b07a9a13f
Author: ravi89 <ravi89@uab.edu>
Date:   Mon Jun 18 16:29:32 2018 -0500

    First commit
[ravi89@c0027 Tutorial_June_2018]$
  • To list version history for a particular file in your project:
[ravi89@c0027 Tutorial_June_2018]$ git log --follow test1
commit 4b679447ceeae3c5c95575eab301c20b2d893b06
Author: ravi89 <ravi89@uab.edu>
Date:   Tue Jun 19 10:25:48 2018 -0500

    Adding a new file
[ravi89@c0027 Tutorial_June_2018]$
  • To output metadata and content changes of the specified commit:
[ravi89@c0027 Tutorial_June_2018]$ git show 5d71077
commit 5d710775910a9fbea8d06a50ced9f5d59e893589
Author: ravi89 <ravi89@uab.edu>
Date:   Mon Jun 18 16:32:42 2018 -0500

    Second commit

diff --git a/test b/test
index 19e2dd9..d04e379 100644
--- a/test
+++ b/test
@@ -1 +1,3 @@
 Show git status
+
+Demo git diff
[ravi89@c0027 Tutorial_June_2018]$

NOTE: In the above example, we have used first 7 characters of the commit hash, instead of the entire commit hash. Minimum number of characters required for it to be useful is 4, but it is recommended to use 6-8 characters for higher likelihood of uniqueness.

[edit] Git branches

  • To list all local branches in the current repository
[ravi89@c0021 Tutorial_June_2018]$ git branch
* master
[ravi89@c0021 Tutorial_June_2018]$
  • Create a new branch
[ravi89@c0021 Tutorial_June_2018]$ git branch new_branch
[ravi89@c0021 Tutorial_June_2018]$ git branch
* master
  new_branch
[ravi89@c0021 Tutorial_June_2018]$
  • Switch to the specified branch and updates the working directory
[ravi89@c0021 Tutorial_June_2018]$ git checkout new_branch
Switched to branch 'new_branch'
[ravi89@c0021 Tutorial_June_2018]$ git branch
  master
* new_branch
[ravi89@c0021 Tutorial_June_2018]$
  • Combine the specified branch’s history into the current branch
git merge [branch]

Example:

[ravi89@c0021 Tutorial_June_2018]$ git status
# On branch new_branch
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   test
#
no changes added to commit (use "git add" and/or "git commit -a")
[ravi89@c0021 Tutorial_June_2018]$ git add test
[ravi89@c0021 Tutorial_June_2018]$ git commit -m "Commit to new_branch"
[new_branch e6a1591] Commit to new_branch
 1 file changed, 1 insertion(+), 1 deletion(-)
[ravi89@c0021 Tutorial_June_2018]$ git checkout master
Switched to branch 'master'
[ravi89@c0021 Tutorial_June_2018]$ git merge new_branch
Updating 65d2065..e6a1591
Fast-forward
 test | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
[ravi89@c0021 Tutorial_June_2018]$
  • Delete the specified branch.
git branch -d [branch-name]
[ravi89@c0021 Tutorial_June_2018]$ git branch
* master
  new_branch
[ravi89@c0021 Tutorial_June_2018]$ git branch -d new_branch
Deleted branch new_branch (was e6a1591).
[ravi89@c0021 Tutorial_June_2018]$ git branch
* master
[ravi89@c0021 Tutorial_June_2018]$

[edit] Suppress Tracking

To remove a file from tracking in git, create a .gitignore file in the project directory. It suppress accidental versioning of files and paths matching the specified patterns.

To list all ignored files in the project, run:

git ls-files --other --ignored --exclude-standard

[edit] Collaboration using Git

To collaborate on a project with your teammates, you can use any of the following Git repository managers:

Gitlab hosted by Research Computing

Github

Gitlab


All of them provide issue tracking features. To get more information about using Gitlab, click here.

[edit] Useful Links

  • Cheat Sheet

A lot of the content on this page has been taken from this cheatsheet

  • To access classes provided by Software Carpentry on Git, click here.
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox