Recap of Key Concepts and Commands

This recap will provide a concise summary of key Git concepts and commands, serving as a quick reference guide for readers to reinforce their understanding of Git fundamentals.

Initialising a Git Repository

To start using Git in a project, you need to initialise a repository. Navigate to your project directory and run the following command:

git init

This creates a new Git repository in the current directory.

Staging and Committing Changes

Git uses a staging area to track changes before committing them. To stage changes, use the following command:

git add <file(s)>

You can specify individual files or use wildcards to stage multiple files. Once changes are staged, commit them with a descriptive message using:

git commit -m "Commit message"

Checking Repository Status

To see the current status of your repository, use:

git status

This command displays information about untracked, modified, or staged files, as well as the branch you're on.

Viewing Commit History

To view the commit history of your repository, including commit messages, authors, and timestamps, run:

git log

You can use various flags and options to customise the log output, such as --oneline, --graph, or --author.

Working with Branches

Branches allow you to work on different features or versions of your code simultaneously. To create a new branch, use

git branch <branch-name>

Switch to a branch using

git checkout <branch-name>

You can combine these commands into one with git checkout -b <branch-name>. To list branches, including remote branches, use git branch -a.

Merging Branches

To merge changes from one branch into another, first switch to the target branch, then run:

git merge <source-branch>

Git will attempt to automatically merge the changes. In case of conflicts, you'll need to manually resolve them.

Pushing and Pulling Changes

To push your local commits to a remote repository, use:

git push <remote> <branch>

To fetch changes from a remote repository and merge them into your local branch, use:

git pull <remote> <branch>

Replace <remote> with the name of the remote repository, such as "origin," and <branch> with the branch name.

Collaborating with Remote Repositories

To clone a remote repository to your local machine, use:

git clone <repository-url>

To add a remote repository to your local Git configuration, use:

git remote add <remote-name> <repository-url>

You can then push and pull changes to and from the remote repository.

Ignoring Files

To exclude certain files or directories from being tracked by Git, create a file named .gitignore in your repository root. List the file patterns you want to ignore in this file. For example:

logs/
*.log
secret.txt

Git will not track or stage any files that match the patterns specified in .gitignore.

Last updated