Skip to content

Understanding Git

Basic Commands

mmd

1. Git Clone

What is it?

The git clone command is used to create a local copy of a remote repository.

Syntax

git clone [repository URL]

Example

git clone https://github.com/user/repo.git

Result

  • Creates a folder with the repository's name and downloads all files and the commit history.

2. Git Checkout

What is it?

The git checkout command is used to switch branches or restore files.

Syntax

git checkout [branch-name]

Creating a new branch

To create and switch to a new branch at the same time, use:

git checkout -b [new-branch-name]

Example

git checkout -b new-feature

3. Git Pull

What is it?

The git pull command is used to update your local repository with changes from the remote repository.

Syntax

git pull

Example

git pull origin main

Result

  • Downloads and merges changes from the remote repository into the current branch.

4. Git Add

What is it?

The git add command is used to add changes to the index (staging area) before a commit.

Syntax

  • To add all files:
    git add .
    
  • To add specific files:
    git add [file-name]
    

Example

git add file1.txt

5. Git Commit

What is it?

The git commit command is used to save changes to the local repository.

Syntax

git commit

Variations

  • To add all changes and commit:
    git commit -a
    
  • To commit with a message:
    git commit -m "Commit message"
    

Example

git commit -m "Add new feature"

6. Using Vim for Commits

What is it?

If you just use git commit, the Vim editor will open for you to edit the commit message.

How to use Vim

  1. Enter edit mode:
  2. Press i to enter insert mode.
  3. Save and exit:
  4. Press Esc, type :x, and press Enter.

Tip

  • Using -m avoids opening Vim and allows you to type the message directly.

7. Git Merge

What is it?

The git merge command is used to combine changes from different branches.

Syntax

git merge [branch-name]

Example

  1. Switch to the branch where you want to merge the changes:
    git checkout main
    
  2. Run the merge:
    git merge new-feature
    

Result

  • The changes from the specified branch will be merged into the current branch.

8. Git Reset

What is it?

The git reset command is used to undo changes in the staging area and, optionally, in commits.

Variations

  • git reset: Removes changes from the staging area but keeps the changes in your local copy.

Usage:

git reset
  • git reset --soft [commit]: Moves HEAD to the specified commit and keeps the changes in the staging area, allowing you to make a new commit.

Usage:

git reset --soft HEAD~1
  • git reset --hard [commit]: Moves HEAD and discards all changes in the staging area and the working directory, permanently removing uncommitted changes.

Usage:

git reset --hard HEAD~1

9. What is HEAD in Git?

HEAD is a special pointer in Git that points to the most recent commit in the current branch you are working on. In other words, it represents the latest state of your local repository in the active branch.

Functions of HEAD

  1. Active Branch Identification: HEAD indicates which branch you are currently using. When you check out a new branch, HEAD is updated to point to the last commit of that new branch.

  2. Last Commit Record: HEAD points to the last commit in the active branch, allowing you to perform operations like git commit, git merge, and git reset based on the current state.

  3. Reference for Navigation: HEAD can also be used to navigate between commits. For example, you can reference the previous commit using HEAD~1, or the most recent commit on a different branch.

Importance of HEAD

HEAD is fundamental to Git's operation, as it helps maintain control over the workflow and changes in the repository. Understanding how HEAD operates is essential for effectively manipulating branches and commits.

10. Commands Summary

| Command | Description | |