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:
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:
You can specify individual files or use wildcards to stage multiple files. Once changes are staged, commit them with a descriptive message using:
Checking Repository Status
To see the current status of your repository, use:
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:
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
Switch to a branch using
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 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:
To fetch changes from a remote repository and merge them into your local branch, use:
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:
To add a remote repository to your local Git configuration, use:
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:
Git will not track or stage any files that match the patterns specified in .gitignore
.
Last updated