Skip to content

Instantly share code, notes, and snippets.

@rbatinov
Last active February 15, 2024 12:55
Show Gist options
  • Save rbatinov/47e14e1447d14aca14aa8ed12a3d203b to your computer and use it in GitHub Desktop.
Save rbatinov/47e14e1447d14aca14aa8ed12a3d203b to your computer and use it in GitHub Desktop.

Git - All you need to know to use it

Table of contents

Configuration

  • set username and user email
git config --global user.name "w3schools-test"
git config --global user.email "test@w3schools.com"

Note: Use global to set the username and e-mail for every repository on your computer. If you want to set the username/e-mail for just the current repo, you can remove global

  • check status and if files are part of the repo
git status 

Getting Started

To download a repository with its full history to your pc then run this command:

git clone https://.....

Add your url. You can get it from GitHub/GitLab or some other Git System. You can find it in your repository - look for drop down Code or Clone.

This will create a folder and download all the code from the repository.

  • Check if you downloaded the full history:
git log
commit facaeae8fd87dcb63629f108f401aa9c3614d4e6 (HEAD -> master, origin/master, origin/HEAD)
Merge: e7de78f 5a04b6f
Author: w3schools-test 
Date:   Fri Mar 26 15:44:10 2021 +0100

    Merge branch 'master' of https://github.com/w3schools-test/hello-world

commit e7de78fdefdda51f6f961829fcbdf197e9b926b6
Author: w3schools-test 
Date:   Fri Mar 26 15:37:22 2021 +0100

    Updated index.html. Resized image
    
.....

You will have different commit records.

Staging

  • Files in your Git repository folder can be in one of 2 states:
    • Tracked - files that Git knows about and are added to the repository
    • Untracked - files that are in your working directory, but not added to the repository

Staged files are files that are ready to be committed to the repository you are working on. You will learn more about commit shortly.

  • Add specific file to the stage
git add index.html
  • Add all files to the stage
git add --all

Commit

Adding commits keep track of our progress and changes as we work. Git considers each commit change point or "save point". It is a point in the project you can go back to if you find a bug, or want to make a change.

When we commit, we should always include a message.

By adding clear messages to each commit, it is easy for yourself (and others) to see what has changed and when.

  • Commit
git commit -m "First release of Hello World!"

The commit command performs a commit, and the -m "message" adds a message.
The Staging Environment has been committed to our repo, with the message: "First release of Hello World!"

  • To view the history of commits for a repository, you can use the log command:
git log

Change last local commit

If you commit local and you forget to add something, do not worry, you can change this easily.

Just add your code in the files you want as you are creating a new feature.

Then Stage your changes and run in the console:

git commit --amend

This command will edit the last commit in your branch. Check for merge conflicts if any and resolve them.

If there are no conflicts you can edit the commit message or let it the same.

You are ready to push your commits to the remote branch.

Git Delete Last Commit

Removing the last commit

!!! NOTE BEFORE RUN THE BELOW COMMANDS !!!

!!! ALL UNSTAGED CHANGES WILL BE LOST IF YOU DELETE THE LAST COMMIT!!!

  • To remove THE LAST commit from git, you can simply run:
git reset --hard HEAD^
  • If you are REMOVING MULTIPLE commits from the top, you can run
git reset --hard HEAD~2

to remove the last two commits.

  • You can increase the number to remove even more commits.

  • If you want to "uncommit" the commits, but keep the changes around for reworking, remove the "--hard":

git reset HEAD^

which will evict the commits from the branch and from the index, but leave the working tree around.

Git Rewrite History

Git Help

If you are having trouble remembering commands or options for commands, you can use Git help.

There are a couple of different ways you can use the help command in command line:

  • See all the available options for the specific command
git command -help
  • See all possible commands
git help --all

More about available commands

Git Branch

In Git, a branch is a new/separate version of the main repository.

Let's say you have a large project, and you need to update the design on it.

Branches allow you to work on different parts of a project without impacting the main branch.

