Creating a branch in Git to add new feature to Android app - android

I am learning how to use Git for version control. One of the uses of creating branches is to add new features while master branch contains the stable code. I am using Git Bash for Windows in conjunction with Eclipse for editing the code.
So, assuming that I want to create a new feature called social-share and a branch named the same, how would I go about doing that?
Wont I first have to create a branch, commit the entire master branch to it and then add new feature code?

So I'm assuming that you already have a branch called develop where your development occurs.
The first thing to do is create the feature branch based on the current branch and switch to the created branch
$ git checkout -b social-share
Optionally if you want to push that branch to a remote Git repo to track it there:
$ git push -u origin social-share
Pushing the branch to the remote repo is dependent on your environment and whether or not you want others to work on this branch.
Once you've finished your changes to the social-share branch you'll want to merge those back into your develop branch.
First switch to develop branch
$ git checkout develop
Merge social-share into devleop*
$ git merge --no-ff social-share
Delete social-share branch
$ git branch -d social-share
Push changes to origin
$ git push origin develop
* The reason you use the --no-ff flag is so the merge uses a new commit object and to avoid just fast-forwarding the develop branch which preserves the fact that the social-share branch existed at one point.
If you want to read more about this branching model then checkout the post it was based on. Also, if you would like to learn more about git branching by actually doing it there's a great tutorial series/sandbox that you should checkout (git it?).

Related

How to update a project that already exists on github

Here's my situation....
I have created a repository on GitHub of a project. I worked on the project on a different computer then that which I have created the repository on. I now need to update the GitHub with the changes. I'm working from Android Studio. It seems the only option I have is to either create a new repository. I want to know how to update my old one without having to clone it and modify it then upload changes.
When you worked on the separate computer did you clone the repo? If so you can just push your changes. If not, you'll need to setup a remote
first init a git repo if your work is not in a git repo
git init
then add a remote
git remote add origin https://github.com/user/repo.git
use git remote -v to verify
set upstream
git remote add upstream
then pull, commit, push your changes now that your remote is setup
https://help.github.com/articles/adding-a-remote/
make a new git repo on your new PC using git init, this will add your project to a new repo on your machine.
add all your source to this new repo using a
git commit -am "whatever description of changes made"
Now checkout a new branch, call it what you like, but something like
`git checkout -b new_work`
should do the trick.
Now switch back to your master branch using git checkout master and connect your repo to your remote git repository on github
git remote add origin https://github.com/user/repo.git
You can set the upstream then using
`git remote add upstream`
Next you pull the remote code into your master branch using
git pull https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git BRANCH_NAME
Now you can merge all your new changes back into the master branch as you would in your normal workflow using
`git merge new_work`
or whatever you called your new branch.
Then push the master back to github using
`git push`
This approach has the advantage of being able to track all your changes properly and reverse them if needs be or amend update as needed whilst keeping the original code in tact and is the normal workflow for git.
Of course in the future you would pull the repo from git before making any changes and you wouldn't have this problem.
Hope that helps

Android : Maintaining several versions of my Android app on Github

I have already did my first commit of my app on Github. Now I want to maintain several versions (like 1.0, 1.1, 1.2 etc.) of my app, whenever I add a new module to my app, on Github. How to do that using android-studio VCS or using terminal?
You can create different branchs for each version. You can have your current version in a generic especific branch and before you change to a new version, create a new branch named after the version of your choice.
If the current version is 1.2, for example, and you need to create a new version, stay in your current branch, create a new branch for version 1.2 and after that return to your current branch. (it will copy all the content of the current branch to a new branch).
To create a branch you can follow this example:
git checkout -b v1.2
When you go back to the current branch, your new changes will not affect your old version 1.2. It will be safe in it's own branch v1.2.
I would go with tags. That's the correct way to manage versions (or releases) of your app.
The Git Flow approach suggest you start from the master branch, then create a new branch called develop.
git checkout -b develop
From there you will create a new branch for each feature/request/issue and after it is done, return to the develop branch.
To create a new branch, if positioning in develop
git checkout -b new-feature
Commit as much as you want in that branch and when done, upload it to GitHub
git push -u origin new-feature
If the feature is done you can create a Pull Request in GitHub and merge that branch with the develop branch.
Repeat the process until you are ready for a new version/release.
Once you are done create a pull request in GitHub to merge develop into master.
Now the master branch will have all you want for you new version.
To tag it, position on master and update it from GitHub
git checkout master
git pull origin master
Now your local branch is up-to-date. Let's tag it
git tag v1.0
An you can also upload the tag to GitHub
git push --tags
A TAG is a mark in your repository pointing to the code of the specific version/release you have tagged.

How to work with (commit) two versions simultaneously in GIT?

