Updated on 2023-05-06 GMT+08:00

Local Development on Git

Background

After creating a repository with a README file in CodeArts Repo, an architect or project manager pushes the architecture code to the repository. Other developers then clone the architecture code to their local computers for incremental development.

  • Git supports code transmission over SSH and HTTPS. The SSH protocol is used as an example.
  • If you want to use the HTTPS protocol, download the HTTPS password, and enter the HTTPS username and password when cloning or pushing code.
  • The SSH URL and HTTPS URL of the same repository are different.

Pushing Architecture Code

  1. Open the architecture code on the local computer. Ensure that the name of the root directory (CodeArts) is the same as that of the code repository created in the cloud. Right-click in the root directory and choose Git Bash Here.
  2. Push local code to the cloud.

    Run commands on Git Bash as instructed below.

    1. Initialize a local code repository. After this command is executed, a .git directory is generated in D:/code/repo1/.
      1
      $ git init
      
    2. Associate the local repository with the one in the cloud.
      1
      $ git remote add origin repoUrl
      

      You can switch to the repository details page, click Clone/Download, and click the highlighted tab in the following figure to obtain the repoUrl value.

    3. Push code to the cloud repository.
      1
      2
      3
      4
      5
      $ git  add . 
      $ git  commit  -m "init project"
      $ git  branch --set-upstream-to=origin/master master
      $ git  pull  --rebase
      $ git  push
      

Cloning Code

Clone the architecture code from the cloud to the local computer.

  1. In the directory where you want to clone the code, right click and choose Git Bash Here.

  1. Run the following command to clone the repository. Click Clone/Download and click the highlighted tab in the following figure to obtain the repoUrl value
    1
    $ git clone repoUrl//Clone the code from the remote repository to the local computer.
    

Committing Code

A change travels from the working directory, stage, and local repository before being pushed to the remote repository.

Executing corresponding git commands can move a file between the four areas.

The following commands are involved:

  1. #git add/rm filename //Add changes from the working directory to the stage after creating, editing, or deleting files.
  2. #git commit –m "commit message" //Commit the files from the stage to the local repository.
  3. #git push //Push the files from the local repository to the remote one.

Performing Branch Operations

  • Create a branch.

    In Git, creating a branch is not to copy a repository, but to create a HEAD, a movable pointer pointing to the last commit. A branch in nature is a file that contains the 40-byte SHA-1 checksum of the commit it points to.

    1
    #git branch branchName commitID
    

    A new branch is pulled based on the specified commit ID. If no commit ID is specified, the branch is pulled from the commit that HEAD points to.

    For example, to create a feature branch, run git branch feature.

  • Check out a branch.

    Run the following command:

    1
    #git checkout branchName
    

    For example, to check out the feature branch, run git checkout feature.

  • Integrate branches.

    There are two ways to integrate changes from one branch to another: git merge and git rebase. The following describes the differences between them.

    Assume that C4 and C3 are added to the master branch and hotfix branch respectively. The hotfix branch is now ready to be integrated to the master branch.

    1. Three-way merge integrates C3, C4, and their most recent common ancestor C2. Merging is simple to operate, but a new commit C5 is created, resulting in a less readable commit history.
      1
      2
      #git checkout master
      #git merge hotfix
      

    2. Git rebase saves the changes introduced to C4 as a patch in the .git/rebase directory, synchronizes the patch C4' to the hotfix branch, and applies the patch on top of C3.
      1
      2
      #git checkout master
      #git rebase hotfix
      

  • Resolve conflicts.
    1. Scenario 1: The same line of code is changed in both the two branches to merge.

      Solution

      1. Manually merge the change that you think is proper.
      2. Commit the change.
    2. Scenario 2: A file is renamed in two different ways.

      Solution

      1. Check which name is correct and delete the incorrect one.
      2. Commit the change.