When the work is complete, a branch can be merged with the main project.

You can even switch between branches and work on different projects without them interfering with each other.

Branching in Git is very lightweight and fast!

  • new git branch
git branch hello-world-images
  • check all available branches
git branch

* shows on which branch we are working

  • move to another branch checkout is the command used to check out a branch. Moving us from the current branch, to the one specified at the end of the command:
git checkout hello-world-images
Switched to branch 'hello-world-images'
  • Emergency branch Now imagine that we are not yet done with hello-world-images, but we need to fix an error on master.

I don't want to mess with master directly, and I do not want to mess with hello-world-images, since it is not done yet.

So we create a new branch to deal with the emergency:

Go to your master branch (or this one which is working version) and run:

git checkout -b emergency-fix
Switched to a new branch 'emergency-fix'

Now we have created a new branch from master, and changed to it. We can safely fix the error without disturbing the other branches.

Fix the error.

Then add to stage and commit:

git add index.html
git commit -m "updated index.html with emergency fix"

After that you are ready to merge the two branches.

  • change to the master branch
git checkout master
  • merge
git merge emergency-fix

Since the emergency-fix branch came directly from master, and no other changes had been made to master while we were working, Git sees this as a continuation of master. So it can "Fast-forward", just pointing both master and emergency-fix to the same commit.

As master and emergency-fix are essentially the same now, we can delete emergency-fix, as it is no longer needed:

git branch -d emergency-fix

Delete a branch

Show all available branches

git branch

Then

git branch -D branch-name

Git Pull

When working as a team on a project, it is important that everyone stays up to date.
Any time you start working on a project, you should get the most recent changes to your local copy.
With Git, you can do that with pull.
pull is a combination of 2 different commands:

  • fetch
  • merge

Let's take a closer look into how fetch, merge, and pull works.


Fetch

fetch gets all the change history of a tracked branch/repo.
So, on your local Git, fetch updates to see what has changed on GitHub:

git fetch origin
  • get differences between local master and origin/master
git diff origin/master

If there are no conflicts you can merge:


Merge

git merge origin/master

Check again if you are up to date with the latest version:

git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

Pull

But what if you just want to update your local repository, without going through all those steps?

pull is a combination of fetch and merge. It is used to pull all changes from a remote repository into the branch you are working on.

  • Use pull to update our local Git:
git pull origin

That is how you keep your local Git up to date from a remote repository.

Let's say we have this scenario:

  1. Create a new branch branch1, add some new code and publish the branch.
  2. Then checkout to your master branch or some else and add/edit some code.
  3. At some moment you decide that you need to update the branch1 which is not yet merged by your lead to the master but in the master there are new activities.
  4. You checkout to the branch1 and you need to pull the latest code - so run git pull and you get an error where it will tell you something like you need to specify with which remote branch you want to merge.
  5. To check available remote branches you can run git remote -v.
  6. If you want to download (pull) the latest code for the master you can run this command: git pull origin master.
  7. This will merge the remote master with your local branch1.
  8. Now you are ready to edit again the branch1 and to publish the new commits after you are done with them.
git pull origin master

Git Push

This command is used to upload your code to the remote repository.

  • First you need to edit your code.

  • Then add it to the stage.

git add index.html
  • Then commit it.
git commit -m "Your commit message here"

!!!Remember to write understandable comments!!! - This helps you and your team to better read the history of the project and understand what did you edit with your commit.

  • Now you are ready to push your code:
git push origin

You can go to your remote repository and check if everything is there.

if you are working on different branch than the master then you need to specify to which branch do you want to push the changes:

git push origin branchName

Git Push Force

!!! Doing this can override and delete important commits on the remote branch, so think TWICE before you run the command!!!

  • If you want your current commits to REWRITE the remote commits history, you can use this command:
git push origin <your_branch_name> -f

Git Push Branch

  • First create a new branch
git checkout -b update-readme

Now you switched to the new update-readme branch and you can add some new code to it.

  • After that check the status
