git command-line basic operations
- Install Git
There are many Git installation tutorials on the Web, if you need a graphical interface, it is recommended to use Git GUI or gitk under Windows Tortoisegit,linux.
- Git basic configuration
git config can configure git parameters and use git config--list to view the configured git parameters. There are three levels of save locations,--system 、--Global 、--Local, representing all users (this system), current user (global), local configuration (current directory), and--local by default.
Configure user name and mailbox
Before you can commit using GIT, you must configure your user name and mailbox, which is permanently saved to your history.
git config--global user.name "Tocy"
git config--global user.email [email protected]
Other configurations
If under Windows It is recommended or configured under the default text editor core.editor and the diff analysis tool Merge.tool.
- Create a git repository
You can directly invoke GIT init to initialize the current directory, that is, to create a git repository.
- Get a git repository
If you need to clone a remote repository, you can use Git clone, such as:
git clone https://git.oschina.net/Tocy/SampleCode.git
- Submit Updates
In the Git GUI under Windows, the commit is simple, right-click-tortoisegit-commit. So what do I need to do with the command line?
There are three states for each file in Git: Committed, staged, modified. The relationship between them is as follows:
Commit <--Stage <--Modify
Commit------modify
All the files in the Git repository that we get are committed state, and if you change the state of the file a,a locally, it becomes modified, if you use git add A,a to staged, and if you use Git commit, A's state becomes commited. This state change also indicates that it is convenient to copy the code, but be cautious about submitting updates.
Of course there is a file state, not tracking status (unversioned/untracked), by using git add can change the non-tracking state to staged, through the git RM can turn staged or committed state into the non-tracking state.
git status
Usually before committing to check what is modified under the current Git directory the status of each file.
$ git status
On Branch Master
Initial Commit
untracked files:
(Use "Git add
Readme.md
Nothing added to commits but untracked files present (use "Git add" to track)
git add
Git add can add a file or directory, or you can use wildcards. Like what:
git add readme.md # Add File only
git add *.cpp # Add all CPP files
Git add/home/code/# Add all Files In/home/code
Git diff
Git diff can see all the changes in the current directory.
Before submitting, it is also a separate confirmation of which files are in the staged state and that the modifications are correct. In practical applications, you may also need to use Git diff to export patches for code-for-the-daytime.
You can use Git diff--staged or git diff--cached to see the difference between staged and the last commit snapshot.
Git commit
Be cautious before submitting. Calling git commit directly will pop up the editor and enter the commit log (if it's a multi-line log, recommended).
For single-line log submissions, you can use the following command: Git commit-m "Add Readme".
There is also a quick way to submit, directly skip the stage buffer, directly submit all the changes in the current directory of Git commit-a (before using this command, it is recommended to confirm that the current directory changes are correct, must).
git rm
git rm deletes files from the current directory (the deleted files are not saved). If you need to remove from the Git repository but remain in the current working directory, i.e. removed from the tracking list, you can use git rm--cached readme.md.
- Submit History View
You can use git log to view all the submitted logs for the current project.
git log--stat # Show only summary options
git log--pretty=oneline # custom record format
git log--graph # Image branch and version update
- Undo Update
- Remote Warehouse
You can use Git remote to view the current remote library.
Git remote-v can display the corresponding clone address. (Useful for multiple remote repositories)
Add a remote repository
Git remote add short_name can add a new repository.
Fetching data from a remote repository
git fetch [remote-name] can fetch data from the remote repository locally.
You can also use Git pull
Push data to a remote repository
git push remote_name
Origin and master are used by default.
View Remote Warehouse Information
git remote show origin
Deletion and renaming of remote warehouses
git remote rename Old_name
Git remote RM [Remote_name]
- Play tags
You can use git tag to display the tags in the current library.
Add tags (with notes)
git tag-a v0.1-m "my version 0.1"
Use the following command to view tag log information (Specify the name of the corresponding tag)
Git show v0.1
You can also create tags using the SHA-1 submission representation:
Git tag-a v0.2 [SHA-1]-M "My version 0.2"
Share tags
By default, git push does not push tag information to the remote repository and requires explicit push via commands.
Git push Origin v0.1
If you need to push all the labels, use the
Git push Origin--tags
- Git branch
The GIT branch is lightweight and fast, logging only the index information.
Show All Branches
Use Git branch to display all current branches.
You can use--merged and--no-merged to view the branches that have been merged and not merged.
Creating and switching branches
You can switch and create a branch directly using the following command
git checkout-b Testing
Equivalent to
Git Branch Testing # Create Testing Branch
git checkout testing # switch to Testing branch
Note When you switch branches, keep the working directory with no uncommitted modifications. GIT encourages the use of branches, and then merges the branches after the problem is done.
Branch Merge
Merging the hotfix branch to master (the main branch) requires the following command:
git checkout Master
Git merge Hotfix
After merging, you can use Git branch-d hotfix to delete a branch.
If there is a conflict in the merge, you need to modify it manually.
Git learns C for some old brother, (invasion)