How To Use Git Effectively

本文介绍如何使用版本控制系统Git来简化编程项目的开发流程。首先确保已安装并配置好Git,然后通过创建项目、初始化仓库、添加文件、提交更改及推送远程服务器等步骤,掌握基本的Git工作流程。

https://www.digitalocean.com/community/tutorials/how-to-use-git-effectively

This article assumes that you have git installed and that your global configuration settings (namely username and email) are properly set. If this is not the case, please refer to the git introduction tutorial.

Git is a very useful piece of software to help streamline development for programming projects. It comes with no language requirements nor file structure requirements, leaving it open for the developers to decide how they want to structure their workflow.

Before using git for your development, it's a good idea to plan out your workflow. The workflow decision is typically based on the size and scale of your project. To gain a basic understanding of git for now, a simple, single-branch workflow will suffice. By default, the first branch on any git project is called "master". In a following tutorial in this series, you will learn how to create other branches.

Let's create our first project and call it "testing". (If you already have a project that you want to import to git you can skip down to that section.)

Creating your workspace

Just like you want to have a good, clean work environment, the same idea applies to where you do your coding, especially if you're going to contribute to a number of projects at the same time. A good suggestion might be to have a folder called git in your home directory which has subfolders for each of your individual projects.

The first thing we need to do is create our workspace environment:

user@host ~ $ mkdir -p ~/git/testing ; cd ~/git/testing

The above commands will accomplish two things: 1) It creates a directory called "git" in our home directory and then creates a subdirectory inside of that called "testing" (this is where our project will actually be stored). 2) It brings us to our project's base directory.

Once inside that directory, we need to create a few files that will be in our project. In this step, you can either follow along and create a few dummy files for testing purposes or you can create files/directories you wish that are going to be part of your project.

We are going to create a test file to use in our repository:

user@host ~/git/testing $ touch file

Once all your project files are in your workspace, you need to start tracking your files with git. The next step explains that process.

Converting an existing project into a workspace environment

Once all the files are in your git workspace, you need to tell git that you want to use your current directory as a git environment.

user@host ~/git/testing $ git init
Initialized empty Git repository in /home/user/git/testing/.git/

Once your have initialized your new empty repository, you can add your files.

The following will add all files and directories to your newly created repository:

user@host ~/git/testing $ git add .

In this case, no output is good output. Unfortunately, git does not always inform you if something worked.

Every time you add or make changes to files, you need to write a commit message. The next section describes what a commit message is and how to write one.

Creating a commit message

A commit message is a short message explaining the changes that you've made. It is required before sending your coding changes off (which is called a push) and it is a good way to communicate to your co-developers what to expect from your changes. This section will explain how to create them.

Commit messages are generally rather short, between one and two sentences explaining what your change did. It is good practice to commit each individual change before you do a push. You can push as many commits as you like. The only requirement for any commit is that it involves at least one file and it has a message. A push must have at least one commit.

Continuing with our example, we are going to create the message for our initial commit:

user@host ~/git/testing $ git commit -m "Initial Commit" -a
[master (root-commit) 1b830f8] initial commit
 0 files changed
 create mode 100644 file

There are two important parameters of the above command. The first is -m, which signifies that our commit message (in this case "Initial Commit") is going to follow. Secondly, the -a signifies that we want our commit message to be applied to all added or modified files. This is okay for the first commit, but generally you should specify the individual files or directories that we want to commit.

We could have also done:

user@host ~/git/testing $ git commit -m "Initial Commit" file

To specify a particular file to commit. To add additional files or directories, you just add a space separated list to the end of that command.

Pushing changes to a remote server

Up until this point, we have done everything on our local server. That's certainly an option to use git locally, if you want to have any easy way to have version control of your files. If you want to work with a team of developers, however, you're going to need to push changes to a remote server. This section will explain how to do that.

The first step to being able to push code to a remote server is providing the URL where the repository lives and giving it a name. To configure a remote repository to use and to see a list of all remotes (you can have more than one), type the following:

user@host ~/git/testing $ git remote add origin ssh://git@git.domain.tld/repository.git 
user@host ~/git/testing $ git remote -v
origin	ssh://git@git.domain.tld/repository.git (fetch)
origin	ssh://git@git.domain.tld/repository.git (push)

The first command adds a remote, called "origin", and sets the URL to ssh://git@git.domain.tld/repository.git.

You can name your remote whatever you'd like, but the URL needs to point to an actual remote repository. For example, if you wanted to push code to GitHub, you would need to use the repository URL that they provide.

Once you have a remote configured, you are now able to push your code.

You can push code to a remote server by typing the following:

user@host ~/git/testing $ git push origin master
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 266 bytes, done.
Total 3 (delta 1), reused 1 (delta 0)
To ssh://git@git.domain.tld/repository.git
   0e78fdf..e6a8ddc  master -> master

"git push" tells git that we want to push our changes, "origin" is the name of our newly-configured remote server and "master" is the name of the first branch.

In the future, when you have commits that you want to push to the server, you can simply type "git push".

I hope this article provided you with a basic understanding of how git can be used effectively for a team of developers. The next article in this series will provide a more in-depth analysis of git branches and why they are so effective.

### Git Tree Object Explained Git is a distributed version control system that uses objects to represent the state of your repository. Among these objects, the **tree object** plays a crucial role in organizing and storing file structures. A tree object represents a directory within the repository and contains references to other tree objects (for subdirectories) and blob objects (for files). A tree object stores the following information for each entry: - The file mode (e.g., regular file, executable file, or directory). - The SHA-1 hash of the associated blob or tree object. - The filename. This structure allows Git to efficiently track changes across different versions of the repository. When you commit changes, Git creates a new tree object that reflects the current state of the working directory[^1]. Here is an example of how Git handles tree objects during a commit process: ```bash # Create a new repository and add some files mkdir example-repo cd example-repo git init echo "Hello World" > file1.txt mkdir subdir echo "Subdirectory content" > subdir/file2.txt git add . # Commit the changes git commit -m "Initial commit" ``` After running the above commands, Git will create: 1. Blob objects for `file1.txt` and `subdir/file2.txt`. 2. A tree object for the `subdir` directory. 3. A root tree object that references both the `file1.txt` blob and the `subdir` tree. The commit object then references the root tree object, effectively capturing the entire state of the repository at that point in time[^5]. If you want to inspect the tree objects in your repository, you can use the following command: ```bash git ls-tree HEAD ``` This command lists the contents of the tree object referenced by `HEAD`. You can also specify a particular tree object using its SHA-1 hash: ```bash git ls-tree <tree-object-hash> ``` For more detailed information about a specific tree object, including recursive listings, you can use: ```bash git ls-tree -r <tree-object-hash> ``` This command provides a comprehensive view of all files and subdirectories represented by the specified tree object[^5]. When dealing with submodules, it's important to note that each submodule is represented as a gitlink entry in the parent repository's tree object. This gitlink points to the specific commit of the submodule's repository. To initialize and update submodules, you can use the following commands: ```bash # Clone a repository with submodules git clone --recurse-submodules -j8 <repository-url> # Update existing submodules git submodule update --init --recursive ``` These commands ensure that the submodules are properly initialized and updated to their respective commits as defined in the parent repository's tree object[^2]. ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值