git status
On branch update-readme
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
  • Add to the stage
git add README.md
  • Now the status will look like this:
git status
On branch update-readme
Changes to be committed:
  (use "git restore --staged ..." to unstage)
        modified:   README.md
  • Now you are ready to commit to our new branch:
git commit -m "Updated readme for GitHub Branches"
  • Now push the branch from our local Git repository, to GitHub, where everyone can see the changes:
git push origin update-readme
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 16 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 366 bytes | 366.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
remote:
remote: Create a pull request for 'update-readme' on GitHub by visiting:
remote:      https://github.com/w3schools-test/hello-world/pull/new/update-readme
remote:
To https://github.com/w3schools-test/hello-world.git
 * [new branch]      update-readme -> update-readme
  • Now if you go to GitHub, GitLab, or other, you will see that there is a merge request.
    If the repository is yours, you can create pull request and merge and pull by yourself.
    If the repository is held by someone else, then it will wait for their approval.

Git Stash

git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. Stashing is handy if you need to quickly switch context and work on something else, but you're mid-way through a code change and aren't quite ready to commit.

The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy. For example:

$ git status
On branch main
Changes to be committed:

    new file:   style.css

Changes not staged for commit:

    modified:   index.html

$ git stash
Saved working directory and index state WIP on main: 5002d47 our new homepage
HEAD is now at 5002d47 our new homepage

$ git status
On branch main
nothing to commit, working tree clean

At this point you're free to make changes, create new commits, switch branches, and perform any other Git operations; then come back and re-apply your stash when you're ready.

Note that the stash is local to your Git repository; stashes are not transferred to the server when you push.

Re-applying your stashed changes

  • You can reapply previously stashed changes with git stash pop:
  • Alternatively, you can reapply the changes to your working copy and keep them in your stash with git stash apply:

Stashing untracked or ignored files

Now that you know the basics of stashing, there is one caveat with git stash you need to be aware of: !!!by default Git won't stash changes made to untracked or ignored files!!!.

By default, running git stash will stash:

  • changes that have been added to your index (staged changes)
  • changes made to files that are currently tracked by Git (unstaged changes)

But it will not stash:

  • new files in your working copy that have not yet been staged
  • files that have been ignored

So if we add a third file to our example above, but don't stage it (i.e. we don't run git add), git stash won't stash it.

$ script.js

$ git status
On branch main
Changes to be committed:

    new file:   style.css

Changes not staged for commit:

    modified:   index.html

Untracked files:

    script.js

$ git stash
Saved working directory and index state WIP on main: 5002d47 our new homepage
HEAD is now at 5002d47 our new homepage

$ git status
On branch main
Untracked files:

    script.js
  • Adding the -u option (or --include-untracked) tells git stash to also stash your untracked files:
$ git status
On branch main
Changes to be committed:

    new file:   style.css

Changes not staged for commit:

    modified:   index.html

Untracked files:

    script.js

$ git stash -u
Saved working directory and index state WIP on main: 5002d47 our new homepage
HEAD is now at 5002d47 our new homepage

$ git status
On branch main
nothing to commit, working tree clean

You can include changes to ignored files as well by passing the -a option (or --all) when running git stash.

Managing multiple stashes

  • You aren't limited to a single stash. You can run git stash several times to create multiple stashes, and then use git stash list to view them. By default, stashes are identified simply as a "WIP" – work in progress – on top of the branch and commit that you created the stash from. After a while it can be difficult to remember what each stash contains:
$ git stash list
stash@{0}: WIP on main: 5002d47 our new homepage
stash@{1}: WIP on main: 5002d47 our new homepage
stash@{2}: WIP on main: 5002d47 our new homepage
  • To provide a bit more context, it's good practice to annotate your stashes with a description, using git stash save "message":
$ git stash save "add style to our site"
Saved working directory and index state On main: add style to our site
HEAD is now at 5002d47 our new homepage

