2023-03-04

git Commands

What is Git

Git is a free and open-source distributed version control system that allows developers to manage their source code and collaborate with others on software projects.

It was created by Linus Torvalds, the same person who created the Linux operating system, and has become the most widely used version control system in the world.

With Git, developers can track changes to their code, collaborate with others, and easily roll back changes if something goes wrong. Its popularity has made it an essential tool for software development teams, from small startups to large enterprises.

Basic Git Commands

Here are common basic Git commands.

Git Add

The git add command is an essential part of the git workflow as it allows developers to stage changes before committing them to the repository.

When you make changes to files in your working directory, Git automatically tracks them as modified files. However, these changes are not automatically added to the staging area, where Git keeps track of changes that are ready to be committed. The git add command is used to move changes from the working directory to the staging area.

Here is an example of how to use the git add command:

bash
$ git add file.txt

In this example, the git add command adds the file "file.txt" to the staging area. You can also use wildcards to add multiple files at once:

bash
$ git add *.txt

This command adds all text files in the current directory to the staging area.

You can also use the git add command to stage all changes made to files in the working directory:

bash
$ git add .

The dot (.) represents the current directory, and this command stages all changes made to files in the directory.

Git Commit

git commit command is used to save the changes made to the local repository. It creates a new commit with a unique ID, timestamp, and commit message. The commit message should be a brief description of the changes made in the commit.

Here's an example of how to use the git commit command:

bash
$ git commit -m "Added new feature to login page"

In this example, the commit message is "Added new feature to login page". The -m flag is used to specify the commit message directly in the command. After executing this command, the changes made in the working directory will be saved in the local repository. It's important to note that committing changes is a local operation and does not affect the remote repository. To push the changes to the remote repository, the git push command needs to be used.

Git Push

The git push command is used to upload local repository content to a remote repository. This is the command that is used to publish changes to a remote repository so that others can access them.

Here's an example of how to use the git push command:

bash
$ git push origin master

In this example, we are pushing the master branch to the origin remote. When you execute this command, Git will attempt to push the changes to the remote repository. If you have made changes that conflict with changes made by others, Git may not allow you to push your changes until you have resolved those conflicts.

You can also specify a specific commit to push using the commit hash. For example:

bash
$ git push origin abc123

This command will push the commit with the hash abc123 to the origin remote.

It's important to note that you must have write access to the remote repository in order to push changes. If you don't have write access, you'll need to ask the repository owner to grant you permission before you can push changes.

Git Pull

The git pull command is used to update the local repository with the changes made to the remote repository. It is useful when multiple developers are working on the same project and need to keep their local repository up-to-date with the changes made by others.

Suppose you have cloned a repository from GitHub and want to update it with the latest changes made to the remote repository. You can do this by running the following command in your local repository:

bash
$ git pull

This will fetch the latest changes from the remote repository and merge them into your local repository. If there are any conflicts between your local changes and the remote changes, Git will prompt you to resolve the conflicts before the merge can be completed.

It's important to note that git pull is a combination of two commands: git fetch and git merge. The git fetch command downloads the latest changes from the remote repository, while the git merge command merges those changes into your local repository. You can also run these commands separately if you prefer more control over the update process.

Git Status

The git status command is used to show the current status of your local repository. It displays any changes made to your files since the last commit, files that are staged and ready to be committed, and files that have not been tracked by Git yet.

Suppose you have a Git repository and you make some changes to a file named "index.html". To see the status of your repository, you can run the following command:

bash
$ git status

This will display output similar to the following:

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

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")

In this example, Git is telling us that we have made changes to the "index.html" file, but those changes are not staged for a commit. Git also suggests that we can use the git add command to stage changes for a commit.

Git Log

The git log command is used to display the commit history of a Git repository. It shows a list of all the commits made in the repository, including the commit message, author, date, and commit hash.

Here is an example output of git log command.

