- I refer to github and in the list of commands below it is supposed to have the account to access it
- purpose of this post is to summarize the list of commands for the management of a remote repository
- all commands are typed into shell
Enter the project directory that you want to save on github and type:
A .git hidden directory is created. With the command:
you can see the current status of saved files in the repository.
Saving a project on github it is a good practice to provide a license file and a readme file. It is therefore advisable to create a LICENSE.txt file and a README.md file. It is possible to find the definitions of the software licenses on the choosealicense.com website through which we can choose which license to use and copy the text in our file. Concerning the README file (the extension md stands for markdown) it is possible to download a template with all the control characters for a correct github formatting on the github/PoorpleBooth address.
Another file to create is .gitignore. The purpose of this file is to prevent git from copying in the repository unnecessary files or specific files of the local machine that are not necessary for replicating the project on another machine. In the specific case of a Microchip MPLAB project, a list of extensions and directories has been compiled, this list can be found on github/RLocelett. A very large list of programs and operating systems extensions is found in github/gitignore.
Now, that we have indicated what not to transfer, we can queue all the files of the project to be included in the repository:
git add .
and insert files into local repository.
git commit --message "first commit"
To check the operation just performed, you can use the command:
Now we need the remote repository on which to transfer the copy of the newly created local repository: you can use the wizard to create a new project on github. In the wizard it is suggested the creation of the readme files and license, do not do it because we need an empty repository. Once entered the name of the project and confirmed you access a welcome window with many useful information, what we need is the remote address of our repository:
Copy the address and type the following command from the shell (the address indicated refers to my repository):
git remote add origin https://github.com/PaoloCarlo/tutorial_1.git
To check if the operation has been successful, you can type the command:
paolocarlobernardi$ git remote -vv
origin https://github.com/PaoloCarlo/tutorial_1.git (fetch)
origin https://github.com/PaoloCarlo/tutorial_1.git (push)
All that remains is to push the local repository on the remote one:
git push -u origin master
As opposit, if you want to remotely download a project to duplicate it on your machine you use the "clone" command followed by the address of the remote repository.
If you want to create a new version of the project, or maybe a temporary variation that will be added to the original project after verification, you can create a new branch with the "branch" command.
git branch temp
We have created the "temp" branch. To drive the changes we want to insert in the repository to the "temp" branch it is necessary to change the branch, from "master" to "temp".
git checkout temp
you can verify what it has just done: the selected branch is highlighted by an asterisk.
You can create a new branch and select it with a single instruction:
git checkout -b temp
The created branch can be deleted with the command:
git branch -d temp
Once the development of the new branch is finished, to join the new code with the existing version, use the "merge" command. First you need to come back to master branch.
git checkout master
git merge temp
This operation is allowed if there are no conflicts with the primary branch.
Useful functions to remember
List below some parameters to remember as useful (update on the way). Unless otherwise indicated, they can be tested without danger.
Display the repository history:
git log --oneline --graph --all --decorate
Display which remote repository is connected as "origin":
git remote -v
Useful to understand if local repository is aligned with remote:
git branch -vv --all
To update remote repository with local:
git push origin master
To update git under mac:
brew upgrade git
For other operating systems go to git-scm.com
To cancel commit if no push has been done yet:
git reset --soft HEAD^
With the soft option, you delete the last commit action but do not touch the contents of the files that remain unchanged. HEAD^ indicates the commit before the one just made.
Wanting to bring back the files to the state before the commit, deleting all the changes made by the last commit (This operation is potentially dangerous because it modifies the repository files):
git reset --hard HEAD^
Having already pushed on the remote repository you can make a new commit that brings the repository back to its previous state (This operation is potentially dangerous because it modifies the repository files):
git revert HEAD