Git is a distributed version control system designed to handle version control and collaboration issues with code. It is widely used in open source and commercial projects, offering advantages of speed, efficiency, and security.
Installing Git#
Before using Git, you need to install it. You can download the latest version of Git from the Git official website download page.
Setting Username and Email#
To set your Git username and email, you can enter the following commands in the terminal, replacing "Your Name" and "[email protected]" with your own name and email address:
Set username:
git config --global user.name "Your Name"
Set email:
git config --global user.email "[email protected]"
Note: Replace "Your Name" with your username and "[email protected]" with your email address.
The global parameter --global
indicates that it applies to all Git repositories. If you want to set a different username and email for the current project, you can remove the --global
parameter and use the same commands in the project directory.
Creating a Repository#
Before using Git for version control, you need to create a Git repository. The Git repository stores your project code and history. Open the terminal or command prompt in your project's root directory and run the following command to create a new Git repository:
git init
This will create a .git
folder in the current directory, which contains all the metadata for Git. You can skip this step if your project already exists.
Creating and Switching Branches#
Suppose you want to create a new branch called feature
. Execute the following command:
git branch feature
Then switch to the feature
branch by executing the following command:
git checkout feature
Adding Files#
In Git, you need to add files to the staging area and then commit them to the local repository. Run the following command to add a file to the staging area:
git add filename
Note: Replace filename
with the name of the file you want to add to the staging area.
If you want to add all files in a directory, you can run the following command:
git add .
Note: Not recommended.
Committing Changes#
Run the following command to commit the changes in the staging area to the local repository:
git commit -m "commit message"
Note: Replace commit message
with a meaningful message for the commit.
When committing, please add a meaningful commit message so that others can understand the changes you made.
Viewing Commit History#
Run the following command to view your commit history:
git log
This will display all the commit history in your local repository, including the author, date, and commit message for each commit.
Pushing Changes#
Finally, you need to push the changes in your local repository to the remote repository. Run the following command to push the changes to the default remote repository:
git push
If you want to push the changes to a different remote repository, use the following command:
git push remote-name branch-name
Note: Replace remote-name
with the name of the remote repository and branch-name
with the name of the branch you want to push.
Pulling from a Remote Repository#
To pull code from a remote repository, you can use the following command:
git pull origin master
Note: master
is the name of the remote branch.
This command will merge the master
branch from the remote repository origin
into the current branch of your local repository. If you need to pull code from other branches, you can replace master
with the corresponding branch name. Note that if you have made local modifications, Git will prompt you to commit or discard the changes before pulling.
Fetching from a Remote Repository#
You can use the git fetch
command to fetch the latest commit records from a remote repository, but it will not automatically merge them into the local repository. This command only downloads the latest code to your local repository without affecting the local code. If you want to view the commit history of the remote repository, you can use the git fetch
command.
git fetch origin master
This command will download the latest commit records from the master
branch of the remote repository origin
to your local repository without affecting the local code. If you want to merge the latest remote code into the local repository, you can use the git merge
command or git pull
command.
Cloning from a Remote Repository#
If you want to get the code from a remote repository on another machine, you can execute the following command:
git clone remote-url
Note: remote-url
is the URL of your remote repository.
Adding a Remote Repository#
You can use the git remote add
command to add a remote repository:
git remote add origin remote-url
Note: origin
is the alias of the remote repository and remote-url
is the URL of the remote repository.
You can use the git remote -v
command to list all the added remote repositories and their URLs.
Setting the Default Remote Repository Branch#
You can use the git branch --set-upstream-to
command to associate a local branch with a remote repository branch and set it as the default remote repository branch. For example, to associate the current branch with the remote repository branch origin/master
and set it as the default remote repository branch, you can execute the following command:
git branch --set-upstream-to=origin/master
This command can also be shortened using the -u
or --set-upstream
parameter:
git branch -u origin/master
Note: origin
is the alias of the remote repository and master
is the branch name.
This will set the upstream branch of the current branch to origin/master
and make it the default remote repository branch. Note that when using the git push
command on the local branch, Git will use the default remote repository branch.
Merging Branches#
You can use the git merge
command to perform a merge operation. To merge the master
branch into the current branch, execute the following command:
git merge master
This command will merge the master
branch into the current branch. This operation will merge all the changes made on the master
branch into the current branch. During the merge, Git will attempt to automatically resolve conflicts. If conflicts are found, manual resolution is required.
Previewing Differences#
When you want to preview which files will be changed before performing a merge operation, you can use the git diff
command. The following command will preview all the files in the current branch that are different from the master
branch:
git diff master
You can also use the git merge --abort
command to undo a merge operation. This command will undo the most recent merge operation and restore the codebase to the state before the merge.
Help#
Git provides many commands and options. You can use the git help
command to access the Git documentation. Here's how to use it:
Open the terminal (Git Bash for Windows), navigate to the directory where your Git repository is located, and enter the following command:
git help
When you run this command, Git will display a concise list of Git commands that you may find useful.
If you want to view the help documentation for a specific command, you can use the git help
command followed by the command name. For example:
git help commit
This will open the help documentation for the commit
command, providing detailed explanations and usage instructions.
In addition to using the git help
command to access the help documentation, you can also refer to the official documentation for assistance.