siemer-solutions.de

Clean and simple it-solutions in Eberswalde and Berlin/Brandenburg




The Most Common Git Commands

Git is an indispensable version control system for developers, enabling efficient tracking of changes, collaboration, and project management. While Git offers a vast array of commands, a handful are used almost daily. Mastering these fundamental commands is crucial for any developer.

Here's a rundown of the most common Git commands:

1. git init

This command initializes a new Git repository in the current directory. It creates a hidden .git subdirectory that contains all the necessary Git files for the repository.

git init

2. git clone [repository_url]

Used to create a local copy of an existing Git repository from a remote server (e.g., GitHub, GitLab, Bitbucket). This command downloads all the project files and the entire Git history.

git clone [https://github.com/user/repository.git](https://github.com/user/repository.git)

3. git add [file_name] or git add .

This command stages changes, preparing them to be committed. You can specify individual files or use . to stage all modified and new files in the current directory. Staging is a crucial intermediate step before committing.

git add index.html git add .

4. git commit -m "[commit_message]"

Committing saves the staged changes to the repository's history. Each commit represents a snapshot of your project at a specific point in time. The -m flag allows you to include a descriptive message for your commit, which is highly recommended for good practice.

git commit -m "Add initial homepage structure"

5. git status

Provides a summary of the current state of your working directory and the staging area. It tells you which files are modified, staged, or untracked. This command is invaluable for understanding what Git sees.

git status

6. git log

Displays a chronological history of commits in the current branch. Each entry includes the commit hash, author, date, and commit message. This helps in tracing changes and understanding project evolution.

git log

7. git branch

Used to list, create, or delete branches. Branches are fundamental to Git's workflow, allowing developers to work on features or fixes independently without affecting the main codebase.

git branch # List all branches git branch new-feature # Create a new branch called 'new-feature' git branch -d old-branch # Delete a branch

8. git checkout [branch_name] or git switch [branch_name]

This command is used to switch between branches or restore files. While git checkout is historically used, git switch is a newer, more focused command specifically for switching branches, introduced for clarity.

git checkout develop # Switch to the 'develop' branch git switch main # Switch to the 'main' branch git checkout -- . # Discard all unstaged changes

9. git pull

Fetches changes from a remote repository and integrates them into your current local branch. This is essential for keeping your local repository synchronized with the remote, especially when collaborating.

git pull origin main

10. git push

Uploads your local commits to a remote repository. This makes your changes available to others and updates the remote branch.

git push origin main

11. git merge [branch_name]

Integrates changes from one branch into another. This is commonly used to combine feature branches into the main development branch after a feature is complete.

git merge feature/login

12. git diff

Shows the differences between various states of your repository, such as changes in your working directory that are not yet staged, or differences between commits.

git diff # Show unstaged changes git diff --staged # Show staged changes git diff HEAD~1 HEAD # Show changes between the last two commits