bash
commit c0c0a7e92e49239e4d7d8f25d4f764da4fe4f17d
Author: John Smith <john.smith@example.com>
Date:   Tue Mar 2 14:07:31 2021 -0500

    Updated index.html file

commit 12040db00cc28a71903c2d8e4f4daa4c5e5b5c8e
Author: Jane Doe <jane.doe@example.com>
Date:   Mon Mar 1 10:44:12 2021 -0500

    Added new styles to style.css file

commit 8c8a49a2a9ce9dc3d76a3e8c1f618ee45ec54a71
Author: John Smith <john.smith@example.com>
Date:   Sun Feb 28 16:15:52 2021 -0500

    Initial commit

The output above shows a list of all the commits made in the repository, in reverse chronological order. The commit hash is shown as a string of alphanumeric characters, and each commit has an author, a date, and a commit message. In this example, we can see that John Smith updated the index.html file in the most recent commit, while Jane Doe added new styles to the style.css file in the second most recent commit. The oldest commit in the list is the initial commit, made by John Smith.

Branching in Git

Branching is a core feature of Git that allows developers to work on different versions of their codebase simultaneously. In Git, a branch is essentially a separate line of development that diverges from the main codebase. This makes it possible for developers to make changes to the codebase without affecting the main branch, or to work on new features or bug fixes independently of other developers.

When a new branch is created in Git, it is based on the current commit in the repository. This means that any changes made to files in the new branch will not affect the main branch until they are merged back in. Once the changes in a branch have been tested and approved, they can be merged back into the main branch using Git's merge functionality.

Using branching effectively is essential for managing complex software development projects, especially those involving multiple developers or teams. It allows for more efficient collaboration, easier testing, and better organization of code changes. With Git's powerful branching functionality, developers can experiment with new ideas and make changes to their codebase without the risk of affecting other parts of the project.

Git Branch

In Git, a branch is a separate line of development that allows you to work on different parts of a project without affecting the main codebase. You can create, list, and delete branches using the git branch command.

Suppose you're working on a project and you want to create a new branch to work on a feature. You can create a new branch called feature-branch using the following command:

bash
$ git branch feature-branch

This will create a new branch based on your current branch (usually master). You can then switch to the new branch using the git checkout command:

bash
$ git checkout feature-branch

Now, any changes you make will only affect the feature-branch branch, not the master branch. Once you're done working on the feature, you can merge the changes back into the master branch using the git merge command.

If you want to list all the branches in your repository, you can use the following command:

bash
$ git branch

This will list all the branches in your repository and highlight the current branch with an asterisk.

If you want to delete a branch, you can use the following command:

bash
$ git branch -d feature-branch

This will delete the feature-branch branch. However, if the branch has not been merged yet, Git will give you a warning to make sure you want to delete the branch.

Git Checkout

In Git, the git checkout command is used to switch between different branches, create new branches, and navigate through the commit history. Here are some examples:

Switching to an existing branch

To switch to an existing branch, you can use the git checkout command followed by the branch name. For example, to switch to the develop branch, you would run:

bash
$ git checkout develop

This will switch you to the develop branch and update your working directory to reflect the files and commits in that branch.

Creating a new branch

You can create a new branch and switch to it in one step by using the -b flag. For example, to create a new branch called feature-branch and switch to it, you would run:

bash
$ git checkout -b feature-branch

This will create a new branch based on the current branch (usually master) and switch you to the feature-branch.

You can use git checkout to navigate through the commit history of a repository. For example, if you want to see what your project looked like two commits ago, you would run:

bash
$ git checkout HEAD~2

This will move your working directory to the commit two commits ago. You can then make changes or view files as they appeared at that time.

It's worth noting that when you switch branches with git checkout, any changes you've made to files that are tracked by Git will be lost if they haven't been committed. Git will warn you if this is the case, so make sure to commit your changes or stash them before switching branches.

Git Merge

In Git, the git merge command is used to combine changes from different branches into a single branch. Here's an example:

Suppose say you've been working on a feature branch called new-feature and you want to merge those changes into the master branch. First, switch to the master branch using the git checkout command:

