I myself am currently learning about github, and I have summarized what I researched in various videos and blogs.
GitHub is a source code management service, which is an indispensable service for engineers because it has SNS functions in addition to browsing the code published on GitHub and simple bug management functions.
3 essential prior knowledge to use Github
- Locas repository and Remote repository: Repository is a place to save the state of files and directories. —-> By placing the directory for which you want to manage the change history under the control of the repository, you can record the change history of the files in that directory. There are two repositories, a local repository on your machine and a remote repository on the network such as a server. Basically, work is done in the local repository, and the work contents are uploaded to the remote repository.
- Commit and push: Commit is the operation to save the history of file additions and changes in the repository, and push is the operation to upload the contents to the remote repository.
- Branch: A branch is a branch that records the flow of addition / change history. Since the branched branch is not affected by other branches, each development can be performed in the same repository. Used to perform multiple version controls in parallel.
How to use GitHub
Basically, it is common to commit in small units of work and push when the work has settled down to some extent.
- Create an account on Github, and make a new repository (name: example)
- Make a folder (name: example ) and repository (git init) on local.
- Make a file (hello.html) and [git add hello.html]
- Commit the file [git commit -m “add a new file”]
- [git status] check what’s in it
- Push the local repository and reflect it in the remote repository [git remoto add origin URL]
- [git push origin master]
- Enter emai address and password of github
How to use Branch
- Display git branch list [git branch]
- Create a branch [git branch sub1
- Move to the branch [git checkout sub1]—–When creating and moving at the same time [git checkout -b sub1]
- Make a file (in sub1) bye.html adn add [git add bye.html]
- [git commit -m “add file bye!
- Reflect in remote repository [git push origin sub1]
- In case of another developer pull the sub1 [git checkout sub1] [git pull]
About merging branches
At the actual development site, new functions are performed in a branch, and when the test is completed, they are incorporated into the main master branch.
- Switch working branch to master [git checkout master]
- Merge the work results of the sub1 branch [git merge sub1]
- Push the merged one to Github [git push origin master]
About deleting branches that are no longer in use
●In the real world, it’s common to leave a branch unless you accidentally created it.
- [git branch -d sub1]
You can undo changes to a file up to a specific commit point. There are various ways, but this time I will use the restore command
- [git restore . –source #### ] ←where you want to go back
- [git restore . –source HEAD~2] ←Revert all up to 2 previous commit