$ git stash list
stash@{0}: On main: add style to our site
stash@{1}: WIP on main: 5002d47 our new homepage
stash@{2}: WIP on main: 5002d47 our new homepage
  • By default, git stash pop will re-apply the most recently created stash: stash@{0}

You can choose which stash to re-apply by passing its identifier as the last argument, for example:

$ git stash pop stash@{2}

Creating a branch from your stash

  • If the changes on your branch diverge from the changes in your stash, you may run into conflicts when popping or applying your stash. Instead, you can use git stash branch to create a new branch to apply your stashed changes to:
$ git stash branch add-stylesheet stash@{1}
Switched to a new branch 'add-stylesheet'
On branch add-stylesheet
Changes to be committed:

    new file:   style.css

Changes not staged for commit:

    modified:   index.html

Dropped refs/stash@{1} (32b3aa1d185dfe6d57b3c3cc3b32cbf3e380cc6a)

This checks out a new branch based on the commit that you created your stash from, and then pops your stashed changes onto it.

Cleaning up your stash

  • If you decide you no longer need a particular stash, you can delete it with git stash drop:
$ git stash drop stash@{1}
Dropped stash@{1} (17e2697fd8251df6163117cb3d58c1f62a5e7cdb)
  • Or you can delete all of your stashes with:
$ git stash clear

You can read more info about the git stash command here.

Git Copy Changes from Current Branch to New (if forgotten to create a new branch)

The common scenario is the following: I forgot to create the new branch for the new feature, and was doing all the work in the old feature branch. I have commited all the "old" work to the master branch, and I want my new branch to grow from the "master". I have not made a single commit of my new work. Here is the branch structure: "master"->"Old_feature"

git stash 
git checkout -b "New_branch"
git stash apply

Where stash is

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

checkout is for creating the new branch

and stash apply is to apply the stashed record from the old branch to the new branch.

Then stage all the changes

git add --all

Then Commit

git commit -m "Your comment"

Then Publish the branch

git push origin New_branch

Git .gitignore

When sharing your code with others, there are often files or parts of your project, you do not want to share.

Examples

  • log files
  • temporary files
  • hidden files
  • personal files
  • etc.

