Basic Git Commands
Git cheat sheet
This Git cheat sheet saves you time when you just can't remember what a command is or don't want to use git help in the command line. It is hard to memorize all the important Git commands by heart, so print this out or save it to your desktop to resort to when you get stuck. Source: Git cheat sheet.
You can download it using this link.
Basic Git commands
To use Git, developers use specific commands to copy, create, change, and combine code. These commands can be executed directly from the command line or by using an application like GitHub. Here are some common commands for using Git:
git init
initializes a brand new Git repository and begins tracking an existing directory. It adds a hidden subfolder within the existing directory that houses the internal data structure required for version control.git clone
creates a local copy of a project that already exists remotely. The clone includes all the project's files, history, and branches.git commit
saves the snapshot to the project history and completes the change-tracking process. In short, a commit functions like taking a photo. Anything that's been staged withgit add
will become a part of the snapshot withgit commit
.git branch
shows the branches being worked on locally.git merge
merges lines of development together. This command is typically used to combine changes made on two distinct branches.git pull
updates the local line of development with updates from its remote counterpart. Developers use this command if a teammate has made commits to a branch on a remote, and they would like to reflect those changes in their local environment.git push
updates the remote repository with any commits made locally to a branch.
For more information, see the full reference guide to Git commands.
Git Workflow
The Git Workflow involves a series of steps to manage changes to a codebase in local and remote repositories. The Git workflow involves working with three main components:
Local Repository: The local repository is where all the Git operations take place on your local machine. It contains the entire history of the project, including all commits, branches, and files. When you initialize a new repository using
git init
or clone an existing repository usinggit clone
, you create a local repository.Staging Area (Index): The staging area, also known as the index, is an intermediate area where you prepare changes to be committed to the repository. Before making a commit, you stage the changes using the
git add
command, which moves the changes from the working directory to the staging area. This allows you to selectively include specific changes in the next commit while excluding others.Remote Repository: The remote repository is a centralized repository hosted on a server (e.g., GitHub, GitLab, Bitbucket) that serves as a collaboration and backup point for the project. Developers can push their local commits to the remote repository to share their work with others and synchronize changes with team members.
Example: Contribute to an existing repository
- Make Changes: You can make changes to the codebase by modifying existing files, adding new files, or deleting files as needed to implement new features, fix bugs, or make improvements.
- Stage Changes: After making changes, developers use the git add command to stage the modified files for the next commit. Staging allows developers to selectively include specific changes in the next commit while excluding others.
- Review Changes: Before committing the staged changes, developers can review the changes using the git diff command to see the differences between the working directory and the staging area. This helps ensure that only the intended changes are included in the commit.
- Commit Changes: Once satisfied with the staged changes, developers use the git commit command to create a new commit. Commits are snapshots of the codebase at a specific point in time and include a message that describes the purpose of the changes.
- Write Commit Message: When creating a commit, developers provide a descriptive commit message that explains the purpose of the changes. A well-written commit message is concise, clear, and provides context for the changes being made.
- Repeat: Developers continue making changes to the codebase, staging them, and committing them as needed. Each commit represents a discrete set of changes and should be focused on a single task or purpose.
# create a new directory, and initialize it with git-specific functions
git init my-repo
# change into the `my-repo` directory
cd my-repo
# create the first file in the project
touch README.md
# git isn't aware of the file, stage it
git add README.md
# take a snapshot of the staging area
git commit -m "add README to initial commit"
# provide the path for the repository you created on github
git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPOSITORY-NAME.git
# push changes to github
git push --set-upstream origin main