mirror of
https://github.com/linkedin/school-of-sre
synced 2026-01-20 23:48:03 +00:00
docs (level 101): fix typos, punctuation, formatting (#160)
* docs: formatted for readability * docs: rephrased and added punctuation * docs: fix typos, punctuation, formatting * docs: fix typo and format * docs: fix caps and formatting * docs: fix punctuation and formatting * docs: capitalized SQL commands, fixed puntuation, formatting * docs: fix punctuation * docs: fix punctuation and formatting * docs: fix caps,punctuation and formatting * docs: fix links, punctuation, formatting * docs: fix code block formatting * docs: fix punctuation, indentation and formatting
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# Working With Branches
|
||||
|
||||
Coming back to our local repo which has two commits. So far, what we have is a single line of history. Commits are chained in a single line. But sometimes you may have a need to work on two different features in parallel in the same repo. Now one option here could be making a new folder/repo with the same code and use that for another feature development. But there's a better way. Use _branches._ Since git follows tree like structure for commits, we can use branches to work on different sets of features. From a commit, two or more branches can be created and branches can also be merged.
|
||||
Coming back to our local repo which has two commits. So far, what we have is a single line of history. Commits are chained in a single line. But sometimes you may have a need to work on two different features in parallel in the same repo. Now one option here could be making a new folder/repo with the same code and use that for another feature development. But there's a better way. Use _branches_. Since git follows tree-like structure for commits, we can use branches to work on different sets of features. From a commit, two or more branches can be created and branches can also be merged.
|
||||
|
||||
Using branches, there can exist multiple lines of histories and we can checkout to any of them and work on it. Checking out, as we discussed earlier, would simply mean replacing contents of the directory (repo) with the snapshot at the checked out version.
|
||||
|
||||
@@ -13,7 +13,7 @@ $ git log --oneline --graph
|
||||
* df2fb7a adding file 1
|
||||
```
|
||||
|
||||
We create a branch called `b1`. Git log tells us that b1 also points to the last commit (7f3b00e) but the `HEAD` is still pointing to master. If you remember, HEAD points to the commit/reference wherever you are checkout to. So if we checkout to `b1`, HEAD should point to that. Let's confirm:
|
||||
We create a branch called `b1`. Git log tells us that `b1` also points to the last commit (`7f3b00e`) but the `HEAD` is still pointing to `master`. If you remember, `HEAD` points to the commit/reference wherever you are checkout to. So if we checkout to `b1`, `HEAD` should point to that. Let's confirm:
|
||||
|
||||
```bash
|
||||
$ git checkout b1
|
||||
@@ -23,7 +23,7 @@ $ git log --oneline --graph
|
||||
* df2fb7a adding file 1
|
||||
```
|
||||
|
||||
`b1` still points to the same commit but HEAD now points to `b1`. Since we create a branch at commit `7f3b00e`, there will be two lines of histories starting this commit. Depending on which branch you are checked out on, the line of history will progress.
|
||||
`b1` still points to the same commit but `HEAD` now points to `b1`. Since we create a branch at commit `7f3b00e`, there will be two lines of histories starting this commit. Depending on which branch you are checked out on, the line of history will progress.
|
||||
|
||||
At this moment, we are checked out on branch `b1`, so making a new commit will advance branch reference `b1` to that commit and current `b1` commit will become its parent. Let's do that.
|
||||
|
||||
@@ -44,7 +44,7 @@ $ git log --oneline --graph
|
||||
$
|
||||
```
|
||||
|
||||
Do note that master is still pointing to the old commit it was pointing to. We can now checkout to master branch and make commits there. This will result in another line of history starting from commit 7f3b00e.
|
||||
Do note that master is still pointing to the old commit it was pointing to. We can now checkout to `master` branch and make commits there. This will result in another line of history starting from commit `7f3b00e`.
|
||||
|
||||
```bash
|
||||
# checkout to master branch
|
||||
@@ -66,7 +66,7 @@ $ git log --oneline --graph
|
||||
* df2fb7a adding file 1
|
||||
```
|
||||
|
||||
Notice how branch b1 is not visible here since we are on the master. Let's try to visualize both to get the whole picture:
|
||||
Notice how branch `b1` is not visible here since we are on the `master`. Let's try to visualize both to get the whole picture:
|
||||
|
||||
```bash
|
||||
$ git log --oneline --graph --all
|
||||
@@ -77,13 +77,13 @@ $ git log --oneline --graph --all
|
||||
* df2fb7a adding file 1
|
||||
```
|
||||
|
||||
Above tree structure should make things clear. Notice a clear branch/fork on commit 7f3b00e. This is how we create branches. Now they both are two separate lines of history on which feature development can be done independently.
|
||||
Above tree structure should make things clear. Notice a clear branch/fork on commit `7f3b00e`. This is how we create branches. Now they both are two separate lines of history on which feature development can be done independently.
|
||||
|
||||
**To reiterate, internally, git is just a tree of commits. Branch names (human readable) are pointers to those commits in the tree. We use various git commands to work with the tree structure and references. Git accordingly modifies contents of our repo.**
|
||||
|
||||
## Merges
|
||||
|
||||
Now say the feature you were working on branch `b1` is complete and you need to merge it on master branch, where all the final version of code goes. So first you will checkout to branch master and then you pull the latest code from upstream (eg: GitHub). Then you need to merge your code from `b1` into master. There could be two ways this can be done.
|
||||
Now say the feature you were working on branch `b1` is complete and you need to merge it on `master` branch, where all the final version of code goes. So first, you will `checkout` to branch `master` and then you `pull` the latest code from `upstream` (eg: GitHub). Then you need to merge your code from `b1` into `master`. There could be two ways this can be done.
|
||||
|
||||
Here is the current history:
|
||||
|
||||
@@ -96,7 +96,7 @@ $ git log --oneline --graph --all
|
||||
* df2fb7a adding file 1
|
||||
```
|
||||
|
||||
**Option 1: Directly merge the branch.** Merging the branch b1 into master will result in a new merge commit. This will merge changes from two different lines of history and create a new commit of the result.
|
||||
**Option 1: Directly merge the branch.** Merging the branch `b1` into `master` will result in a new merge commit. This will merge changes from two different lines of history and create a new commit of the result.
|
||||
|
||||
```bash
|
||||
$ git merge b1
|
||||
@@ -114,9 +114,9 @@ $ git log --oneline --graph --all
|
||||
* df2fb7a adding file 1
|
||||
```
|
||||
|
||||
You can see a new merge commit created (8fc28f9). You will be prompted for the commit message. If there are a lot of branches in the repo, this result will end-up with a lot of merge commits. Which looks ugly compared to a single line of history of development. So let's look at an alternative approach
|
||||
You can see a new merge commit created (`8fc28f9`). You will be prompted for the commit message. If there are a lot of branches in the repo, this result will end-up with a lot of merge commits. Which looks ugly compared to a single line of history of development. So let's look at an alternative approach.
|
||||
|
||||
First let's [reset](https://git-scm.com/docs/git-reset) our last merge and go to the previous state.
|
||||
First, let's [reset](https://git-scm.com/docs/git-reset) our last merge and go to the previous state.
|
||||
|
||||
```bash
|
||||
$ git reset --hard 60dc441
|
||||
@@ -129,7 +129,7 @@ $ git log --oneline --graph --all
|
||||
* df2fb7a adding file 1
|
||||
```
|
||||
|
||||
**Option 2: Rebase.** Now, instead of merging two branches which has a similar base (commit: 7f3b00e), let us rebase branch b1 on to current master. **What this means is take branch `b1` (from commit 7f3b00e to commit 872a38f) and rebase (put them on top of) master (60dc441).**
|
||||
**Option 2: Rebase.** Now, instead of merging two branches which has a similar base (commit: `7f3b00e`), let us rebase branch `b1` on to current master. **What this means is take branch `b1` (from commit `7f3b00e` to commit `872a38f`) and rebase (put them on top of) master (`60dc441`).**
|
||||
|
||||
```bash
|
||||
# Switch to b1
|
||||
|
||||
@@ -3,14 +3,14 @@
|
||||
## Prerequisites
|
||||
|
||||
1. Have Git installed [https://git-scm.com/downloads](https://git-scm.com/downloads)
|
||||
2. Have taken any git high level tutorial or following LinkedIn learning courses
|
||||
2. Have taken any git high-level tutorial or following LinkedIn learning courses
|
||||
- [https://www.linkedin.com/learning/git-essential-training-the-basics/](https://www.linkedin.com/learning/git-essential-training-the-basics/)
|
||||
- [https://www.linkedin.com/learning/git-branches-merges-and-remotes/](https://www.linkedin.com/learning/git-branches-merges-and-remotes/)
|
||||
- [The Official Git Docs](https://git-scm.com/doc)
|
||||
|
||||
## What to expect from this course
|
||||
|
||||
As an engineer in the field of computer science, having knowledge of version control tools becomes almost a requirement. While there are a lot of version control tools that exist today like SVN, Mercurial, etc, Git perhaps is the most used one and this course we will be working with Git. While this course does not start with Git 101 and expects basic knowledge of git as a prerequisite, it will reintroduce the git concepts known by you with details covering what is happening under the hood as you execute various git commands. So that next time you run a git command, you will be able to press enter more confidently!
|
||||
As an engineer in the field of computer science, having knowledge of version control tools becomes almost a requirement. While there are a lot of version control tools that exist today like SVN, Mercurial, etc, Git perhaps is the most used one and this course we will be working with Git. While this course does not start with Git 101 and expects basic knowledge of git as a prerequisite, it will reintroduce the git concepts known by you with details covering what is happening under the hood as you execute various `git` commands. So that next time you run a `git` command, you will be able to press `enter` more confidently!
|
||||
|
||||
## What is not covered under this course
|
||||
|
||||
@@ -49,11 +49,11 @@ $ ls .git/
|
||||
HEAD config description hooks info objects refs
|
||||
```
|
||||
|
||||
There are a bunch of folders and files in the `.git` folder. As I said, all these enables git to do its magic. We will look into some of these folders and files. But for now, what we have is an empty git repository.
|
||||
There are a bunch of folders and files in the `.git` folder. As I said, all these enable git to do its magic. We will look into some of these folders and files. But for now, what we have is an empty git repository.
|
||||
|
||||
### Tracking a File
|
||||
|
||||
Now as you might already know, let us create a new file in our repo (we will refer to the folder as _repo_ now.) And see git status
|
||||
Now as you might already know, let us create a new file in our repo (we will refer to the folder as _repo_ now.) And see `git status`:
|
||||
|
||||
```bash
|
||||
$ echo "I am file 1" > file1.txt
|
||||
@@ -70,7 +70,7 @@ Untracked files:
|
||||
nothing added to commit but untracked files present (use "git add" to track)
|
||||
```
|
||||
|
||||
The current git status says `No commits yet` and there is one untracked file. Since we just created the file, git is not tracking that file. We explicitly need to ask git to track files and folders. (also checkout [gitignore](https://git-scm.com/docs/gitignore)) And how we do that is via `git add` command as suggested in the above output. Then we go ahead and create a commit.
|
||||
The current git status says `No commits yet` and there is one untracked file. Since we just created the file, git is not tracking that file. We explicitly need to ask git to track files and folders. (Also checkout [gitignore](https://git-scm.com/docs/gitignore)) And how we do that is via `git add` command as suggested in the above output. Then, we go ahead and create a commit.
|
||||
|
||||
```bash
|
||||
$ git add file1.txt
|
||||
@@ -90,7 +90,7 @@ $ git commit -m "adding file 1"
|
||||
create mode 100644 file1.txt
|
||||
```
|
||||
|
||||
Notice how after adding the file, git status says `Changes to be committed:`. What it means is whatever is listed there, will be included in the next commit. Then we go ahead and create a commit, with an attached messaged via `-m`.
|
||||
Notice how after adding the file, `git status` says `Changes to be committed:`. What it means is whatever is listed there, will be included in the next commit. Then, we go ahead and create a commit, with an attached message via `-m`.
|
||||
|
||||
### More About a Commit
|
||||
|
||||
@@ -123,7 +123,7 @@ $ git log --oneline --graph
|
||||
|
||||
`git log`, as the name suggests, prints the log of all the git commits. Here you see two additional arguments, `--oneline` prints the shorter version of the log, ie: the commit message only and not the person who made the commit and when. `--graph` prints it in graph format.
|
||||
|
||||
**Now at this moment the commits might look like just one in each line but all commits are stored as a tree like data structure internally by git. That means there can be two or more children commits of a given commit. And not just a single line of commits. We will look more into this part when we get to the Branches section. For now this is our commit history:**
|
||||
**Now at this moment, the commits might look like just one in each line but all commits are stored as a tree like data structure internally by git. That means there can be two or more children commits of a given commit. And not just a single line of commits. We will look more into this part when we get to the Branches section. For now, this is our commit history:**
|
||||
|
||||
```bash
|
||||
df2fb7a ===> 7f3b00e
|
||||
@@ -131,7 +131,7 @@ $ git log --oneline --graph
|
||||
|
||||
### Are commits really linked?
|
||||
|
||||
As I just said, the two commits we just made are linked via tree like data structure and we saw how they are linked. But let's actually verify it. Everything in git is an object. Newly created files are stored as an object. Changes to file are stored as an objects and even commits are objects. To view contents of an object we can use the following command with the object's ID. We will take a look at the contents of the second commit
|
||||
As I just said, the two commits we just made are linked via tree like data structure and we saw how they are linked. But let's actually verify it. Everything in git is an object. Newly created files are stored as an object. Changes to file are stored as an objects and even commits are objects. To view contents of an object, we can use the following command with the object's ID. We will take a look at the contents of the second commit:
|
||||
|
||||
```bash
|
||||
$ git cat-file -p 7f3b00e
|
||||
@@ -143,7 +143,7 @@ committer Sanket Patel <spatel1@linkedin.com> 1603273316 -0700
|
||||
adding file 2
|
||||
```
|
||||
|
||||
Take a note of `parent` attribute in the above output. It points to the commit id of the first commit we made. So this proves that they are linked! Additionally you can see the second commit's message in this object. As I said all this magic is enabled by `.git` folder and the object to which we are looking at also is in that folder.
|
||||
Take a note of `parent` attribute in the above output. It points to the commit id of the first commit we made. So this proves that they are linked! Additionally, you can see the second commit's message in this object. As I said all this magic is enabled by `.git` folder and the object to which we are looking at also is in that folder.
|
||||
|
||||
```bash
|
||||
$ ls .git/objects/7f/3b00eaa957815884198e2fdfec29361108d6a9
|
||||
@@ -154,7 +154,7 @@ It is stored in `.git/objects/` folder. All the files and changes to them as wel
|
||||
|
||||
### The Version Control part of Git
|
||||
|
||||
We already can see two commits (versions) in our git log. One thing a version control tool gives you is ability to browse back and forth in history. For example: some of your users are running an old version of code and they are reporting an issue. In order to debug the issue, you need access to the old code. The one in your current repo is the latest code. In this example, you are working on the second commit (7f3b00e) and someone reported an issue with the code snapshot at commit (df2fb7a). This is how you would get access to the code at any older commit
|
||||
We already can see two commits (versions) in our git log. One thing a version control tool gives you is ability to browse back and forth in history. For example: some of your users are running an old version of code and they are reporting an issue. In order to debug the issue, you need access to the old code. The one in your current repo is the latest code. In this example, you are working on the second commit (`7f3b00e`) and someone reported an issue with the code snapshot at commit (`df2fb7a`). This is how you would get access to the code at any older commit.
|
||||
|
||||
```bash
|
||||
# Current contents, two files present
|
||||
@@ -181,13 +181,13 @@ $ ls
|
||||
file1.txt
|
||||
```
|
||||
|
||||
So this is how we would get access to old versions/snapshots. All we need is a _reference_ to that snapshot. Upon executing `git checkout ...`, what git does for you is use the `.git` folder, see what was the state of things (files and folders) at that version/reference and replace the contents of current directory with those contents. The then-existing content will no longer be present in the local dir (repo) but we can and will still get access to them because they are tracked via git commit and `.git` folder has them stored/tracked.
|
||||
So this is how we would get access to old versions/snapshots. All we need is a _reference_ to that snapshot. Upon executing `git checkout ...`, what git does for you is use the `.git` folder, see what was the state of things (files and folders) at that version/reference and replace the contents of current directory with those contents. The then-existing content will no longer be present in the local dir (repo) but we can and will still get access to them because they are tracked via `git commit` and `.git` folder has them stored/tracked.
|
||||
|
||||
### Reference
|
||||
|
||||
I mention in the previous section that we need a _reference_ to the version. By default, git repo is made of tree of commits. And each commit has a unique IDs. But the unique ID is not the only thing we can reference commits via. There are multiple ways to reference commits. For example: `HEAD` is a reference to current commit. _Whatever commit your repo is checked out at, `HEAD` will point to that._ `HEAD~1` is reference to previous commit. So while checking out previous version in section above, we could have done `git checkout HEAD~1`.
|
||||
|
||||
Similarly, master is also a reference (to a branch). Since git uses tree like structure to store commits, there of course will be branches. And the default branch is called `master`. Master (or any branch reference) will point to the latest commit in the branch. Even though we have checked out to the previous commit in out repo, `master` still points to the latest commit. And we can get back to the latest version by checkout at `master` reference
|
||||
Similarly, `master` is also a reference (to a branch). Since git uses tree like structure to store commits, there of course will be branches. And the default branch is called `master`. Master (or any branch reference) will point to the latest commit in the branch. Even though we have checked out to the previous commit in out repo, `master` still points to the latest commit. And we can get back to the latest version by `checkout` at `master` reference
|
||||
|
||||
```bash
|
||||
$ git checkout master
|
||||
@@ -218,7 +218,7 @@ $ cat .git/refs/heads/master
|
||||
7f3b00eaa957815884198e2fdfec29361108d6a9
|
||||
```
|
||||
|
||||
Viola! Where master is pointing to is stored in a file. **Whenever git needs to know where master reference is pointing to, or if git needs to update where master points, it just needs to update the file above.** So when you create a new commit, a new commit is created on top of the current commit and the master file is updated with the new commit's ID.
|
||||
Viola! Where `master` is pointing to is stored in a file. **Whenever git needs to know where master reference is pointing to, or if git needs to update where master points, it just needs to update the file above.** So when you create a new commit, a new commit is created on top of the current commit and the master file is updated with the new commit's ID.
|
||||
|
||||
Similary, for `HEAD` reference:
|
||||
|
||||
@@ -239,7 +239,7 @@ $ git log --oneline --graph
|
||||
* df2fb7a adding file 1
|
||||
```
|
||||
|
||||
Now let's change master to point to the previous/first commit.
|
||||
Now, let's change `master` to point to the previous/first commit.
|
||||
|
||||
```bash
|
||||
$ echo df2fb7a61f5d40c1191e0fdeb0fc5d6e7969685a > .git/refs/heads/master
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
# Git with GitHub
|
||||
|
||||
Till now all the operations we did were in our local repo while git also helps us in a collaborative environment. GitHub is one place on the internet where you can centrally host your git repos and collaborate with other developers.
|
||||
Till now all the operations we did were in our local repo while git also helps us in a collaborative environment. GitHub is one place on the Internet where you can centrally host your git repos and collaborate with other developers.
|
||||
|
||||
Most of the workflow will remain the same as we discussed, with addition of couple of things:
|
||||
|
||||
1. Pull: to pull latest changes from github (the central) repo
|
||||
2. Push: to push your changes to github repo so that it's available to all people
|
||||
1. Pull: to pull latest changes from GitHub (the central) repo
|
||||
2. Push: to push your changes to GitHub repo so that it's available to all people
|
||||
|
||||
GitHub has written nice guides and tutorials about this and you can refer them here:
|
||||
GitHub has written nice guides and tutorials about this and you can refer to them here:
|
||||
|
||||
- [GitHub Hello World](https://guides.github.com/activities/hello-world/)
|
||||
- [Git Handbook](https://guides.github.com/introduction/git-handbook/)
|
||||
@@ -22,7 +22,7 @@ applypatch-msg.sample fsmonitor-watchman.sample pre-applypatch.sample pr
|
||||
commit-msg.sample post-update.sample pre-commit.sample pre-rebase.sample prepare-commit-msg.sample
|
||||
```
|
||||
|
||||
Names are self explanatory. These hooks are useful when you want to do certain things when a certain event happens. If you want to run tests before pushing code, you would want to setup `pre-push` hooks. Let's try to create a pre commit hook.
|
||||
Names are self-explanatory. These hooks are useful when you want to do certain things when a certain event happens. If you want to run tests before pushing code, you would want to setup `pre-push` hooks. Let's try to create a pre commit hook.
|
||||
|
||||
```bash
|
||||
$ echo "echo this is from pre commit hook" > .git/hooks/pre-commit
|
||||
|
||||
Reference in New Issue
Block a user