Understang Git & Github with Unity
Objective: Mastering Git Branch
A git branch is root of your work directory and when you create new branch it goes off to the side with exact copy of your work directory. A git branch contain a history or state how project was at specific time.
A branch represents an independent line of development, and this possible to work on both of them in parallel, without affecting each other.
In order to start using branch we have to know how to create a branch.
we use this command to create a branch: “git branch <nameof branch>”.
It’s important to understand that branches are just pointers to commits. When you create a branch, git create a new pointer, it doesn’t change the repository in any other way.
If you start with a repository that looks like this.
Then create a dev branch
The second thing is to know how to list branches we use command git branch without name this mean list all branch we create
we currently we have two branch and the one highlighted in green with asterisk on left is a current active branch.
Working with branch
I have create a master a script in unity editor while am active on master branch , then add all files and commit .
Now lets switch on dev branch and see if we have master script, to swithch between branch we use command “git checkout dev” or “git switch dev”.
Switching Branch
This prove by using branch we can work at other branch and not affect other branch completely. Because when we switch to dev branch master script disappear.
Merging Branch
when we say “git merge master” while we are active on dev, the dev branch wont loss any thing the merge command will add anything which is not part of dev branch and add it to to dev.
First lest create a dev script while we are active on dev branch
Then lets merge the master to the dev.
Dev branch is a branch where all a development activities take place , in order to start development we have to merge all feature starting template from master to dev branch start developing and testing before release new features. This is a safe way when developer work with different features before merge to the master branch or release branch.
and merge dev to master
we normally merge our dev branch to master when dev branch is completely tested developer and make sure is ready to be merged .
Pushing our local Branch To remote server
Pushing our local dev branch to github and create new remote dev branch , but first lets switch to dev branch to make sure we are active on dev branch .
Lets go to github to verify this
Reverting or Resetting by Branch
This git Feature allow us to go back in time to a specific commit
In order To do this first to view a commit history we use command “git log” to view all history with its hash code .
Then a command to revert by branch is “git checkout <Hashcode>” , these allow us to go to specific commit as a branch but its temporary .
This enable us to verify to a specific state without loosing anything
Other way we can turn that hash code to branch and start work in that point while we have our master , when we want to add anything to old branch it became easy to grab any feature from master and turn to branch using hash code and merge to the new branch we create using first hash code.
Note: To exist the log history type q
Now we can jump back to previous state by switching to master and have the same code as before
Reset our Project when we create unity Project
In order to do these we use command called
“git reset — hard <commit Hashcode>”
First we have to get the commit hash code whether in git log or github for in a branch we want to reset , for us its master.
Second lets go to git bash and write those command to reset our local project on master branch.
The Third step is to push this change to our remote repo, we use this command to push the reset changes to to remote repo.
“git push --force origin master”
The best practices when come to revert and reset is better to branch your changes than to do hard reset , you can loose months work by hard reset.