How to: Merge, Push, Pull, Create a New Branch, and More in Android Studio
In this article we’ll demonstrate how to perform these tasks using both Android Studio’s graphical user interface (GUI) and the terminal.
For a step-by-step walkthrough, check out my YouTube video accompanying this article. Learn visually how to merge, push, pull, and create branches with ease! 👇
1. Cloning a Repository
Using Android Studio
- Open Android Studio and go to File > New > Project from Version Control.
- Enter the repository URL and choose a directory to clone the project.
- Click Clone.
Using Terminal
✍🏻 Use the cd command to navigate to the directory where you want to clone the repository.
$ git clone <repository-url>
2. Creating a New Branch
Using Android Studio
- Open the Git tool window (View > Tool Windows > Git).
- Click on the Branch dropdown at the bottom-right corner.
- Select New Branch.
- Enter a branch name and click OK.
Using Terminal
$ git branch <new-branch-name>
$ git checkout <new-branch-name>
or
$ git checkout -b <new-branch-name>
3. Pulling Changes from the Remote Repository
Using Android Studio
- Open the Git menu (VCS > Git).
- Select Pull.
- In the dialog box, confirm the remote and branch, then click Pull.
💞I write these articles to help fellow developers. If you found this useful and want to support my work, you can do so here: Buy Me a Coffee ☕
Using Terminal
$ git pull origin <branch-name>
✍🏻 Take Notes: Pull vs Update Project ✍🏻
4. Making Changes and Committing
Using Android Studio
- Make your changes in the code.
- Open the Commit dialog (Ctrl+K / Command+K).
- Select the files to commit, write a commit message, and click Commit
Using Terminal
$ git add .
$ git commit -m "Your commit message"
5. Pushing Changes to the Remote Repository
Using Android Studio
- Open the Git menu (VCS > Git).
- Select Push.
- In the dialog box, confirm the branch and click Push.
Using Terminal
$ git push origin <branch-name>
6. Merging Branches
Using Android Studio
- Open the Git tool window.
- Click the Branch dropdown and select Merge Branches.
- Choose the branch you want to merge into the current branch and click Merge.
Using Terminal
$ git checkout <target-branch>
$ git merge <source-branch>
7. Handling Merge Conflicts
Using Android Studio
- If a conflict occurs, Android Studio will highlight the conflicting files.
- Open the file and use the conflict resolution tool to select changes.
- Once resolved, commit the changes.
Using Terminal
- Open the conflicting files and manually resolve conflicts.
- After resolving, stage the files:
$ git add <file>
3. Commit the resolved changes:
$ git commit
8. Checking the Git History
Using Android Studio
- Open the Git tool window.
- Go to the Log tab to view commit history.
Using Terminal
$ git log
9. Deleting a Branch
Using Android Studio
- Open the Git tool window.
- Click the Branch dropdown and select Delete Branch.
- Choose the branch to delete and confirm.
Using Terminal
For a local branch:
$ git branch -d <branch-name>
For a remote branch:
$ git push origin --delete <branch-name>
Tips for Using Git Effectively
- Always pull the latest changes before starting work.
- Write meaningful commit messages.
- Use branches to separate features and bug fixes.
- Regularly push changes to avoid losing work.