Skip to content

Commit cdd8586

Browse files
andystevensnameGuaris
authored andcommitted
[Update] Getting Started with Git (#2259)
* [Update] Getting Started with Git * Removed old copy of guide, as it was included somehow
1 parent 812590e commit cdd8586

File tree

1 file changed

+96
-14
lines changed
  • docs/development/version-control/how-to-configure-git

1 file changed

+96
-14
lines changed

docs/development/version-control/how-to-configure-git/index.md

Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ author:
55
description: 'Learn the basics of Git in this guide. Discover one of the most popular distributed version control and source code management systems that make contributing to projects and working with a team easy.'
66
keywords: ["git", "dvcs", "vcs", "scm", "gitweb", "gitolite", "ubuntu", "debian", "arch", "gentoo"]
77
license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)'
8-
modified: 2017-07-03
8+
modified: 2019-01-15
99
modified_by:
1010
name: Linode
1111
published: 2009-09-04
1212
title: Getting Started with Git
1313
external_resources:
14-
- '[Refspec Information](http://git-scm.com/book/ch9-5.html)'
14+
- '[Refspec Information](https://git-scm.com/book/en/v2/Git-Internals-The-Refspec)'
1515
- '[Learn Git with Bitbucket Cloud](https://www.atlassian.com/git/tutorials/learn-git-with-bitbucket-cloud)'
1616
- '[Pro Git Book](https://git-scm.com/book/en/v2)'
1717
- '[Github Guides](https://guides.github.com/)'
@@ -22,7 +22,7 @@ audiences: ["foundational"]
2222

2323
## What is Git?
2424