bash
$ git checkout master

Then, use the git merge command followed by the name of the branch you want to merge (in this case, new-feature):

bash
$ git merge new-feature

Git will then attempt to merge the changes from the new-feature branch into the master branch. If there are conflicts (i.e., changes in both branches that affect the same lines of code), Git will prompt you to resolve them before completing the merge.

Once the merge is complete, the changes from the new-feature branch will be incorporated into the master branch. You can then delete the new-feature branch if you no longer need it.

It's worth noting that in some cases, Git may perform a "fast-forward" merge, which simply moves the current branch pointer to the most recent commit on the other branch. This happens when the branch you're merging in has no new changes since the branch you're merging into was created. In this case, the merge is very simple and no conflicts will occur.

Git Workflow

Git workflow is a process that describes the steps involved in using Git for version control in software development projects. The workflow defines the way code changes are made, reviewed, and integrated into the codebase.

There are various Git workflows that can be followed, but they all share some common principles, such as the use of branches for isolating changes, committing changes regularly, and collaborating with other developers through code reviews.

Git workflows provide a structured and organized approach to software development that helps teams work together effectively and deliver high-quality code. By following a Git workflow, developers can ensure that code changes are properly tracked, reviewed, and integrated, leading to a more streamlined and efficient development process.

Git Stash

In Git, the git stash command is used to temporarily save changes that you've made to a branch, without committing them. This can be useful when you need to switch to another branch or work on a different task, but don't want to lose your current changes. Here's an example:

Let's say you've been working on a feature branch called new-feature and you've made some changes, but you're not ready to commit them yet. However, you need to switch to another branch to work on a critical bug fix. You can use the git stash command to save your changes temporarily:

bash
$ git stash

This command will save your changes and reset your working directory to the last committed state. You can now switch to the other branch and work on the bug fix:

bash
$ git checkout bug-fix

Once you've completed the bug fix, you can switch back to the new-feature branch and retrieve your saved changes using the git stash apply command:

bash
$ git checkout new-feature
$ git stash apply

This command will restore your saved changes to the branch and apply them to your working directory. You can now continue working on the new-feature branch as usual.

It's worth noting that you can use the git stash list command to see a list of all your saved stashes, and the git stash drop command to delete a specific stash. You can also use the git stash pop command to apply the most recent stash and remove it from the stash list in one step.

Git Rebase

In Git, the git rebase command is used to integrate changes from one branch onto another. It allows you to reapply commits on top of a different branch, which can help simplify the history of your repository and make it easier to understand. Here's an example:

Suppose you have two branches, feature and master. You've been working on the feature branch for a while, but in the meantime, some new changes have been made to the master branch that you want to include in your feature. To do this, you can use the git rebase command:

bash
$ git checkout feature
$ git rebase master

This command will reapply all the commits on the feature branch on top of the current master branch, effectively incorporating the changes from master into your feature. If there are any conflicts between the two branches, Git will prompt you to resolve them before continuing.

Once the rebase is complete, you can switch back to the master branch and merge in the changes from the feature branch as usual:

bash
$ git checkout master
$ git merge feature

This will merge the changes from the feature branch, including the changes from master that you integrated using git rebase.

Git Revert

In Git, the git revert command is used to undo a specific commit by creating a new commit that reverses the changes made in the original commit. This is different from the git reset command, which removes commits entirely and rewrites the commit history. Here's an example:

Suppose you have a branch called master, and you've made a commit that introduces a bug into the code:

bash
$ git checkout master
$ git commit -m "Introduce bug into code"

To undo this commit, you can use the git revert command:

bash
$ git revert HEAD

This command will create a new commit that undoes the changes made in the previous commit. If there are any conflicts between the commit being reverted and other changes in the repository, Git will prompt you to resolve them before creating the new commit.

Once the revert commit has been created, you can push the changes back to the remote repository as usual:

bash
$ git push origin master

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!