If you haven't installed Git already, you can download and install it from the official Git website: https://git-scm.com/downloads
To create a new Git repository, navigate to the directory where you want to store your project and run the following command:
git initThis will create a new Git repository in the current directory.
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.htmlThis 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 .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".
To check the status of your repository, you can run the following command:
git statusThis command shows you which files have been modified, which files are staged for commit, and which files are not tracked by Git.
To view the commit history of your repository, you can run the following command:
git logThis 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.
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-branchThis command creates a new branch based on the current branch you are on.
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-branchThis command switches you to the specified branch.
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-branchThis command merges the changes from the specified branch into the current branch.
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-repositoryYou 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 masterThis command pushes the changes you made to the specified branch of the 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 masterThis command fetches the changes from the specified branch of the remote repository and merges them into your local 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.gitThis 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.
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
*.tmpIf 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 abc123This command creates a new commit that undoes the changes made by the specified commit.
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.htmlThis command overwrites the local version of the file with the last committed version.
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 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 featureThis 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 masterThis 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.