DEV Community

Cover image for Git Basics: Key Commands and Tips for Version Control
Yuhao Chen
Yuhao Chen

Posted on

Git Basics: Key Commands and Tips for Version Control

Common Commands

1. Initialization and Configuration

  • git init: Initialize a new Git repository.
  • git config --global user.name "Your Name": Set your Git username.
  • git config --global user.email "your.email@example.com": Set your Git email.

2. Basic Commands

  • git clone <repository_url>: Clone a repository from a remote server.
  • git status: Check the status of your working directory.
  • git add <file>: Stage changes for the next commit.
  • git commit -m "Commit message": Commit staged changes.
  • git push: Push committed changes to a remote repository.
  • git pull: Fetch and merge changes from the remote repository.
  • git fetch: Download changes from the remote repository without merging them. This allows you to review before merging.

3. Branching and Merging

  • git branch: List all branches.
  • git branch <branch_name>: Create a new branch.
  • git checkout <branch_name>: Switch to a different branch.
  • git merge <branch_name>: Merge a branch into the current branch.

4. History and Diff

  • git log: Show commit history.
  • git diff: Show changes between commits, branches, etc.

5. Undoing Changes

  • git checkout -- <file>: Discard changes in a working directory.
  • git reset <commit>: Undo commits while preserving changes in the working directory.
  • git stash: Temporarily save (stash) changes in a working directory without committing, so you can switch branches or pull updates safely. Use git stash pop to apply stashed changes later.

Full Workflow

Starting a project from scratch and implementing a new feature

1. Set Up a New Git Repository

  • Initialize the Git Repository: Create a new project folder and initialize a Git repository.
  mkdir my-new-project
  cd my-new-project
  git init
Enter fullscreen mode Exit fullscreen mode
  • Set Global Git Configuration (if not done previously):
  git config --global user.name "Your Name"
  git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

2. Set Up a Remote Repository

  • Create a remote repository (e.g., on GitHub, GitLab, Bitbucket) and link your local repo to it.

  • Connect to Remote

  git remote add origin https://github.com/your-username/your-repository.git
Enter fullscreen mode Exit fullscreen mode

3. Create an Initial Commit

  • Add basic project setup files like README.md and .gitignore
  touch README.md
  echo "node_modules/" >> .gitignore

  git add .
  git commit -m "Initial project setup with README and .gitignore"
  git push -u origin main
Enter fullscreen mode Exit fullscreen mode

4. Create Initial Branches

  • Create a developbranch (recommended) for ongoing development, keeping main for production-ready code.
  git checkout -b develop
  git push -u origin develop
Enter fullscreen mode Exit fullscreen mode

5. Set Up Project Dependencies

  • Initialize your project with any required dependencies. For example, for Node.js:
  npm init -y
  npm install express  # Example dependency

  git add .
  git commit -m "Initialize project with basic dependencies"
  git push
Enter fullscreen mode Exit fullscreen mode

6. Create a Feature Branch

  • Create a new branch for your feature to keep the develop branch clean.
  git checkout -b feature/new-feature
Enter fullscreen mode Exit fullscreen mode

7. Work on the Feature

  • Implement the feature by writing the necessary code. Make sure to commit frequently with clear, descriptive commit messages.
  git add .
  git commit -m "Implement login functionality"
Enter fullscreen mode Exit fullscreen mode

8. Pull Latest Changes from the Target Branch

  • Before pushing, ensure your feature branch is up-to-date with the latest code from develop (or main):

    • Switch to the Target Branch:
    git checkout develop
    
    • Pull Latest Changes:
    git pull origin develop
    
    • Switch Back to Your Feature Branch:
    git checkout feature/new-feature
    
    • Merge the Latest Changes:
    git merge develop
    
    • Resolve Merge Conflicts (if any): If conflicts arise, Git will flag them, and you’ll need to manually resolve conflicts in the affected files. Once resolved:
    git add <file>
    git commit -m "Resolve merge conflicts"
    

9. Test Your Feature

  • Ensure the feature works by testing locally:
    • Run Unit Tests.
    • Manual Testing: Run the application and verify that the feature works as expected.

10. Push the Feature Branch to the Remote Repository

  • After testing and merging the latest changes from develop, push your feature branch to the remote repository.
  git push origin feature/new-feature
Enter fullscreen mode Exit fullscreen mode

11. Create a Pull Request (PR)

  • On your Git hosting platform (GitHub, GitLab, etc.), create a Pull Request (PR) to merge your feature branch into develop (or main).
    • Provide a Description: Explain what the feature does and highlight any important points for the reviewer.
    • Link to Issues (if applicable): If the feature resolves any issues, link to them in the PR.

12. Code Review and Address Feedback

  • Collaborate with other developers for code review.

  • Make Changes Based on Feedback: If requested changes are necessary, implement them and push new commits to the feature branch:

  git add .
  git commit -m "Address code review feedback"
  git push
Enter fullscreen mode Exit fullscreen mode

13. Merge the Feature Branch

  • Once the PR is approved, merge the feature branch into develop (or main).

  • If using the Git hosting platform’s UI, complete the merge there. Alternatively, merge locally:

  git checkout develop
  git merge feature/new-feature
  git push origin develop
Enter fullscreen mode Exit fullscreen mode

14. Delete the Feature Branch

  • After merging, delete the feature branch both locally and on the remote repository to clean up.
  git branch -d feature/new-feature
  git push origin --delete feature/new-feature
Enter fullscreen mode Exit fullscreen mode

15. Deploy and Verify (Optional)

  • If your project has a CI/CD pipeline, the feature might be automatically deployed. After deployment, verify that the feature works as expected in the staging or production environment.

Top comments (0)