mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-12 09:44:24 +02:00
Git: removed some clutter
This commit is contained in:
@@ -8,17 +8,17 @@ contributors:
|
|||||||
filename: LearnGit.txt
|
filename: LearnGit.txt
|
||||||
---
|
---
|
||||||
|
|
||||||
Git is a distributed version control and source code management system.
|
Git is a distributed version control and source code management system.
|
||||||
|
|
||||||
It does this through a series of snapshots of your project, and it works
|
It does this through a series of snapshots of your project, and it works
|
||||||
with those snapshots to provide you with functionality to version and
|
with those snapshots to provide you with functionality to version and
|
||||||
manage your source code.
|
manage your source code.
|
||||||
|
|
||||||
## Versioning Concepts
|
## Versioning Concepts
|
||||||
|
|
||||||
### What is version control?
|
### What is version control?
|
||||||
|
|
||||||
Version control is a system that records changes to a file, or set of files, over time.
|
Version control is a system that records changes to a file(s), over time.
|
||||||
|
|
||||||
### Centralized Versioning VS Distributed Versioning
|
### Centralized Versioning VS Distributed Versioning
|
||||||
|
|
||||||
@@ -42,8 +42,9 @@ Version control is a system that records changes to a file, or set of files, ove
|
|||||||
|
|
||||||
### Repository
|
### Repository
|
||||||
|
|
||||||
A set of files, directories, historical records, commits, and heads. Imagine it as a source code data structure,
|
A set of files, directories, historical records, commits, and heads. Imagine it
|
||||||
with the attribute that each source code "element" gives you access to its revision history, among other things.
|
as a source code data structure, with the attribute that each source code
|
||||||
|
"element" gives you access to its revision history, among other things.
|
||||||
|
|
||||||
A git repository is comprised of the .git directory & working tree.
|
A git repository is comprised of the .git directory & working tree.
|
||||||
|
|
||||||
@@ -54,32 +55,33 @@ The .git directory contains all the configurations, logs, branches, HEAD, and mo
|
|||||||
|
|
||||||
### Working Tree (component of repository)
|
### Working Tree (component of repository)
|
||||||
|
|
||||||
This is basically the directories and files in your repository. It is often referred to
|
This is basically the directories and files in your repository. It is often
|
||||||
as your working directory.
|
referred to as your working directory.
|
||||||
|
|
||||||
### Index (component of .git dir)
|
### Index (component of .git dir)
|
||||||
|
|
||||||
The Index is the staging area in git. It's basically a layer that separates your working tree
|
The Index is the staging area in git. It's basically a layer that separates your working tree
|
||||||
from the Git repository. This gives developers more power over what gets sent to the Git
|
from the Git repository. This gives developers more power over what gets sent
|
||||||
repository.
|
to the Git repository.
|
||||||
|
|
||||||
### Commit
|
### Commit
|
||||||
|
|
||||||
A git commit is a snapshot of a set of changes, or manipulations to your Working Tree.
|
A git commit is a snapshot of a set of changes, or manipulations to your Working
|
||||||
For example, if you added 5 files, and removed 2 others, these changes will be contained
|
Tree. For example, if you added 5 files, and removed 2 others, these changes
|
||||||
in a commit (or snapshot). This commit can then be pushed to other repositories, or not!
|
will be contained in a commit (or snapshot). This commit can then be pushed to
|
||||||
|
other repositories, or not!
|
||||||
|
|
||||||
### Branch
|
### Branch
|
||||||
|
|
||||||
A branch is essentially a pointer that points to the last commit you made. As you commit,
|
A branch is essentially a pointer to the last commit you made. As you go on
|
||||||
this pointer will automatically update and point to the latest commit.
|
committing, this pointer will automatically update to ooint the latest commit.
|
||||||
|
|
||||||
### HEAD and head (component of .git dir)
|
### HEAD and head (component of .git dir)
|
||||||
|
|
||||||
HEAD is a pointer that points to the current branch. A repository only has 1 *active* HEAD.
|
HEAD is a pointer that points to the current branch. A repository only has 1 *active* HEAD.
|
||||||
head is a pointer that points to any commit. A repository can have any number of heads.
|
head is a pointer that points to any commit. A repository can have any number of heads.
|
||||||
|
|
||||||
###Stages of Git
|
### Stages of Git
|
||||||
* Modified - Changes have been made to a file but file has not been committed to Git Database yet
|
* Modified - Changes have been made to a file but file has not been committed to Git Database yet
|
||||||
* Staged - Marks a modified file to go into your next commit snapshot
|
* Staged - Marks a modified file to go into your next commit snapshot
|
||||||
* Committed - Files have been committed to the Git Database
|
* Committed - Files have been committed to the Git Database
|
||||||
@@ -95,7 +97,7 @@ head is a pointer that points to any commit. A repository can have any number of
|
|||||||
|
|
||||||
### init
|
### init
|
||||||
|
|
||||||
Create an empty Git repository. The Git repository's settings, stored information,
|
Create an empty Git repository. The Git repository's settings, stored information,
|
||||||
and more is stored in a directory (a folder) named ".git".
|
and more is stored in a directory (a folder) named ".git".
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -144,8 +146,8 @@ $ git init --help
|
|||||||
|
|
||||||
### status
|
### status
|
||||||
|
|
||||||
To show differences between the index file (basically your working copy/repo) and the current
|
To show differences between the index file (basically your working copy/repo)
|
||||||
HEAD commit.
|
and the current HEAD commit.
|
||||||
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -172,7 +174,8 @@ $ git add /path/to/file/HelloWorld.c
|
|||||||
$ git add ./*.java
|
$ git add ./*.java
|
||||||
```
|
```
|
||||||
|
|
||||||
This only adds a file to the staging area/index, it doesn't commit it to the working directory/repo.
|
This only adds a file to the staging area/index, it doesn't commit it to the
|
||||||
|
working directory/repo.
|
||||||
|
|
||||||
### branch
|
### branch
|
||||||
|
|
||||||
@@ -205,7 +208,8 @@ Updates all files in the working tree to match the version in the index, or spec
|
|||||||
$ git checkout
|
$ git checkout
|
||||||
# Checkout a specified branch
|
# Checkout a specified branch
|
||||||
$ git checkout branchName
|
$ git checkout branchName
|
||||||
# Create a new branch & switch to it, like: "git branch <name>; git checkout <name>"
|
# Create a new branch & switch to it
|
||||||
|
# equivalent to "git branch <name>; git checkout <name>"
|
||||||
$ git checkout -b newBranch
|
$ git checkout -b newBranch
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -268,7 +272,7 @@ $ git config --global alias.g "grep --break --heading --line-number"
|
|||||||
$ git grep 'variableName' -- '*.java'
|
$ git grep 'variableName' -- '*.java'
|
||||||
|
|
||||||
# Search for a line that contains "arrayListName" and, "add" or "remove"
|
# Search for a line that contains "arrayListName" and, "add" or "remove"
|
||||||
$ git grep -e 'arrayListName' --and \( -e add -e remove \)
|
$ git grep -e 'arrayListName' --and \( -e add -e remove \)
|
||||||
```
|
```
|
||||||
|
|
||||||
Google is your friend; for more examples
|
Google is your friend; for more examples
|
||||||
@@ -303,7 +307,7 @@ $ git merge --no-ff branchName
|
|||||||
|
|
||||||
### mv
|
### mv
|
||||||
|
|
||||||
Rename or move a file
|
Rename or move a file
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Renaming a file
|
# Renaming a file
|
||||||
@@ -338,7 +342,7 @@ $ git pull origin master --rebase
|
|||||||
Push and merge changes from a branch to a remote & branch.
|
Push and merge changes from a branch to a remote & branch.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Push and merge changes from a local repo to a
|
# Push and merge changes from a local repo to a
|
||||||
# remote named "origin" and "master" branch.
|
# remote named "origin" and "master" branch.
|
||||||
# git push <remote> <branch>
|
# git push <remote> <branch>
|
||||||
# git push => implicitly defaults to => git push origin master
|
# git push => implicitly defaults to => git push origin master
|
||||||
@@ -347,23 +351,25 @@ $ git push origin master
|
|||||||
# To link up current local branch with a remote branch, add -u flag:
|
# To link up current local branch with a remote branch, add -u flag:
|
||||||
$ git push -u origin master
|
$ git push -u origin master
|
||||||
# Now, anytime you want to push from that same local branch, use shortcut:
|
# Now, anytime you want to push from that same local branch, use shortcut:
|
||||||
$ git push
|
$ git push
|
||||||
```
|
```
|
||||||
|
|
||||||
### stash
|
### stash
|
||||||
|
|
||||||
Stashing takes the dirty state of your working directory and saves it on a stack of unfinished changes that you can reapply at any time.
|
Stashing takes the dirty state of your working directory and saves it on a stack
|
||||||
|
of unfinished changes that you can reapply at any time.
|
||||||
|
|
||||||
Let's say you've been doing some work in your git repo, but you want to pull from the remote.
|
Let's say you've been doing some work in your git repo, but you want to pull
|
||||||
Since you have dirty (uncommited) changes to some files, you are not able to run `git pull`.
|
from the remote. Since you have dirty (uncommited) changes to some files, you
|
||||||
Instead, you can run `git stash` to save your changes onto a stack!
|
are not able to run `git pull`. Instead, you can run `git stash` to save your
|
||||||
|
changes onto a stack!
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ git stash
|
$ git stash
|
||||||
Saved working directory and index state \
|
Saved working directory and index state \
|
||||||
"WIP on master: 049d078 added the index file"
|
"WIP on master: 049d078 added the index file"
|
||||||
HEAD is now at 049d078 added the index file
|
HEAD is now at 049d078 added the index file
|
||||||
(To restore them type "git stash apply")
|
(To restore them type "git stash apply")
|
||||||
```
|
```
|
||||||
|
|
||||||
Now you can pull!
|
Now you can pull!
|
||||||
@@ -410,7 +416,7 @@ Now you're ready to get back to work on your stuff!
|
|||||||
|
|
||||||
[Additional Reading.](http://git-scm.com/book/en/v1/Git-Tools-Stashing)
|
[Additional Reading.](http://git-scm.com/book/en/v1/Git-Tools-Stashing)
|
||||||
|
|
||||||
### rebase (caution)
|
### rebase (caution)
|
||||||
|
|
||||||
Take all changes that were committed on one branch, and replay them onto another branch.
|
Take all changes that were committed on one branch, and replay them onto another branch.
|
||||||
*Do not rebase commits that you have pushed to a public repo*.
|
*Do not rebase commits that you have pushed to a public repo*.
|
||||||
|
Reference in New Issue
Block a user