Git can specify which files or parts of your project should be ignored by Git using a ``.gitignore` file.

Git will not track files and folders specified in .gitignore. However, the .gitignore file itself IS tracked by Git.

  • To create a .gitignore file, go to the root of your local Git, and create it:
touch .gitignore
  • .gitignore example file:
# ignore ALL .log files
*.log

# ignore ALL files in ANY directory named temp
temp/

Now all .log files and anything in temp folders will be ignored by Git.

Note: In this case, we use a single .gitignore which applies to the entire repository. It is also possible to have additional .gitignore files in subdirectories. These only apply to files or folders within that directory.


Git Security SSH

What is SSH?

SSH is a secure shell network protocol that is used for network management, remote file transfer, and remote system access.

SSH uses a pair of SSH keys to establish an authenticated and encrypted secure network protocol. It allows for secure remote communication on unsecured open networks.

SSH keys are used to initiate a secure "handshake". When generating a set of keys, you will generate a "public" and "private" key.

The "public" key is the one you share with the remote party. Think of this more as the lock.

The "private" key is the one you keep for yourself in a secure place. Think of this as the key to the lock.

SSH keys are generated through a security algorithm. It is all very complicated, but it uses prime numbers, and large random numbers to make the public and private key.

It is created so that the public key can be derived from the private key, but not the other way around.

Generating an SSH Key Pair

ssh-keygen -t rsa -b 4096 -C "test@w3schools.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/user/.ssh/id_rsa):
Created directory '/Users/user/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/user/.ssh/id_rsa
Your public key has been saved in /Users/user/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:******************************************* test@w3schools.com
The key's randomart image is:
+---[RSA 4096]----+
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
+----[SHA256]-----+

You will be prompted with the following through this creation:

Enter file in which to save the key (/c/Users/user/.ssh/id_rsa):

Select a file location, or press "Enter" to use the default file location.

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

Entering a secure passphrase will create an additional layer of security. Preventing anyone who gains access to the computer to use that key without the passphrase. However, it will require you to supply the passphrase anytime the SSH key is used.

Now we add this SSH key pair to the SSH-Agent (using the file location from above):

ssh-add /Users/user/.ssh/id_rsa
Enter passphrase for /Users/user/.ssh/id_rsa:
Identity added: /Users/user/.ssh/id_rsa (test@w3schools.com)

You will be prompted to supply the passphrase, if you added one.

Now the SSH key pair is ready to use.

Copy the SSH Public Key

clip < /Users/user/.ssh/id_rsa.pub
  • Go to GitHub, navigate to the top left corner, click your profile, and select: Settings:
  • Then select "SSH and GPG keys". and click the "New SSH key" button:
  • Select a title, and paste the public SSH key into the "Key" field, and click "Add SSH Key":

Test SSH Connection to GitHub

ssh -T git@github.com
The authenticity of host 'github.com (140.82.121.3)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,140.82.121.3' (RSA) to the list of known hosts.
Hi w3schools-test! You've successfully authenticated, but GitHub does not provide shell access.

If the last line contains your username on GitHub, you are successfully authenticated!

Add New GitHub SSH Remote

Now we can add a new remote via SSH to our Git.

First, get the SSH address from our repository on GitHub:

Then use that address to add a new origin:

git remote add ssh-origin git@github.com:w3schools-test/hello-world.git

Note: You can change a remote origin from HTTPS to SSH with the command: git remote set-url remote-name git@github.com:username/repository.git

git remote set-url origin git@github.com:w3schools-test/hello-world.git

Git Revert

revert is the command we use when we want to take a previous commit and add it as a new commit, keeping the log intact.

  • Step 1: Find the previous commit:
  • Step 2: Use it to make a new commit:

Let's make a new commit, where we have "accidentally" deleted a file:

git commit -m "Just a regular update, definitely no accidents here..."
[master 16a6f19] Just a regular update, definitely no accidents here...
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 img_hello_git.jpg

Git Revert Find Commit in Log

First thing, we need to find the point we want to return to. To do that, we need to go through the log.

To avoid the very long log list, we are going to use the --oneline option, which gives just one line per commit showing:

  • The first seven characters of the commit hash
  • the commit message

So let's find the point we want to revert:

git log --oneline
52418f7 (HEAD -> master) Just a regular update, definitely no accidents here...
9a9add8 (origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from w3schools-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 merged with hello-world-images after fixing conflicts
1f1584e added new image
dfa79db updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!

We want to revert to the previous commit: 52418f7 (HEAD -> master) Just a regular update, definitely no accidents here..., and we see that it is the latest commit.

Git Revert HEAD

We revert the latest commit using git revert HEAD (revert the latest change, and then commit), adding the option --no-edit to skip the commit message editor (getting the default revert message):

git revert HEAD --no-edit
[master e56ba1f] Revert "Just a regular update, definitely no accidents here..."
 Date: Thu Apr 22 10:50:13 2021 +0200
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 img_hello_git.jpg

Note: To revert to earlier commits, use git revert HEAD~x (x being a number. 1 going back one more, 2 going back two more, etc.)


Git Reset

reset is the command we use when we want to move the repository back to a previous commit, discarding any changes made after that commit.

  • Step 1: Find the previous commit:
  • Step 2: Move the repository back to that step:

Git Reset Find Commit in Log

First thing, we need to find the point we want to return to. To do that, we need to go through the log.

To avoid the very long log list, we are going to use the --oneline option, which gives just one line per commit showing:

  • The first seven characters of the commit hash - this is what we need to refer to in our reset command.
  • the commit message

So let's find the point we want to reset to:

git log --oneline
e56ba1f (HEAD -> master) Revert "Just a regular update, definitely no accidents here..."
52418f7 Just a regular update, definitely no accidents here...
9a9add8 (origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from w3schools-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 merged with hello-world-images after fixing conflicts
1f1584e added new image
dfa79db updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!

We want to return to the commit: 9a9add8 (origin/master) Added .gitignore, the last one before we started to mess with things.

Git Reset

We reset our repository back to the specific commit using git reset commithash (commithash being the first 7 characters of the commit hash we found in the log):

git reset 9a9add8

Warning: Messing with the commit history of a repository can be dangerous. It is usually ok to make these kinds of changes to your own local repository. However, you should avoid making changes that rewrite history to remote repositories, especially if others are working with them.

Git Undo Reset

Even though the commits are no longer showing up in the log, it is not removed from Git.

If you know the commit hash you can reset to it:

git reset e56ba1f

Git Log

  • If you want to check the history of the commits, you can run this command:
git log
  • If you want to filter the log by some author, you can run this command:
git log --author="AuthorName"
  • If you want to filter the log by specific branch, you can run this command:
git log brancName
  • If you want to filter the log by specific branch and author, you can run this command:
git log brancName --author="AuthorName"

Git log --graph

  • If you want to visualize your branches in the console, you can run this command:
git log --graph
  • For branch names and a compact view, try:
git log --graph --decorate --oneline

Git Branch Rename

  • If you want to rename the branch you are currently in:
git branch -m <newname>

Git Cherry-Pick

  • If you want to add specific commit (or multiple commits) to any branch, you can use the git cherry-pick command.
  • Let's say you need to hotfix some fast update on production and you have the commit for that, but also you have let's say 100 more commits in development. You cannot upload all of them, but you can cherry pick the desired commit and "paste" it to another branch.
  • You can read this tutorial for more information since it is very good: https://acompiler.com/git-cherry-pick/

Git Rebase

Rebasing is the process of moving or combining a sequence of commits to a new base commit. Rebasing is most useful and easily visualized in the context of a feature branching workflow. On the other hand, rebasing is like saying, “I want to base my changes on what everybody has already done.”

Git Merge vs. Rebase

Git merge and rebase are two different ways which can be used to merge changes from one branch into another. Git merge can develop a new commit that can integrate the changes from two branches. This new commit has two parent commits, one from each branch. Git rebase, execute the changes from one branch onto another and explore it as though the changes were made directly on that branch.

  • You can read more detailed information here

How to use the command

  • Simply if you have some new branch your-branch-name. Then checkout to it and after that run the rebase command:
git checkout your-branch-name
git rebase master

Where master is the master branch we want to get the latest commits from. But you can specify any other branch if you want.

Check when code was added

While git blame displays the last author that modified a line, often times you will want to know when a line was originally added. This can be cumbersome to achieve using git blame. It requires a combination of the -w, -C, and -M options. It can be far more convenient to use the git log command.

To list all original commits in-which a specific code piece was added or modified execute git log with the -S option. Append the -S option with the code you are looking for. Let's take one of the lines from the README output above to use as an example. Let us take the text "CSS3D and WebGL renderers" from Line 12 of the README output.

$ git log -S"CSS3D and WebGL renderers." --pretty=format:'%h %an %ad %s'
    e339d3c85 Mario Schuettel Tue Oct 13 16:51:06 2015 +0200 reverted README.md to original content
    509c2cc35 Daniel Tue Sep 8 13:56:14 2015 +0200 Updated README
    cb20237cc Mr.doob Mon Dec 31 00:22:36 2012 +0100 Removed DOMRenderer. Now with the CSS3DRenderer it has become irrelevant.

This output shows us that content from the README was added or modified 3 times by 3 different authors. It was originally added in commit cb20237cc by Mr.doob. In this example, git log has also been prepended with the --pretty-format option. This option converts the default output format of git log into one that matches the format of git log. For more information on usage and configuration options visit the git log page.

This documentation with commands is written with the support of the W3Schools Git Lessons as well as with my personal experience.

You can read more about the git tutorials from W3schools here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment