Skip to main content
Image
osl-banner%20%288%29_4.jpg

Git Commands Every Drupaler Should Know

article publisher

pritish.k

Drupal

“A free & open source, distributed version control system designed to handle everything from small to very large projects with speed and efficiency.”

If you are working on a project, you know that you cannot work without Git. Like Demo module in Drupal which keeps a backup and snapshot of your backup updates and configuration settings, git - a version control system - helps you manage your codes and keep track of the changes in the code repository. 

How does it help you? 

  • It keeps a track of the history, which includes an addition or subtraction in the codes, so it is easy to track when and by who the changes were made.
  • It helps you retrieve your codes, in case you accidentally delete or change them, so you don’t have to re-write the codes again and again.
  • It helps you share and exchange your codes with other developers you are working with, which make teamwork an easy task. 
  • And most importantly, it saves and stores your codes on a remote server, so ultimately your data is safe. 

Therefore, in this article, we will discuss the important git commands which will help you get started really smooth.

  1. Git - Configurations
    Git configurations help us setup git configuration variables like name, email etc. This is important since it introduces git to you. A casual hello, you can say.
     
    • Site-wide configuration
      $git config --global user.name “johndoe”
      $git config --global user.email “[email protected]
    • Project-specific configuration
      $git config --local user.name “johndoe”
      $git config --local user.email “[email protected]
      These configurations can be global for every project or local for a particular project. The git config command helps you set configuration information.
       
  2. Git - Creating a Repository
    To work with a git repository, you need to have a repository first.

    There are two ways you can obtain a git repository on a local machine. First, Turn your existing directory containing your code into a git repository. Or Second, Cloning an existing repository from a remote repository (bitbucket / github).
     
    • Making your code directory as a repository
      • Navigate to your project directory and run git init. This initializes the repository with a .git folder. 
        $​​​​​​​cd /path/to/my_project/directory
        $git init
    • Cloning an existing Repository

      The git clone <url> command helps us get a copy of the git repository. Git clone supports the https and  ssh protocols
      $git clone https://github.com/johndoe/my_project.git 
      $git clone [email protected]:johndoe/my_project.git 
  3. Git - Making Changes in your repository
    • Checking the Files status

      The git status command tells the state of the files in the repository.
      This is what we get when we do git status in a newly created repository.
      git status

      Now if we add a new file named demo.txt with some content in it. It returns the following.
      $git status
      git demo txt
    • Tracking Newly added files
      The above examples of git status shows that the new file is untracked. So to track this file we need to add this file using the git add command.
      $git add <file-name>
      $git add demo.txt
      Now if we check the git status 
      $git status
      git add status
    • Committing the modified changes

      Now the changes which we have done should be committed to the local repository before committing to the remote repository. This can be done using the git commit command.
      $git commit - m “<message>”
  4. Git - Review History

    You can also review the complete history of your git repository using the git log command. The git log command lists the history of your current branch. The “--follow” option can be used to list the history for a specific file.
    $git log 
    $git log --follow <file-name> 
  5. Git - Branching

    Git Branching helps us isolate the features and work independently on those features. Some of the important Git branching commands are:

    $git branch <branch-name> //This creates a new branch of the specified branch name.
    $git checkout <branch-name> //This switches the branch to the specified branch name.
    $git checkout -b <branch-name> //This is the shorthand for the above two commands.

    After we have made the specified changes to the new branch this can be merged with the main branch. For this, we need to first check out to the main branch and then merge the branch we have worked on.

    $git checkout master
    Switched to branch 'master'
    ​​​​​​​$git merge <branch-name>

Most of the important commands related to Drupal project development our mentioned under version control tab of the project page on Drupal.org. From version control, we get information such as :

1. Git command to clone and checkout into the selected branch. All the development branch can be accessed through the version control page.

2. Drupal projects hosted on git.drupal.org which you can access via your Drupal account on Drupal.org.

3. Commit message format used is:

git commit -m "Issue #[issue number] by [comma-separated usernames]: [Short summary of the change]."

4. Versioning, which is also handled through git. Projects can have major release version such as 7 or 8, followed by minor release number and patch level. Take for example the following situation - a project with release 8.x-1.2 indicates that a major release is 8 and project is for Drupal 8, the minor release version is 1 and patch level is 2. It is currently in the stable release and fit for the live environment of the website. The maintainer can also add minor releases such alpha, beta and RC before going for a stable release of the project.

5. Patches for contribution also have a specific format i.e [project_name]-[short-description]-[issue-number]-[comment-number]-[drupal-version].patch. 

6. The maintainer can start a project by adding a development branch using following commands.

git checkout -b 8.x-1.x
git push -u origin 8.x-1.x

 Once they have completed all the development, they can add a tag for a stable release.

git checkout  8.x-1.x
git tag 8.x-1.0
git push origin tag 8.x-1.0

As a Drupal developer, git is important to me. It helps bring accountability and ensure flexibility. Getting up and working with git is easy once you have the much-needed commands handy.

Need more? Let us know in the comments below. 

Subscribe

Ready to start your digital transformation journey with us?

Related Blogs

SDC: Integrating Storybook & Single Directory Component

Integrating SDC With Storybook OpenSense Labs

Today, we will talk about about Drupal Single Directory Components or SDC and Storybook. Single Directory Components in Drupal allows you…

RFP: How To Create An RFP For Open Source Solutions?

RFP OpenSense Labs

A good Request for Proposals (RFP) or Call for Proposals (CFP) clearly states the goals and expectations of your project and sets the…

Drupal 7 End Of Life: Top Reasons You Should Migrate To Drupal 10

Drupal 7 End Of Life Cover Image OpenSense Labs

Drupal 10 was released in December 2022 and ever since, the community has been pushing its users to do Drupal 7 to 10 migration. As per…