Git is a widely used tool in the field of programmers. Proficient use of it can bring great convenience to project management and collaboration. As a novice programmer, I started learning it in early 2019 and began applying it in practice. Since then, I can't live without it. Only those who use it know the benefits of version control, management, standardization, and collaboration.
However, I am not completely proficient in using it yet. I only know some basic applications, so I will organize them first for review and upgrade.
Resources#
There are many learning resources about GIT online. I recommend Dequan Technology Station, which provides detailed explanations of basic concepts.
Creating a Repository#
git init <dir>
creates an empty repository.
git clone https://github.com/Ekkone/hero_robot
creates a copy of a remote repository. I usually use this method because it is convenient for synchronization.
Basic Operations#
git status
is the most frequently used command to check the status of the current repository.
git add .
submits all changes in the working directory to the staging area. You can replace '.' with the filename you want to submit.
git commit -m 'Added self-check module, tested'
submits the changes in the staging area to the local repository. You can add a log message in the single quotes.
git commit -am 'Added self-check module, tested'
combines the previous two commands and directly submits the changes in the working directory to the local repository.
git log
displays the log information. You can add --oneline
to display a simplified log. The string before the log is the version number. You can add --graph
to show the branch merging situation with a graph.
git checkout -- .
undoes all changes in the working directory. You can replace '.' with the filename you want to undo.
git reset HEAD .
undoes all changes in the staging area. You can replace '.' with the filename you want to undo.
git reset --hard <version number>
undoes the changes in the local repository to a specific version. This is called version rollback. You don't need to include the entire version number, the first five or six digits are usually enough.
Branch Management#
git branch
shows all local branches.
git branch branchName
creates a branch.
git checkout branchName
switches to the specified branch.
git checkout -b branchName
creates a branch and switches to it automatically.
git branch -d branchName
deletes a merged branch. -D
is used to delete an unmerged branch.
git merge branchName
merges a branch. If there are conflicts, you need to use git add
to indicate that the conflicts have been resolved.
Tags#
git tag tagname
adds a tag to the current version.
git tag tagname <version number>
adds a tag to a specific version.
git tag -a tagname -m <remark>
creates a new tag with a remark.
git tag
lists all tags.
git show tagname
displays information about a tag.
git tag -d tagname
deletes a local tag.
git push origin tagname
pushes a tag to a remote repository.
git push origin --tags
pushes all tags to a remote repository.
git push origin :refs/tags/tagname
deletes a tag from a remote repository.
Remote Repository#
git push origin
submits the local master branch to the remote git repository.
git push origin <remote branch name>:<local branch name>
submits a local branch to a remote branch.
git push --set-upstream origin <remote branch name>
creates a remote branch for a local branch.
git branch -a(-r)
shows remote branches.
git push origin --delete <remote branch name>
deletes a remote branch.
end#
Currently, I haven't used all the features of git myself, so here are just some commonly used ones. I will continue to expand in the future!