Common Git commands, git
Repository 1. clone a remote repository to a local device
- HTTP: git clone https://github.com/linus87/GitTest.git
- SSH: git clonegit@github.com: linus87/GitTest. git
- GitRead-OnlyMethod: gitclone git: // github.com/linus87/GitTest.git
In general, both HTTP and SSH methods are supported by GIT, but they are not absolute. If HTTP does not work, change to SSH. And vice versa.
For a custom directory, it may be as follows:
git clone git@github.com:username/GitTest.git my_gittest
Specify the clone of the repository. This method will create all branches in this repository, but only one of the currently active branches is checked out.
2. delete a repository
git remote remove <repository-name>
3. Rename the Repository
git remote rename <old> <new>
View 1. View branch status
Including modified files. Files not tracked are displayed here.
git status
Stage modified file:
git add project/src/main/webapp/css/tab.css
2. view the status of a single file
git status -- project/src/main/webapp/css/tab.css
3. view the submit log of a single file
git log -- project/src/main/webapp/css/tab.css
Comparison 1. Compare the differences between a single file in the work tree and the previous commit
git diff -- project/src/main/webapp/css/tab.css
Submit 1. Discard the changes made to the modified file (changes made by stage)
git reset<paths>
By default, paths is a HEAD object and can be customized as other file names and paths.
For example, git reset -- frotz. c
In addition, using git reset for a file will remove a newly added file from the tracked record, which is the reverse command of git add.
2. Submit changes
git commit –m "commit messages"
3. files that are automatically changed or deleted at the time of submission
Git commit-a or git commit-all
4. Submit rollback
git reset -- soft commits
Commits format: HEAD, HEAD ^, HEAD ~ 2, representing the last three submissions.
Edit index
The index content is modified by staged.
1. New Stage files or modified files
git add project/src/main/*.css
Edit workingtree
The leaf node of the working tree is a file that has been modified (but not added to the index, that is, it is not staged.
1. Save the local modification, but do not add the index and roll back to the HEAD.
git stash
Edit 1. delete files from the work tree and also delete files from the index
git rm -- a.css
2. Remove the file from the index, but keep the file and the modified content.
git rm --cached -- a.css
3. delete folders with files (all files are not modified)
git rm -r folder
4. Force delete a folder (if any file is changed)
git rm -r -f folder
5. fetch from another warehouse or local branch and merge
git pull origin master
Explanation: origin is a repository name and master is a branch.
Branch 1. List local branches
git branch
2. List the branches on the remote server
git branch --remotes
3. List remote and local branches at the same time
git branch -a
4. Create a branch
git branch <branchname>
This is only a local branch. After the local branch is successfully created, you also need to submit it to the remote server through git push.
5. Set Tracking Information
git branch --set-upstream-to=origin/<branch>
6. delete a local branch
git branch (-d | -D) <branchname>
The difference between-D and-d is that-D indicates force deletion, ignoring the merge status.
7. delete a local remote Branch
git branch (-d | -D) -r origin/<branchname>
This command does not delete the real remote branch, but deletes the local remote branch. Next time, git fetch/pull will retrieve the real remote branch again.
To delete a real remote branch, you must log on to github to delete the real remote branch.
8. Modify the branch name
git branch (-m | -M) [<oldbranch>] <newbranch>
Configuration 1. How to configure so that git only pushes the current Branch
git config --global push.default current
The value of push. default may be: nothing, matching, upstream, and current.
If this parameter is not configured, you must manually write it as follows:
git push origin my-branch
2. Configure the user name and email address used for display
git config user.name Linus Yangit config user.email username@email.com
3. Save the user name and password used for code submission.
git config crediential.https://github.example.com.username linus
The password is a little complicated and needs to be saved in two ways:
- Save in cache:
- Save on Hard Disk:
git config --global credential.helper 'store --store=~/.git-credentials'
For a single project, the global settings are used:
git config credential.helper 'store'
The content of the. git-credentials file:
Format: https: // user: pass@example.com
In the following example, the token is used to replace pass (because 2FA is used ):
https://linus:9911b3a8a11349b1fdd38879b5f4756f8379324c@github.example.com
Merge 1. Merge history of current Branch
git merge topic
Because git is a distributed system, the history on the local and server may be inconsistent. Therefore, you need to merge the local and local files on the server.
Topic refers to a named commit on the local branch.
2. Discard Merging
git merge --abort
Prerequisites: You cannot submit any changes before using git merge. Otherwise, git cannot be restored.
Working directory 1. Conversion Work Branch
git checkout <branchname>git pull
First, use git checkout to replace the current work branch, and then use git pull to get the latest code on the server.
Sometimes git pull is invalid. Use the following command:
git pull origin <branchname>
2. merge two different branches
git checkout <branchname>git pull
First, checkout the other branch to the local machine and get the latest code, and then return to the current Branch
git merge<branchname>
Use merge.
Simultaneous development of primary and secondary branches 1. Merge primary and secondary branches
Assume that the current branch is a topic:
A---B---C topic
/
D---E---F---G master
// The topic is a secondary branch and the master is the primary branch.
Git rebase master topicThe following will be:
A' -- B '-- C' topic
/
D---E---F---G master
Modify project repository Sourcegit remote set-url origin git://github.scm.corp.ebay.com/my-org/my-repo.git