THE SITUATION
I am a fresh android developer and I made an easy application that I want to publish. There are two versions: one free with limitations, and an other without limitations. 99.99 percent of the code is same in the two versions, so I would like to work with them simultaneously. I have already used GIT and version control before, but only to save my data. So the point is I have never used branches.
THE PROBLEM
Of course I would like to learn to use the stuff, but now I am looking for a quick solution how to edit both of them.
So I am standing on my last commit and I would like to make the branching, but I have no idea how.
Steps for git branching :
1) Creating branch :
git checkout -b <new-branch-name>
2) Checking your current branch :
git branch
3) Switching branches :
git checkout <branch-name>
in your case
git checkout FREE or git checkout PRO
4) Make commits on your checked out branch.
5) For pushing the code to particular branch :
git push origin <branch-name>
in your case
git push origin FREE or git push origin PRO
respectively.
Hope this helps.
In order to create a branch, just run git branch <branch-name>. That will create a new branch right on the revision where you are right now. Then you can check it out (git checkout <branch-name>) so you can commit on it (nothing will the touched on the original branch). Both steps can be achieved at the same time with a single call: git checkout -b <branch-name>.
Git can’t work with two versions simultaneously, but branches in git can make it separately. So you just need to work on two branches to develop your code based on different versions.
Assume the two versions are A and B, and they both located on your default branch master. The structure looks like:
A---B master
Now you just need to create a new branch from the other version A by
git checkout -b dev <commit id for A>
Then your branch structure will look like:
A---B master
\
… dev
Then you can make change and commit separately for this two branches. For one time you can just make/commit changes for a branch since there has the same working directory.
Such as when you are working on dev branch (HEAD point to it), you can just make/commit changes based on version A. Then you can switch HEAD to point master by git checkout master, then make changes and use git add . and git commit -m 'message' to commit changes.
Finally you can use git push origin branchname to push to remote.

Creating Branches in Android Studio

I am new to Android Studio. I am wondering if I could create different branches to run my application. Likewise for example, if there is any possibility to have same copy of application but the difference would be just that ,one would run the production app and the other branch would run the test app.
So please suggest some method if available, to create branches or tags in Android Studio.
For this I would recoment to use gitflow here is an explanation image from nvie.com.
You can find the details in the link above. In short you use it to maintain features independed. You work on the development branch and your stable version is the master branch. So you can add fixes for the stable version without getting issues while fixing bugs with committed changes.
Back to your question how to add a branch in Android Studio click on the lower right edge in Android Studio and create that branch you want to:
If you need more information write a comment.
Create an account on github.com or bitbucket.com
then create a new repository for your project
Please read this git doc:
https://git-scm.com/book/fr/v1/D%C3%A9marrage-rapide
it's in french but there is an english version here https://git-scm.com :
in command line init git for your project:
cd ~/project-path-here
git init
git add .
git commit -am "initial commit"
git remote add origin <your link>
git push -u origin master
then manage your project with git.
You can create a branch with
git branch -checkout <new_branch_name>
Then you should be merge into your develop, release then master branch. You can use this git flow nvie.com

Git - How to add local repository to new Branch

I'm working on a project where we have reached a milestone. My git repository is up to date and everything is working correctly. Our client has now updated their api's and we're unsure how it will affect our code.
What I want to do is create a branch (already done) and upload the full repository to the branch. So I will have the project duplicated, one in the master and one in the new branch. I will then work and commit to the new branch leaving the master untouched. So going forward then for each release I can do the same, so we can fall back to the previous branch if there's any problem.
Can someone tell me how to do this please. I'm using Git and I'm working through a mac terminal.
I've gone through relevant questions on SO but I don't see the info I require, or perhaps I've missed it.
You don't upload a repository to a branch.
You have your code in a repository, and the repository has branches. No duplication, but you can get (checkout) master or the branch to set your working files to the version you want: master for untouched changes, branch for your recent changes.
Always good to do when you have reached a milestone is to tag the commit:
$ git tag v1.0
Then from there you can start a branch to work on integrating the new API
$ git checkout -b client_api_update
Work and commit in your branch. It does not affect the content in master, which you can checkout when you want to get your working files as they were in your milestone version (or checkout the tag).
When the integration of the new API is done, it's time to merge your branch to master in order to release support of the new API:
$ git checkout master
$ git merge client_api_update --no-ff
--no-ff is optional, but I think it makes for a more elegant history.
You are at a new milestone, so tag:
$ git tag v1.1
Here is how your history would look like:
/- v1.0 /- v1.1
A---B------------------G - master
\---C---D---E---F-/ - client_api_update
Let's say the branch you created in the main repo is <main_repo_new_branch>. Then the steps would be:
cd <local_repo_dir>
git remote add mainrepo <main_repo_dir>
git push mainrepo <local_repo_branch>:<main_repo_new_branch>
cd <main_repo_dir>
git checkout <main_repo_new_branch>

Categories

Resources