25-
Git was designed and developed by [Linus Torvalds](https://en.wikipedia.org/wiki/Linus_Torvalds) for Linux kernel development. Git provides support for non-linear, distributed development, allowing multiple contributors to work on a project simultaneously. Git is the most popular distributed version control and source code management system. This guide will walk you through the basics of getting started with Git, from installing the software to using basic commands on both local and remote repositories (repo).
25+
Git is a distributed version control system. Git was designed and developed by [Linus Torvalds](https://en.wikipedia.org/wiki/Linus_Torvalds) for Linux kernel development. Git provides support for non-linear, distributed development, allowing multiple contributors to work on a project simultaneously. Git is the most popular distributed version control and source code management system. This guide will walk you through the basics of getting started with Git, from installing the software to using basic commands on both local and remote repositories (repo).
2626

2727
## Configure Git
2828

@@ -49,24 +49,38 @@ Set your default text editor, replacing `editor-name` with your desired editor:
4949

5050
The output of `git config --list` should show echo the information you inputted:
5151

52-
MacBook-Pro:~ user$ git config --list
52+
$ git config --list
5353
user.name=exampleuser
5454
user.email=user@email.com
5555
core.editor=editor-name
5656

57-
## Work with an Existing Local Repository (Repo)
57+
## Use Git with a Local Repository
5858

59-
If you have an existing project and you want to start using Git to keep track of its changes, run `git init` from the existing project's directory:
59+
A *repository*, or repo, is a collection of files and folders and the history of their changes. Changes are tracked through *commits*, which are like snapshots of a file at various points in the file's history. These commits are not automatic, you need to manually stage a commit after each of series of file changes. Commits allow you to refer or revert back to a place in the file's timeline if there are bugs or errors in your code.
60+
61+
If you have an new or existing project and you want to start using Git to keep track of its changes, run `git init` from the existing project's directory:
6062

6163
git init
6264

63-
`git init` creates a new `.git` subdirectory in the current directory. This is where Git stores your configurations. The `git add` command tells Git to track changes of files:
65+
`git init` creates a new `.git` subdirectory in the current directory. This is where Git stores your configurations. The `git add` command tells Git to add a file to the repository and track that file's changes:
6466

6567
git add filename
6668

67-
After you have added the file, stage a commit and leave a commit message. Commit message serve as a reminder of the changes that were made to a file:
69+
After you have added the file, stage a commit and leave a commit message. Commit messages serve as a reminder of the changes that were made to a file:
70+
71+
git commit -m "Initialized a Git repository for this project. Tracking changes to a file."
72+
73+
{{< note >}}
74+
It's good practice to provide clear and descriptive commit messages for every commit you stage, as this helps collaborators to understand what a commit encompasses.
75+
{{< /note >}}
6876

69-
git commit -m "Initialized a Git repository for this project. tracking changes to a file"
77+
There may be files or folders in your project directory that you do not wish to include in your Git repository. You can include these files in a `.gitignore` file, and Git will ignore them. A sample `.gitignore` file might look like the following:
78+
79+
{{< file ".gitignore" >}}
80+
.DS_Store
81+
*.zip
82+
__doNotInclude__/
83+
{{< /file >}}
7084

7185
### Basic Git Commands
7286

@@ -78,13 +92,13 @@ This table lists basic commands, a description, and an example of the command in
7892
| `git rm` | Remove a file from a repository. | `git rm filename` |
7993
| `git mv` | Move or rename a tracked file, directory, or symlink. | `git mv file_from file_to` |
8094
| `git branch` | List all the local and remote branches. | `git branch branchname` |
81-
| `git commit` | Commit all staged objects. | `git commit -m "updates"` |
95+
| `git commit` | Commit all staged objects. Optionally, you can append a message with the `-m` flag. | `git commit -m "updates"` |
8296
| `git pull` | Download all changes from the remote repo and merge them in a specified repo file. | `git pull repo refspec` |
8397
| `git push` | Publish the changes to the remote repo. | `git push repo` |
8498

8599
### Branches
86100

87-
Branches are used for editing files without disturbing the working portions of a project. The main branch is normally named `master`, it's customary to name the branch after an issue being fixed or a feature being implemented. Because Git keep tracks of file changes, you can jump from branch to branch without overwriting or interfering with other branches in the repo.
101+
*Branches* are used for editing files without disturbing the working portions of a project. The main branch is normally named `master` and is usually reserved for clean, working code. When making changes to your code, it's customary to create a new branch and name it after the issue being fixed or the feature being implemented. Because Git keep tracks of file changes, you can jump from branch to branch without overwriting or interfering with other branches in the repo.
88102

89103
The basic options used with the `git branch` command are:
90104

@@ -96,13 +110,42 @@ The basic options used with the `git branch` command are:
96110
| -d | Delete a branch |
97111
| -r -d | Delete a remote branch |
98112

99-
## Working with Remote Repositories
113+
### Example Usage
114+
115+
Consider an application with a single `master` branch. The author of the application wants to develop a new search feature. They would add a new feature branch:
116+
117+
git branch new-search-feature
118+
119+
Then, they would switch to that branch using the `checkout` command:
120+
121+
git checkout new-search-feature
122+
123+
Now they can safely develop and commit their changes to this feature branch without altering the working code of the `master` branch. At any time they could switch back to the `master` branch:
100124

101-
Remote repositories are hosted on a network or another location on the Internet. This section provides some basic information on navigating remote Git repositories.
125+
git checkout master
126+
127+
A shortcut for creating a branch and switching to that branch is to use the `-b` flag with the `checkout` command:
128+
129+
git checkout -b new-search-feature
130+
131+
Once the new search feature is finalized, the author of the application can merge the `new-search-feature` branch into the `master` branch:
132+
133+
git checkout master
134+
git merge new-search-feature
135+
136+
Now the `master` branch has the new search feature.
137+
138+
## Use Git with a Remote Repository
139+
140+
[GitHub](https://github.com), [GitLab](https://gitlab.com), and [Bitbucket](https://bitbucket.org/) all provide ways to store Git repositories remotely and facilitate collaboration. Many of these services also include a number of other features that are vital to content development, including pull requests, continuous integration / continuous delivery pipelines (CI/CD), wikis, and webhooks. If you'd rather use a self-hosted solution, GitLab and [Gogs](https://gogs.io/) offer free locally hosted versions of their software that can easily be managed on a Linode. Check out our guides on [installing GitLab](/docs/development/version-control/install-gitlab-on-ubuntu-18-04/) and [installing Gogs](/docs/development/version-control/install-gogs-on-debian/) for more information on hosting your own remote repository software. GitHub and Bitbucket also offer paid enterprise versions of their software for local hosting. When discussing remote repositories, usually one of the aforementioned services is being referenced.
141+
142+
This section provides some basic information on navigating remote Git repositories.
102143

103144
To copy every file from a remote repository to your local system, use `git clone` followed by the remote repo's URL:
104145

105-
git clone remoteurl
146+
git clone https://github.com/linode/docs.git
147+
148+
You can typically find a remote repository's URL by clicking on the *Clone* or *Download* buttons of a remote repository's user interface.
106149

107150
To check the status of the files within the current branch of your repository, use `status`:
108151

@@ -118,6 +161,8 @@ The `remote` command will display the short names of your remote repositories. I
118161

119162
git remote -v
120163

164+
### Git Remote Repository Commands
165+
121166
Below are some basic commands for working with remote repositories:
122167

123168
| Command | Description |
@@ -129,3 +174,40 @@ Below are some basic commands for working with remote repositories:
129174
| `git remote show [remote-name]` | Display information about the remote you specified. |
130175
| `git remote rename [old-name] [new-name]` | Rename a remote. |
131176
| `git remote rm [name]` | Remove the remote you specified. |
177+
178+
### Example Usage
179+
180+
Continuing the example posed in the previous section, let's say that someone wants to add a functionality to an application in a remote repository. The first step would be to *fork* that repository. This feature, like cloning, is usually available in the user interface of the remote repository software. Forking creates a remote repository, which is a copy of the forked repository, on the user's own account. From their the user would clone the fork locally use the `clone` command discussed above.
181+
182+
git clone https://github.com/username/Spoon-Knife.git
183+
184+
Once the repository is cloned, the user would create a branch for the new feature using either the `branch` or `checkout -b` command.
185+
186+
git checkout -b new-special-feature
187+
188+
After the branch is made, the user would make the necessary updates or changes to the codebase, and commit them.
189+
190+
git commit -m "A new special feature"
191+
192+
With the commit staged, the user would then push their changes to their fork of the remote repository:
193+
194+
git push origin new-special-feature
195+
196+
The result of the push command is that now the new feature branch exists on the user's fork. To contribute those changes to the initial repository, known as the upstream repository, the user must now submit a *pull request* (PR). A PR is a feature that most remote repository software has that allows the user to safely contribute a commit, or series of commits, to an upstream repository. This is accomplished by navigating through the user interface to the pull requests page, selecting the master branch of the upstream repository as the target, and selecting the origin repository's feature branch as the source. A pull request will be created and the code will be up for review before being merged into the master branch of the upstream repository.
197+
198+
While creating PRs is a healthy standard within the development community, it is not the only way to contribute to a remote repository. You could simply push your commits directly to the upstream repository:
199+
200+
git push my-repo new-special-feature
201+
202+
The downside of this approach is that there is no option to hold the pushed commits for review. However, if you are the only contributor to a project then obviously there will be no one to review your changes, so creating a fork and contributing PRs is not necessary.
203+
204+
If you are collaborating with another developer, it is necessary to be able to retrieve their work. To do so, issue the `pull` command:
205+
206+
git checkout new-special-feature
207+
git pull
208+
209+
Git will grab the new code from the chosen remote repository branch and merge it into your local branch.
210+
211+
{{< note >}}
212+
Sometimes two developers will edit the same section of a file at the same time and attempt to merge their changes into the codebase. When this happens, Git will throw an error called a *merge conflict*. Because Git will be unable to determine which set of changes is the correct set of changes, it will prompt you to fix the merge conflict before it moves forward with the merge.
213+
{{< /note >}}

0 commit comments

Comments
 (0)