Tag Archives: Git

GIT ADD . (ALL)

git add . is a Git command used to stage all changes made to a working directory, before committing them.

Here is an example:

Suppose you made changes to two files named file1.txt and file2.txt. You want to include both changes in your next commit.
To do so, first navigate to the project directory in the terminal and execute:

git add .

This command stages all of the changes made in repository. You can now review the changes staged by executing:

git status

You should see that file1.txt and file2.txt have been staged.
Now you can commit the staged changes by running:

git commit -m "Added new feature to file1.txt"

The commit will include all changes.

GIT PUSH

git push is a Git command used to upload your local commits to a remote repository. It is used to push changes made to your local repository to a remote repository and update the remote repository’s branch with your local changes. Here is an example:

Let’s say you have made some changes to a local repository and committed those changes. Now, you want to push those changes to a remote repository on GitHub.

First, you need to add a remote repository to your local repository by using the command git remote add origin [remote repository URL].

Once you have added a remote repository, you can push your changes by running the command git push origin [branch name]. For example, if you want to push your changes to the master branch, you will run the command.

git push origin master

or simply

git push

This will upload your local changes to the remote repository’s master branch, making them available to others who are collaborating on the project.

GIT PULL

git pull is a command in Git that fetches and merges changes from a remote repository to your local repository. This command is handy when working in a team that pushes code to a remote repository frequently.

Here’s an example:
Let’s say there are two branches in the remote repository main and feature. Your local repository is currently on the main branch. To update your local repository with the changes from the feature branch of the remote repository, you can use the following command:

git pull origin feature

This command will fetch changes from the feature branch of the remote repository origin and merge them into your local repository. If there are any conflicts between the changes in the remote repository and your local repository, you’ll need to resolve these conflicts before you can push your changes back to the remote repository.

GIT CHECKOUT

Git checkout command is used to switch between different branches and to update the files in the working directory to match the versions stored in a specific branch or commit.

Here’s an example:

Suppose you are working on a branch called “feature-branch”, and you want to switch to the main branch. You can use the following command:

git checkout main

This command will switch you to the main branch. Now, let’s say you need to work on a specific commit in that branch. You can use the commit hash or ID to check out that version as follows:

git checkout 87a3cde

Here, 87a3cde is the commit ID you want to check out.

I hope that helps!

GIT BRANCH

git branch is a Git command used to list, create, or delete branches in a Git repository. When used with no arguments, it lists all existing branches in the repository.

Here’s an example of how git branch can be used:

$ git branch
* master
  development
  feature-123

In this example, the git branch command is used to list all the branches in the repository. The * character indicates the currently checked out branch, which is master in this case. The other branches listed are development and feature-123.

You can also use git branch with additional arguments to create or delete branches. For example, to create a new branch called new-feature, you can run:

$ git branch new-feature

And to delete a branch called old-feature, you can run:

$ git branch -D old-feature

I hope this helps! Let me know if you have any other questions.

GIT LOG

git log is a command in Git used to display the commit history of a repository. When run, it shows the author, date, and commit message of all commits in reverse chronological order. The latest commit appears at the top of the list.

Here’s an example:

$ git log
commit b4691c6fd3feb96eed5b51c4f9ebdd5738cde0eb (HEAD -> main, origin/main, origin/HEAD)
Author: John Doe <[email protected]>
Date:   Fri Jul 23 15:51:47 2021 -0400

    Add new feature

commit 0e94d82558f8c8ed494e7714c7bc28c41462a9e1
Author: Jane Smith <[email protected]>
Date:   Thu Jul 22 11:24:35 2021 -0400

    Update documentation

commit 3028c5a76d78f6c673296b059b452fe466305d20
Author: John Doe <[email protected]>
Date:   Wed Jul 21 09:17:26 2021 -0400

    Fix bug in login feature

In this example, the git log command lists the three most recent commits in the repository, showing the commit hash, the author, the date, and the commit message for each one.

GIT CLONE

git clone is a command that creates a copy of a repository, including all its branches and history, in a new directory.

Here’s an example:

git clone https://github.com/username/repo.git

This will create a copy of the repository repo owned by username on GitHub, and save it to a new directory called repo in your local machine.

You can also use git clone to clone a repository from a remote server using SSH, or to clone a specific branch.

For more information, you can check out the official Git documentation.