Pull, Push and Fetch Files with Git

Use this workflow to work with a remote repository. With Git™, there is a two-step workflow: commit local changes, and then push to the remote repository. In MATLAB®, the only access to the remote repository is through the Pull, Push and Fetch menu options. All other actions, such as Compare to Ancestor and Commit, use the local repository. This diagram represents the Git workflow.

Pull and Push

To get the latest changes, right-click the Current Folder browser and select Source Control > Pull. Pull fetches the latest changes and merges them into your current branch. If you are not sure what is going to come in from the repository, use fetch to examine the changes first and then merge the changes manually.

Note

Before you can merge, you must install command-line Git and register binary files to prevent Git from inserting conflict markers. See Install Command-Line Git Client.

Pull might fail if you have conflicts. With a complicated change you might want to create a branch from the origin, make some compatibility changes, then merge that branch into the main tracking branch.

To commit changes to the local repository, right-click the Current Folder browser and select Source Control > View and Commit Changes.

To see if your local changes have moved ahead of the remote tracking branch, right-click the file or white space of the Current Folder browser and select Source Control > View Details. The Git information field indicates whether your committed local changes are ahead of, behind, or coincident with the remote tracking branch.

To send local commits to the remote repository, right-click in the Current Folder browser and select Source Control > Push. A message appears if you cannot push your changes directly because the repository has moved on. Right-click in the Current Folder browser and select Source Control > Fetch to fetch all changes from the remote repository. Merge branches and resolve conflicts, and then you can push your changes.

Using Git, you cannot add empty folders to source control, so you cannot select Push and then clone an empty folder. You can create an empty folder in MATLAB, but if you push changes and then sync a new sandbox, then the empty folder does not appear in the new sandbox. To push empty folders to the repository for other users to sync, create a gitignore file in the folder and then push your changes.

Fetch and Merge

Use Fetch to get changes and merge manually. Use Pull instead to fetch the latest changes and merge them into your current branch.

Note

After fetching, you must merge. Before you can merge branches, you must install command-line Git and register binary files to prevent Git from inserting conflict markers. See Install Command-Line Git Client.

To fetch changes from the remote repository, right-click in the Current Folder browser and select Source Control > Fetch. Fetch updates all of the origin branches in the local repository. Your sandbox files do not change. To see others’ changes, you need to merge in the origin changes to your local branches.

For information about your current branch relative to the remote tracking branch in the repository, right-click the file or white space of the Current Folder browser and select Source Control > View Details. The Git information field indicates whether your committed local changes are ahead of, behind, or coincident with the remote tracking branch. When you see the message Behind, you need to merge in changes from the repository to your local branch.

For example, if you are on the master branch, get all changes from the master branch in the remote repository.

  1. Right-click in the Current Folder browser and select Source Control > Fetch

  2. Right-click in the Current Folder browser and select Source Control > Branches.

  3. In the Branches dialog box, select origin/master in the Branches list.

  4. Click Merge. The origin branch changes merge into the master branch in your sandbox.

If you right-click the Current Folder browser and select Source Control > View Details, the Git information field indicates Coincident with /origin/master. You can now view the changes that you fetched and merged from the remote repository in your local sandbox.

Use Git Stashes

Store uncommitted changes for later use by creating a Git stash. Use stashes to:

  • Store modified files without committing them.

  • Move changes easily to a new branch.

  • Browse and examine the changes within a stash.

To create and manage stashes, in the Current Folder browser, right-click the white space in a folder managed by Git and select Source Control > Stashes.

In the Stashes dialog box:

  • To create a stash containing your currently modified files, click New Stash.

  • To view modified files in a stash, select the stash under Available Stashes. Right-click modified files to view changes or save a copy.

  • To apply the stash to your current branch and then delete the stash, click Pop.

  • To apply the stash and keep it, click Apply.

  • To delete the stash, click Drop.

Related Topics