Tutorial: Getting started with Git in CLion | CLion (2024)

This tutorial will guide you through the most popular Git operations and show how they can be performed in CLion. You will learn how to create Git repositories from your projects, share them on GitHub, commit and push changes, create and merge branches, investigate the files' history, and more.

Before you start

In this tutorial, we will create a simple project, share it on GitHub, and perform some Git tasks described below. If you want to use an existing project hosted on GitHub, clone it as described in Check out a project from a remote host (git clone).

Create a new project

  1. Launch CLion and click New Project on the Welcome screen.

  2. In the dialog that opens, select the project type (for example, C++ Executable), provide the location path and set the language standard. Click Create.

    Tutorial: Getting started with Git in CLion | CLion (1)
  3. The new project will immediately open in CLion:

    Tutorial: Getting started with Git in CLion | CLion (2)

Step 1. Enable Git integration

  1. In the main menu, go to VCS | Enable Version Control Integration.

  2. In the dialog that opens, select Git from the list of available version control systems and click OK.

    Tutorial: Getting started with Git in CLion | CLion (3)

Alternatively, press Alt+` and select Create Git Repository (or press 1). In the Finder window that opens, specify a root folder for the local Git repository.

You will get a notification that the local Git repository has been created for your project. On the toolbar and status bar, the Git-related controls will appear:

Tutorial: Getting started with Git in CLion | CLion (4)

The dedicated tool windows for working with Git are now available: Commit (Ctrl+K or View | Tool Windows | Commit) and Git (Alt+9 or View | Tool Windows | Git).

Tutorial: Getting started with Git in CLion | CLion (5)

From the Commit tool window Alt+0, you can review the local changes and commit them to the local Git repository. From the Git tool window, you can work with the Git log, manage pull requests from GitHub, and more.

Step 2. Add files to .gitignore

On the Local Changes tab of the Commit tool window Alt+0, you see the list of files that belong to your project. These files are not added to the Git repository yet — you need to select which of them you want to share and which ones should be ignored by the VCS.

  1. Group the files by directories: press Ctrl+Alt+P or click Tutorial: Getting started with Git in CLion | CLion (6) on the toolbar and select Directory.

    Tutorial: Getting started with Git in CLion | CLion (7)
  2. Select the directories that you don't want to share. For example, the following directories may be ignored without breaking the integrity of the project:

    • .idea: settings of your local CLion installation. Ignore this directory unless you want to share your settings among the team members.

    • cmake-build-debug: the automatically created directory for CMake build artifacts.

  3. Right-click the selection and choose Add to .gitignore | Add to .gitignore.

    Tutorial: Getting started with Git in CLion | CLion (8)
  4. You will be prompted to confirm the creation of a new .gitignore file in the root directory of your project. Click Create.

  5. From the dialog that opens, you can either add the newly created file to Git right away by pressing Add or postpone this by pressing Cancel.

    Click Add. You will see that the .gitignore file has been added to the root directory of your project and placed to Changes area. The directories you selected to ignore are no longer displayed in the Unversioned Files list:

    Tutorial: Getting started with Git in CLion | CLion (9)

    Any time, you can edit the .gitignore file to add new directories to the list or remove existing ones.

Step 3. Commit your project to the local Git repository

Now when all unnecessary directories are excluded from the list of unversioned files, you just need to add all the files to the repository and commit them to save their current state.

  1. In the Commit tool window Alt+0, move all the files from the Unversioned Files list to Changes using drag-and-drop. Select all the files by clicking the root folder checkbox.

  2. Type a message for your first commit:

    Tutorial: Getting started with Git in CLion | CLion (10)
  3. Click Commit. The corresponding notification appears once the commit is performed:

    Tutorial: Getting started with Git in CLion | CLion (11)

Step 4. Share your project on GitHub

To make your project available for other contributors, you need to publish it to a remote repository, for example, on github.com. CLion provides integration with GitHub which allows you to manage projects hosted on GitHub, fork external repositories, manage pull requests, and perform other GitHub operations from the IDE. See more details in the GitHub section.

  1. In the main menu, go to VCS | Share Project on GitHub.

  2. In the dialog that opens, enter your GitHub login and password and click Log In:

    Tutorial: Getting started with Git in CLion | CLion (12)

    If you are not registered on GitHub, click Sign up for GitHub to go to github.com and create a new account there.

  3. If you have two-factor authentication enabled for GitHub, the following dialog will appear:

    Tutorial: Getting started with Git in CLion | CLion (13)

    Enter the code and click OK.

  4. In the dialog that opens, you can change the repository name (by default, it is the same as the project name), the name of a remote (by default, origin), choose the repository type (public or private), and add some description if needed:

    Tutorial: Getting started with Git in CLion | CLion (14)

    Click Share. After the project is successfully published on GitHub, the following notification will appear:

    Tutorial: Getting started with Git in CLion | CLion (15)

    Click the link in the notification to open the repository on GitHub.

Step 5. Create a new branch

You may need to create a separate branch, for example, when you are working on a new feature and you don't want your changes to get into the main (master) branch before they are tested.

  1. Press Ctrl+T to pull the latest version of the current branch.

  2. On the status bar, you can see your current branch — master. Click it to open the Git Branches menu:

    Tutorial: Getting started with Git in CLion | CLion (16)
  3. From the Git Branches menu, select New Branch.

  4. In the dialog that opens, specify the branch name, for example, new_feature, and select the Checkout branch checkbox to switch to the new branch right away.

    Tutorial: Getting started with Git in CLion | CLion (17)

    Now you are switched to the newly created branch:

    Tutorial: Getting started with Git in CLion | CLion (18)
  5. Add the corresponding branch to the remote repository. To do this, select the local branch in the Git Branches menu and click Push:

    Tutorial: Getting started with Git in CLion | CLion (19)

    In the dialog that opens, click Push. The new branch will be added to the remote repository and will appear in the Remote Branches list in the Git Branches menu:

    Tutorial: Getting started with Git in CLion | CLion (20)

Step 6. Make and view the changes

  1. Press Ctrl+T to pull the latest version of the current branch.

  2. Add a new file to the project, for example, subfunc.cpp, and modify the main.cpp file.

    In the Project tool window and in the editor tabs, CLion applies different colors to files: blue to modified, green — to newly added. Moreover, in the gutter area of a modified file, the colored change markers appear next to the modified lines. You can click the gutter marker to view details:

    Tutorial: Getting started with Git in CLion | CLion (21)
  3. Click Tutorial: Getting started with Git in CLion | CLion (22) to view the difference in a separate window:

    Tutorial: Getting started with Git in CLion | CLion (23)
  4. Go to the Commit tool window Alt+0, to preview all the changes at once. Double-click a file to open the diff view in the editor:

    Tutorial: Getting started with Git in CLion | CLion (24)

    See more in Investigate changes in Git repository.

Step 7. Commit and push the changes

  1. Press Ctrl+T to pull the latest version of the current branch.

  2. In the Commit tool window Alt+0, review the changes, make sure that the new file that you're adding to Git is selected, type the commit message, and click Commit.

  3. Press Ctrl+Shift+K or select Git | Push from the main menu to push the changes to the remote repository. The Push Commits dialog opens. Here you can see all the commits to be pushed as well as all the affected files. Before pushing the changes, you can see the differences for each file. To do this, right-click a file and select Show Diff or press Ctrl+D:

    Tutorial: Getting started with Git in CLion | CLion (25)
  4. Click Push.

Step 8. Merge branches and resolve conflicts

There are several ways how you can apply the changes from one branch to another, such as merging and rebasing the branches, cherry-picking commits, applying separate changes or files. All these methods are described in details in Merge, rebase, or cherry-pick to apply changes.

In this tutorial, you will learn how to merge two branches and cherry-pick commits.

Merge branches

  1. Select the master branch in the Git Branches menu on the status bar and click Checkout.

  2. Make some changes in main.cpp that has been modified before in the new_feature branch, for example:

    Tutorial: Getting started with Git in CLion | CLion (26)

    This way, we simulate a conflict situation. Push and commit the change as described above.

  3. From the Git Branches menu, select new_feature and click Merge into Current.

Since we have made changes for the same file in different branches, the Conflicts dialog appears.

Tutorial: Getting started with Git in CLion | CLion (27)

Resolve conflicts

  1. In the Conflicts dialog, you have several options to resolve the conflict:

    • Accept Yours: keep the changes made in the current branch.

    • Accept Theirs: apply the changes from the branch that you want to merge into the current one.

    • Merge: resolve conflicts manually in a dedicated dialog.

    Click Merge. The Merge Revisions dialog opens:

    Tutorial: Getting started with Git in CLion | CLion (28)
  2. In this dialog, you can accept changes by clicking Tutorial: Getting started with Git in CLion | CLion (29)/Tutorial: Getting started with Git in CLion | CLion (30), decline them by clicking Tutorial: Getting started with Git in CLion | CLion (31), and type code in the Result pane. See more in Resolve Git conflicts.

    The syntax errors are highlighted in the Merge Revisions window:

    Tutorial: Getting started with Git in CLion | CLion (32)

    Resolve the conflict as on the screenshot above and click Apply.

  3. Push the changes to the remote repository by pressing Ctrl+Shift+K or selecting Git | Push from the main menu.

You can see the commits in all the branches on the Log tab of the Git tool window Alt+9:

Tutorial: Getting started with Git in CLion | CLion (33)

From here, you can also revert commits, cherry-pick changes from one branch to another, and more. See Log tab for more details.

Step 9. View history

In the master branch, open the main.cpp file. You see that the file's syntax is broken — there are two containers within one view. Imagine these changes were made long ago or by someone else: you might be interested which commit this code came from. To find it out, do one of the following:

  • Right-click the file in the editor or in the Project tool window Alt+1 and select Git | Show History. The History tab of the Git tool window opens:

    Tutorial: Getting started with Git in CLion | CLion (34)

    On this tab you can view all commits that affected the file and find out in which commit the change of your interest was added.

  • In the editor, select a code fragment you want to view history for, right-click the selection, and choose Git | Show History for Selection. The History for Selection window will open:

    Tutorial: Getting started with Git in CLion | CLion (35)

    Here you can review all the commits that affected the code selection of your interest.

Find more ways of exploring the Git history in Investigate changes in Git repository.

Step 10. Cherry-pick commits

This operation allows you to apply changes from selected commits from one branch to another.

  1. In the master branch, make a change that you are going to cherry-pick. Commit Ctrl+K and push Ctrl+Shift+K this change.

  2. Switch to the new_feature branch.

  3. In the Git tool window Alt+9, open the Log tab.

  4. In the Branch list, select master.

  5. Select your last commit and click Tutorial: Getting started with Git in CLion | CLion (36):

    Tutorial: Getting started with Git in CLion | CLion (37)
  6. Resolve the conflict by pressing Accept Theirs form the dialog that opens. This means we override the code in the main.cpp file by the changes from the cherry-picked commit.

  7. Commit the changes from the Commit Changes dialog that opens and push them Ctrl+Shift+K.

    Tutorial: Getting started with Git in CLion | CLion (38)

What's next

If you didn't find how to perform some specific Git task in this tutorial, refer to the Git guidelines — all the Git operations available from the IDE are described there.

For general information on working with version control systems, go to the Version control section. CLion also supports Mercurial, Perforce, and Subversion.

If your project is not under version control, you can still track and manage local changes, roll back to the specific file state, restore deleted files, and more using the Local History feature.

Last modified: 26 May 2024

GitSet up a Git repository

Tutorial: Getting started with Git in CLion | CLion (2024)
Top Articles
Latest Posts
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 6261

Rating: 4 / 5 (71 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.