- vừa được xem lúc

Chapter 3: Basic Git Commands and Workflow

0 0 37

Người đăng: Duc Nguyen

Theo Viblo Asia

This chapter belong to Getting Started with Git: A Beginner's Guide Series

In this chapter, we will be diving into the basics of using Git, including how to use essential Git commands and the basic workflow of using Git. Before diving into the commands, it's important to understand what Git is and what it does. Git is a distributed version control system, which means that it keeps track of changes made to files and directories over time. The changes are tracked by creating a repository, which is a central place to store all changes. The repository contains all the information about the current version of the files, including all the previous versions, making it easy to revert to an older version if needed.

In order to use Git, you'll need to have it installed on your computer. Once installed, you can start using Git from the command line. To help you understand how Git works, we will go over some of the most commonly used Git commands.

Git terms

  • Working Area: This refers to the place where you do your day-to-day work. This is where you create, edit and save your files.
  • Staging Area: Also known as the "Index", the staging area is a place where you can collect changes that you want to commit to the repository.
  • Local Repository: The local repository is a place where you store the changes you make to your working area. It is stored in your computer and acts as a backup for your work.
  • Remote Repository: This is a copy of your local repository stored on a remote server. This allows you to share your work with others and collaborate on projects.
  • Commit: A commit is a saved version of your changes. When you commit changes, Git takes a snapshot of your working area and stores it in your local repository.
  • Branch: A branch is a separate line of development in Git. You can create a new branch to work on a new feature or bug fix, and then merge the branch back into the main branch when you are done.

Git Workflows

The basic Git workflow starts with creating a new repository, making changes to your files, and then committing those changes to your local repository. When you are ready to share your work, you can push your changes to a remote repository. When collaborating with others, you can pull changes from the remote repository to your local repository.

It is important to regularly commit your changes and push them to a remote repository to keep your work safe and to make it easier for others to access your changes. When working on a larger project, it is a good idea to create a new branch for each feature or bug fix you work on. This way, you can easily switch between different branches and merge your changes back into the main branch when you are done.

By following these basic Git commands and workflow, you will be able to effectively manage your projects and collaborate with others using Git.

Git commands

  1. git init This command is used to initialize a new Git repository. It creates a new directory in your current location, which acts as the repository. The command is used to start a new project and create a local repository for it. Example:
    $ git init
    
  2. git clone This command is used to clone an existing Git repository. It creates a local copy of a remote repository, which you can then use to make changes and push them back to the remote repository. Example:
    $ git clone [repository-url]
    
  3. git status This command is used to check the status of a Git repository. When you use this command, Git will show you which files have been modified, added, or deleted. It will also show you which changes are staged for committing. Example:
    $ git status
    
  4. git add This command is used to stage changes for committing. When you make changes to a file, you'll need to use this command to add those changes to the staging area. The staging area is a place where changes are kept temporarily until they are committed. Example:
    $ git add [file-name]
    
  5. git commit This command is used to commit changes to the repository. When you use this command, all changes in the staging area are committed to the repository, along with a message that describes the changes. Example:
    $ git commit -m "commit message"
    
  6. git push This command is used to push changes from a local repository to a remote repository. When you use this command, all changes that have been committed are pushed to the remote repository, updating it with the latest changes. Example:
    $ git push [remote-repository-name] [branch-name]
    
  7. git pull This command is used to retrieve changes from a remote repository and update a local repository with those changes. When you use this command, Git will download any changes that have been made to the remote repository and merge them with your local repository. Example:
    $ git pull [remote-repository-name] [branch-name]
    
  8. git log This command is used to view a log of all changes that have been committed to a repository. When you use this command, Git will display a list of all commits, along with the date, author, and commit message for each commit. Example:
    $ git log
    
  9. git stash The git stash command is used to temporarily save changes that have not yet been committed to the repository. This is useful when you need to switch to a different branch to work on a different task, but do not want to commit your current changes. The stash allows you to save your changes and retrieve them later when you are ready to continue working on the same task. To run the git stash command, simply type "git stash" in the terminal or command prompt. Example:
    $ git stash
    
  10. git cherry-pick The git cherry-pick command is used to apply a specific commit from one branch to another branch. This is useful when you want to incorporate changes made in one branch into another branch, without having to merge the entire branch. To run the git cherry-pick command, simply type "git cherry-pick [commit_hash]" in the terminal or command prompt, replacing [commit_hash] with the hash of the specific commit you want to apply. Example:
    $ git cherry-pick [commit_hash]
    

This chapter provides an overview of how these commands and stages work together to help you manage your code changes in Git. The examples provided in this chapter give you a hands-on understanding of how to use these commands effectively in your Git workflow. By the end of this chapter, you should have a clear understanding of the basic Git commands and how to use them to manage your code changes.

Bình luận

Bài viết tương tự

- vừa được xem lúc

Đặt tên commit message sao cho "tình nghĩa anh em chắc chắn bền lâu"????

. Lời mở đầu. .

1 1 770

- vừa được xem lúc

Tập hợp những câu lệnh GIT hữu dụng

Dưới đây là một vài ví dụ về các câu lệnh Git mà tôi thường dùng. git config --global user.name "John Doe". git config --global user.

0 0 68

- vừa được xem lúc

Cấu hình CI/CD với Github (phần 2): Trigger một work flow

Events trigger. Bạn có thể cấu hình cho workflows chạy khi có một sự kiện nào đó xảy ra trên GitHub, theo một lịch có sẵn hoặc cũng có thể là một sự kiện nào đó xảy ra ngoài GitHub.

0 0 80

- vừa được xem lúc

Cấu hình CI/CD với Github (phần 1): Một ít lý thuyết

CI/CD là gì. Về mặt khái niệm là vậy nhưng về mặt triển khai thì CI/CD là quá trình tự động thực hiện các quá trình build, test, release, deploy khi có các trigger như commit/merge code lên một branch định sẵn hoặc có thể là tự động chạy theo một lịch cố định.

0 0 128

- vừa được xem lúc

Giới thiệu về Git LFS

. Git LFS là gì . Git LFS làm điều này bằng cách thay thế các tệp lớn trong repo của bạn bằng một con trỏ nhỏ.

0 0 37

- vừa được xem lúc

Git workflow được Google và Facebook sử dụng có gì hay ho

Với developer thì Git hẳn là công cụ rất quen thuộc và không thể thiếu rồi. Thế nhưng có mấy ai thực sự hiểu được Git.

0 0 85