On this blog I am going to explain how to commit the code to a git or bitbucket repository from android studio. I already created a got repository on bitbucket, Here I will show how to commit and push the code to the repository I created on bitbucket.
I am doing this using the command on Android studio Terminal,
Step 1
First we need to initialize the git on out code,
git init
This command creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new, empty repository.
Step 2
Remove Existing git repository,
git remote rm origin
If you want to remove the old or existing git repository you can run the above comment.
you can try this code if the above code is not working.
rm -rf .git
Here -r means "recursive", so it will delete the entire contents of the folder instead of erroring out because the folder isn't empty, and -f makes it not ask if you're really sure about deleting stuff.
Adding a remote git repository,
git remote add origin YOUR_GIT_REPOSITORY_HTTPS_LINK
To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at. The git remote add command takes two arguments: A remote name, for example, origin.
git remote -v
This code will help us to knew did the previous code worked correctly, Is the fetch and push URL is applied correctly.
Switch Branch / Create New Branch
git checkout BranchName
This comment will switch BranchName branch which is already exist in the repository.
git checkout -b NewBranchName
This code will create a new branch NewBranchName and switch to it at the same time.
git branch –show-current
This code will show you the current branch.
Step 3
Adding the files or changes to the repository,
git add --all
If this code is not working with you you can try the below code also,
git add -A
If you are in any subdirectory of the working directory, git add -A will add all files from the entire working directory, and git add . will add files from your current directory.
git add .
If you are in any subdirectory of the working directory, git add . will add new files and modifications, without deletions (on the current directory and its subdirectories).
git add -u
If you are in any subdirectory of the working directory, git add -u will add stages modifications and deletions, without new files.
Step 4
Moves changes from the working directory to the local repository,
This command takes the staged snapshot and commits it to the project history. My passing the Message you can Identify the changes we made on that commit.
Step 5
Upload local repository content to a remote repository,
git push origin master --force
This command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repository.
--force This option overrides the “fast forward” restriction and matches our local branch to the remote branch.