Git: basic usage, git usage
A few simple operations allow you to use git to upload files on the terminal.
1. clone a project
In work, the common scenario is that the remote database has been created. You need to pull down the code and work together for development. All operations in this document are performed on the terminal.
// Clone a local database
Git clone your remote repository address
2. Local Database Operations
// Add and modify
1. File Name modified by git add
// Submit the modification content
2. git commit-m "Modify remarks"
3. Submit the modified content to the remote database
// Add the-u parameter to the first push, which can be omitted later
Git push-u origin master
In this simple step, you can upload your code to a remote repository. If you have other requirements, you can continue.
4. Other frequently used operations
1. Basic operations
// Use Xcode to open a file
Open-a Xcode file name
// Keep abreast of the workspace status
Git status
// View the modification content of the file
Git diff file name
2. Version Management
// The version is rolled back to the previous version. HEAD indicates the latest version.
Git reset -- hard HEAD ^
// The version is rolled back to the previous version.
Git reset -- hard HEAD ^
// View the submission history. The commit_id of different versions is displayed.
Git log // contains a large amount of information.
Git log -- pretty = oneline
// View the command history to determine which version to return.
Git reflog
// Roll back the version to the specified version, commit_id
Git reset -- hard commit_id
3. Modify and delete operations
// You can discard the modification of the workspace, which is very important and should not be less. Otherwise, it will change to "switch to another branch"
Git checkout -- file name
// Undo the modification of the temporary storage area and return it to the workspace.
Git reset HEAD file name
// Delete an object
Git rm file name
4. Branch Management
Git recommends that you use a branch to complete a task and then delete the branch after merging. This process is safer.
1) Create and switch to the branch.-B indicates creating and switching.
Git checkout-B branch name
Is equal to two Commands: git branch name // create branch
Git checkout branch name // switch to Branch
2) list all branches. A * number is marked before the current branch.
Git branch
3) perform modification and other operations on the branch
Git add file name
Git commit-m "Modify remarks"
4) after the branch is completed, switch back to the master branch, merge the branch work results to the master branch, and then delete the branch
Git checkout master
Git merge branch name
Git branch-d branch name
When merging branches, git will first use the Fast forward mode. In this mode, after deleting a branch, the branch information will be lost.
If the Fast forward mode is forcibly disabled, git will generate a new commit at merge, so that the branch information can be seen from the branch history.
// -- No-ff parameter, indicating to disable Fast forward
Git merge -- no-ff-m "merge with no-ff" branch name
5) if a conflict occurs, you need to manually modify the statement and merge it in the submission process.
// You can see the branch merge chart
Git log -- graph
In actual development, branch management should follow several basic principles:
1. The master branch should be very stable, that is, it is only used to release new versions.
2. Where can I work? Work on the branch built by yourself
3. You and your friends each have their own branches. You can merge them on the branches from time to time.