This tutorial will quickly walk through the basics of Git.
What is Git?
Git is a distributed version-control system. In a distributed version-control system a developer follows the following basic workflow:
- The distributed repository is cloned by every developer. A developer clones it from another developer or from a central location (if there are a lot of developers then a central location is preferred).
- Once a developer has made a group of changes by committing changes to the local repository, he/she then pushes the changes to another developer or to the central location. Another developer can also pull your commits to see the changes.
- A developer's local repository is merged with the pulled commits. Git attempts to merge changes automatically, if there are conflicts then the developer resolves them manually.
This tutorial shows how to create a new repository and use the basic git commands.
Downloading and installing
Follow this quick tutorial to install Git on Windows. On Linux, git can be install by the following command:
sudo apt install git
Working with Git
Creating new repository
New repository can be created by command:
git init
Let's create an example directory 'example-project':
D:>mkdir example-project
D:>cd example-project
Creating git repository
D:\example-project>git init Initialized empty Git repository in D:/example-project/.git/
D:\example-project>dir /a /b .git
The folder .git is where the repository is stored. It contains the database of all metadata and version history etc.
Checking status
Following command shows working tree status. It also suggests the possible actions to perform next:
git status
D:\example-project>git status On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
Adding new files
A new file can be added to git by using:
git add <theFile>
This command changes the state of a new file from untracked to tracked + staged.
An untracked file is not monitored by git. A tracked file is monitored by git. A staged file is committable i.e. to commit a file it must be staged first.
Creating a new file 'myfile.txt':
D:\example-project>echo hello> myfile.txt
Checking status:
D:\example-project>git status On branch master
No commits yet
Untracked files: (use "git add <file>..." to include in what will be committed)
myfile.txt
nothing added to commit but untracked files present (use "git add" to track)
Adding the file to the git staging:
D:\example-project>git add myfile.txt
Checking status again:
D:\example-project>git status On branch master
No commits yet
Changes to be committed: (use "git rm --cached <file>..." to unstage)
new file: myfile.txt
Listing files tracked by git
git ls-files
D:\example-project>git ls-files myfile.txt
Commit changes
To commit changes to local repository:
git commit -m "my change description"
D:\example-project>git commit -m "first change"
*** Please tell me who you are.
Run
git config --global user.email "you@example.com" git config --global user.name "Your Name"
to set your account's default identity. Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'Joe@jpc.(none)')
If this is the first time your are committing, you will get the above message and commit will fail. Follow the instructions and enter your name and email address:
D:\example-project>git config --global user.name "Joe"
D:\example-project>git config --global user.email "joe@example.com"
Try committing again:
D:\example-project>git commit -m "first change" [master (root-commit) 50abb3c] first change 1 file changed, 1 insertion(+) create mode 100644 myfile.txt
Checking status:
D:\example-project>git status On branch master nothing to commit, working tree clean
When committing we can specify file(s) as well:
git commit myfile.txt -m "first change' //single file
git commit file1 file2 ... -m "my message" //many files
git commit -m "my message" //all files in current directory
Commit history
git log
D:\example-project>git log commit 50abb3cf5d95776b018e68c2dacfb9083147c43a Author: Joe <joe@example.com> Date: Tue Nov 20 18:28:50 2018 -0600
first change
Make more changes
D:\example-project>echo more text >> myfile.txt
Since our file myfile.txt was previously staged, it is still tracked by git. So git knows that the file has changes.
Viewing difference
git diff <file-name>
D:\example-project>git diff myfile.txt diff --git a/myfile.txt b/myfile.txt index ce01362..f038e11 100644 --- a/myfile.txt +++ b/myfile.txt @@ -1 +1,2 @@ hello +more text
Checking status:
D:\example-project>git status On branch master 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: myfile.txt
no changes added to commit (use "git add" and/or "git commit -a")
Adding to git again:
D:\example-project>git add myfile.txt
Question: Why do we need to add the file again? Answer: we need to use 'add' command in two scenarios: - to make untracked file to tracked and staged. - to make already tracked but modified files to be staged again.
Following diagram summarizes it:
The purpose of staging
The 'add' command only adds the content of the specified file(s) to staging at the time the add command is run.
After new changes to a file which was previously committed, we need to use 'add' command again only if we want the new changes to be included in coming commit. In other words, we selectively stage just those changes we want to be part of our next commit.
We can add multiple files to staging area. When we think all new 'staged' changes are ready, we can commit them together.
Let's continue with our example, and check the status:
D:\example-project>git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)
modified: myfile.txt
Committing:
D:\example-project>git commit -m "second change" [master 892d3d6] second change 1 file changed, 1 insertion(+)
Log:
D:\example-project>git log commit 892d3d660b4e0027fe18b0cd9b6cdd616dc546b5 Author: Joe <joe@example.com> Date: Tue Nov 20 18:30:08 2018 -0600
second change
commit 50abb3cf5d95776b018e68c2dacfb9083147c43a Author: Joe <joe@example.com> Date: Tue Nov 20 18:28:50 2018 -0600
first change
Work Directory vs Git Directory
The staging data and committed data both are stored in
.git folder. This folder is generally called
Git Directory.
The folder where our working files reside, is called
Working Directory or
Git Work Tree.
Git Help
git help
D:\example-project>git help usage: git [--version] [--help] [-C <path>] [-c <name>=<value>] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path] [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare] [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] <command> [<args>]
These are common Git commands used in various situations:
start a working area (see also: git help tutorial) clone Clone a repository into a new directory init Create an empty Git repository or reinitialize an existing one
work on the current change (see also: git help everyday) add Add file contents to the index mv Move or rename a file, a directory, or a symlink reset Reset current HEAD to the specified state rm Remove files from the working tree and from the index
examine the history and state (see also: git help revisions) bisect Use binary search to find the commit that introduced a bug grep Print lines matching a pattern log Show commit logs show Show various types of objects status Show the working tree status
grow, mark and tweak your common history branch List, create, or delete branches checkout Switch branches or restore working tree files commit Record changes to the repository diff Show changes between commits, commit and working tree, etc merge Join two or more development histories together rebase Reapply commits on top of another base tip tag Create, list, delete or verify a tag object signed with GPG
collaborate (see also: git help workflows) fetch Download objects and refs from another repository pull Fetch from and integrate with another repository or a local branch push Update remote refs along with associated objects
'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept.
To list all git commands:
git help -a
D:\example-project>git help -a usage: git [--version] [--help] [-C <path>] [-c <name>=<value>] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path] [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare] [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] <command> [<args>]
available git commands in 'D:/Program Files/Git/mingw64/libexec/git-core'
add get-tar-commit-id rebase--interactive add--interactive grep receive-pack am gui reflog annotate gui--askpass remote apply gui--askyesno remote-ext archive gui.tcl remote-fd askpass hash-object remote-ftp bisect help remote-ftps bisect--helper http-backend remote-http blame http-fetch remote-https branch http-push repack bundle imap-send replace cat-file index-pack request-pull check-attr init rerere check-ignore init-db reset check-mailmap instaweb rev-list check-ref-format interpret-trailers rev-parse checkout legacy-rebase revert checkout-index legacy-stash rm cherry log send-email cherry-pick ls-files send-pack citool ls-remote serve clean ls-tree sh-i18n--envsubst clone mailinfo shortlog column mailsplit show commit merge show-branch commit-graph merge-base show-index commit-tree merge-file show-ref config merge-index stage count-objects merge-octopus stash credential merge-one-file status credential-manager merge-ours stripspace credential-store merge-recursive submodule credential-wincred merge-resolve submodule--helper cvsexportcommit merge-subtree subtree cvsimport merge-tree svn daemon mergetool symbolic-ref describe mktag tag diff mktree unpack-file diff-files mv unpack-objects diff-index name-rev update diff-tree notes update-git-for-windows difftool p4 update-index difftool--helper pack-objects update-ref fast-export pack-redundant update-server-info fast-import pack-refs upload-archive fetch patch-id upload-pack fetch-pack prune var filter-branch prune-packed verify-commit fmt-merge-msg pull verify-pack for-each-ref push verify-tag format-patch quiltimport web--browse fsck range-diff whatchanged fsck-objects read-tree worktree gc rebase write-tree
git commands available from elsewhere on your $PATH
flow lfs
'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept.
To get help on a particular command, use --help option
D:\example-project>git help --help
That opens a help web page:
D:\example-project>git ls-files --help
Instead of using: git <command-name> --help we can also use: git help <command-name>
D:\example-project>git help commit
Concept guides
git help -g can be used to get the list of some concept guides:
D:\example-project>git help -g
The common Git guides are: attributes Defining attributes per path cli Git command-line interface and conventions core-tutorial A Git core tutorial for developers cvs-migration Git for CVS users diffcore Tweaking diff output everyday A useful minimum set of commands for Everyday Git glossary A Git Glossary hooks Hooks used by Git ignore Specifies intentionally untracked files to ignore modules Defining submodule properties namespaces Git namespaces repository-layout Git Repository Layout revisions Specifying revisions and ranges for Git tutorial A tutorial introduction to Git tutorial-2 A tutorial introduction to Git: part two workflows An overview of recommended workflows with Git
'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept.
Let's get help on 'tutorial'
D:\example-project>git help tutorial
|