Skip to main content

Git Basic Howto

Git Basic How-To

URL https://git.webhosting.rug.nl

What is Git?

Git is a distributed version control system that allows developers to track changes in their codebase, collaborate with others, and manage different versions of their projects efficiently.


Setting Up Git

  1. Configure Git
    Set your username and email:

    git config --global user.name "Your Name" git config --global user.email "your.email@example.com"

Common Git Commands

1. Initialize a Repository

git init

Creates a new Git repository in your current directory.

2. Clone a Repository

git clone <repository_url>

Copies an existing repository to your local machine.

3. Check Status

git status

Shows the current status of your working directory and staging area.

4. Stage Changes

git add <file> git add .

Stages changes to be committed. Use . to stage all changes.

5. Commit Changes

git commit -m "Commit message"

Saves changes to the local repository with a message describing the changes.

6. View History

git log

Shows the commit history.


Branching and Merging

1. Create a New Branch

git branch <branch_name>

2. Switch to a Branch

git checkout <branch_name>

Or, create and switch in one step:

git checkout -b <branch_name>

3. Merge a Branch

git merge <branch_name>

Merges the specified branch into the current branch.

4. Delete a Branch

git branch -d <branch_name>

Pushing and Pulling Changes

1. Push to Remote Repository

git push origin <branch_name>

2. Pull from Remote Repository

git pull origin <branch_name>

Stashing Changes

Save Uncommitted Changes

git stash

Apply Stashed Changes

git stash apply

Undoing Changes

1. Discard Unstaged Changes

git checkout -- <file>

2. Unstage Changes

git reset <file>

3. Revert a Commit

git revert <commit_hash>

Helpful Tips

  • Use git diff to see changes in your working directory.
  • Use .gitignore to exclude specific files or directories from version control.
  • Keep commits atomic—focus on one feature or fix per commit.

For more details, check the Git documentation.