Git - Daily work commands
This post will give a summary of the git commands and a parallel view of the tree. It is not another tutorial. There are many really good ones.
PS: The trees created here was done by gitHub School.
First of all...
- Config the account
-
Each commit is uniquely identified (SHA1 hash): 79fc79530ecf44809d0aaeb93830ec23a5254d66
-
You just need the first seven values in hash code to give as reference in git commands. For example, you just need 79fc795 to do some reference to the previous commit.
Starting by Local works
Here is a summary about the first steps. After the Step 4 you will have the folder '.git'.
Now, let's improve the scenario adding a new file.
And here is how supposed to be the tree.
Now, let's change both files (adding any text inside the files) and commit them.
Next level ... let's revert the commits.
git revert is used to record some new commits to reverse the effect of some earlier commits (often only a faulty one). This requires your working tree to be clean (no modifications from the HEAD commit).
The step 15 will go back the files to the state before your changes (original files) and the Step 17 return your changes (changed files).
If you want to throw away all uncommitted changes in your working directory, you should see git-reset.
The step 20 move the pointer the a specific position on the tree. You just need the first seven first number of the commit's hash. Bellow is how is supposed to be the tee.
Change the branch
Until now, all the works is happening using the master branch. Now, let's create a new one.
Let's create a new file to this branch.
Merge branches
You need to be on your branch target. Here, we will merge from the newbranch to the master. So, checkout to the master and let's go.
Here is the tree after the merge to master.
If you want to remove the branch you just need use the attribute 'd'. And here is the tree after that.
The first example show the case where the new branch has everything the master. The image below show another example where the master has a new commit and the other branch also has different commits.
In case happen changes in the same file in both branches the console will the the conflict's message. You just need to open your file, fix that and commit again. The git diff show in the console all the changes in the files.
Working with Remote Repositories
When you create the repository remotely the GitHub show you all the directions to synchronize it with a local repository. You just need follow it. For example used in this post you should follow the second option.
However, if you have more then one branches and want push all of them you can use git push --all origin. In case you want let the master branch default you can write git push -u origin master and the last time you just need to write git push.
Other case is when exist the project in the remote repository and you want just use it. For that you have to clone the repository.
Before the pull, you can use stash to store locally the changes you are not prepared to commit yet. And put back the code using git stash pop. If you want do a clean commit you can use git squash.
Probably will be necessary configure the key ssh to use the remote repository. One possibility is you have to generate new SSH key locally and copy in the github repository. Other case you can create in your repository (Settings/Developer Settings/Personal access tokens) and copy locally when try to be logged.
Example of the Tree
Here are the steps regarding an example of using remote repository. Below you will see commits, merges and push to remote repository. Pay attention: The HEAD in remote repository is changed only after the merger.
Reference
- Git-school.github.io/visualizing-git/#free
- Udemy: Learn Git by Doing: A step-by-step guide to version control (Codingdojo, Inc)
- Video Fabia Akita: https://youtu.be/6Czd1Yetaac