Skip to content

Instantly share code, notes, and snippets.

@zrruziev
Last active February 26, 2023 13:22
Show Gist options
  • Save zrruziev/1720ca1892153a358e61c3780c373c53 to your computer and use it in GitHub Desktop.
Save zrruziev/1720ca1892153a358e61c3780c373c53 to your computer and use it in GitHub Desktop.
The easiest, smallest, and best git tutorial!

Step 1: Install Git

If you haven't installed Git already, you can download and install it from the official Git website: https://git-scm.com/downloads

Step 2: Create a new repository

To create a new Git repository, navigate to the directory where you want to store your project and run the following command:

git init

This will create a new Git repository in the current directory.

Step 3: Add files to the repository

Once you have created a new repository, you can add files to it using the following command:

git add <file>

For example, to add a file called index.html, you can run:

git add index.html

This command adds the file to the staging area, which is where Git tracks changes to your files.

You can also add all files in the current directory and its subdirectories to the staging area by running:

git add .

Step 4: Commit changes

After adding files to the staging area, you can commit your changes using the following command:

git commit -m "Commit message"

The -m flag allows you to specify a commit message, which is a brief description of the changes you made. For example, if you added a new feature to your code, you can write a commit message like "Added new feature".

Step 5: Check the status of the repository

To check the status of your repository, you can run the following command:

git status

This command shows you which files have been modified, which files are staged for commit, and which files are not tracked by Git.

Step 6: View the commit history

To view the commit history of your repository, you can run the following command:

git log

This command shows you a list of all the commits that have been made to the repository, along with their commit messages, commit hash-codes, author infos, and the dates they were made.

Step 7: Create a new branch

To create a new branch in your repository, you can run the following command:

git branch <branch-name>

For example, to create a new branch called feature-branch, you can run:

git branch feature-branch

This command creates a new branch based on the current branch you are on.

Step 8: Switch to a different branch

To switch to a different branch, you can run the following command:

git checkout <branch-name>

For example, to switch to the feature-branch branch, you can run:

git checkout feature-branch

This command switches you to the specified branch.

Step 9: Merge branches

To merge one branch into another, you can run the following command:

git merge <branch-name>

For example, to merge the feature-branch into the master branch, you can run:

git checkout master
git merge feature-branch

This command merges the changes from the specified branch into the current branch.

Step 10: Push changes to a remote repository

To push changes to a remote repository, you need to first add the remote repository's URL using the following command:

git remote add <remote-name> <remote-url>

For example, to add a remote repository called origin with the URL https://github.com/your-username/your-repository.git, you can run:

git remote add origin https://github.com/your-username/your-repository

You can use whatever you want as remote repository name instead of "origin", which is the default name given to the remote repository. Once you have added the remote repository, you can push your changes to it using the following command:

git push <remote-name> <branch-name>

For example, to push changes to the master branch of the origin remote repository, you can run:

git push origin master

This command pushes the changes you made to the specified branch of the remote repository.

Step 11: Pull changes from a remote repository

To pull changes from a remote repository, you can run the following command:

git pull <remote-name> <branch-name>

For example, to pull changes from the master branch of the origin remote repository, you can run:

git pull origin master

This command fetches the changes from the specified branch of the remote repository and merges them into your local repository.

Step 12: Clone a remote repository

To clone a remote repository, you can run the following command:

git clone <remote-url>

For example, to clone a repository with the URL https://github.com/your-username/your-repository.git, you can run:

git clone https://github.com/your-username/your-repository.git

This command creates a copy of the remote repository on your local machine.
When you clone a remote repository, you create a local copy of the entire repository, including all of its branches. By default, Git checks out the main branch (usually called master or main) after cloning a repository. If you want to switch to a different branch, you can use the git checkout <branch-name> command.

Step 13: Ignore files

Sometimes you might want to ignore certain files or directories in your repository, for example, build artifacts or temporary files. To do this, you can create a file called .gitignore in the root directory of your repository and add the names of the files or directories you want to ignore. Here's an example .gitignore file:

# Ignore build artifacts
build/

# Ignore temporary files
*.tmp

Step 14: Revert changes

If you want to revert your changes to a previous commit, you can use the following command:

git revert <commit-hash>

For example, to revert to the commit with the hash abc123, you can run:

git revert abc123

This command creates a new commit that undoes the changes made by the specified commit.

Step 15: Undo local changes

If you want to discard your local changes and revert to the last committed version of a file, you can use the following command:

git checkout -- <file>

For example, to discard local changes to a file called index.html, you can run:

git checkout -- index.html

This command overwrites the local version of the file with the last committed version.

That's it!



EXTRA:

git fetch and git pull

git fetch and git pull are both used to update a local repository with changes made to a remote repository. However, they work in slightly different ways.

git fetch downloads the latest changes from the remote repository, but does not automatically merge them into the local repository. Instead, it updates the local tracking branches, which are references to the state of the remote branches. This allows you to review the changes before merging them into your local repository. You can use git merge or git rebase to integrate the changes into your local branch after you've reviewed them.

git pull, on the other hand, automatically downloads the latest changes from the remote repository and merges them into the local repository. This means that any changes you've made to your local repository will be overwritten by the changes from the remote repository, potentially leading to conflicts. If you're working on a team or with a shared repository, it's generally a good idea to use git fetch followed by git merge or git rebase to ensure that you're reviewing and integrating changes in a controlled way.

In summary, git fetch is a safe way to update your local repository with changes from the remote repository, while git pull should be used with caution to avoid overwriting local changes.


git merge vs git rebase

git merge and git rebase are both used to integrate changes from one branch into another branch. However, they do it in different ways.

git merge takes the changes from one branch and merges them into another branch. The result is a new commit on the target branch that incorporates the changes from the source branch. The history of the source branch is preserved. Here's an example of merging branch feature into branch master:

git checkout master
git merge feature

This creates a new merge commit that incorporates the changes from 'feature' into 'master'.

git rebase, on the other hand, moves the entire 'feature' branch to begin on the top of the 'master' branch, effectively incorporating all the changes in the 'feature' branch into the master branch in a linear history. The result is a cleaner history that shows the changes from the 'feature' branch applied directly on top of the 'master' branch. Here's an example of rebasing branch 'feature' onto branch 'master':

git checkout feature
git rebase master

This moves the entire 'feature' branch to begin on the top of the 'master' branch, effectively incorporating all the changes in the 'feature' branch into the 'master' branch in a linear history.

In summary, git merge is used when you want to merge changes from one branch into another and preserve the history of both branches. git rebase is used when you want to incorporate the changes from one branch into another in a cleaner, linear history.

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