
Posted on: 9 months ago
Git Commands
Branch, Commands, Git, Git Commands, Repository
49 | 0 | 0
Here’s a list of common Git commands and their descriptions, which we can use to manage our repositories.
- git config: Configure Git settings on our system
We use "git config" command to configure Git settings on our system, such as setting up our username, email, or editor preferences. It is the first step in personalizing our Git environment.// To set repository-specific username/email configuration in git config $ git config user.name "Your Name" $ git config user.email "youremail@example.com" // To set global username/email configuration in git config $ git config --global user.name "Your Name" $ git config --global user.email "youremail@example.com"
- git init: Initialize a new Git repository
We use "git init" command to initialize a new Git repository in our project folder. It sets up all the necessary Git configuration files, allowing us to start version controlling of our project.$ git init
- git clone: Clone a remote repository
we use "git clone" command to clone a remote repository to our local system. This command creates a local copy of the project and enabling us to work on it offline.// Cloning project repository into an existing empty directory $ git clone <repository-url> <existing-directory-path> // Cloning project repository into the current working directory $ git clone <repository-url> .
- git status: Check the status of our working project directory
We use "git status" command to check the status of our working project directory and staging area (modified, untracked, staged files). It shows which files have changes that
need to be committed, helping us keep track of our modifications.// Full status $ git status // File status in a more compact way $ git status --short
- git add: Add changes to the staging area
We use "git add" command to add changes to the staging area, preparing them for a commit. In this way we can selet which changes we want to include in the next snapshot of our project.// Add a specific file $ git add <filename with extension or directory> // Add all changes in the current directory $ git add . or $ git add --all or $ git add -A
- git commit: Record changes to the repository
We use "git commit" command to commit the staged changes to the local repository with a message. Each commit acts as a save point for our project and making it easy to revert if needed.$ git commit -m "commit message"
- git diff:
We use "git diff" command to check the differences between changes in the working project directory and the last commit.// A specific file $ git diff <filename with extension> // all files $ git diff
- git log: See the commit history of our project repository
We use "git log" to see the commit history of our repository. It shows a list of all commits and make it easy to review past changes.$ git log or $ git log --oneline
- git branch: List, create, or delete branches in project repository
We use "git branch" command to list, create or delete branches in our project repository.// Create a new branch $ git branch <name_of_branch> // List branches $ git branch // Delete a branch $ git branch -d <branch_name> // Switching to other branches $ git checkout <branch_name> // Create a new branch and directly switching to it $ git checkout -b <branch_name>
- git checkout: Switch branches or restore files
We use "git checkout" command to switch branches or restore files.// Switch to another branch $ git checkout <branch_name> // Restore a specific file to the state of the last commit (discard local changes). $ git checkout -- <file> // Restore all files $ git checkout .
- git merge: Merge changes from one branch into another
We use "git merge" command to merge changes from one branch into another. It combines the histories of different branches, bringing new features or fixes into the main branch.// Merge another branch into the current branch $ git merge <branch_name>
- git rebase: Reapply commits on top of another base branch.
$ git rebase <branch_name>
- git remote: Manage connections to remote repositories
We use "git remote" command to manage connections to remote repositories. It lets us link our local project repository to a remote one, enabling collaboration and code sharing.// Show the URL of remote repositories $ git remote -v // Add a remote repository $ git remote add origin <remote_url>
- git push: Upload local repository changes to a remote repository
We use "git push" command to upload local project repository changes to a remote repository. It allows us to share our updates with others by sending our commits to the remote server.// Push changes to the remote $ git push <remote_name> <branch_name> // Push changes to the master branch (default) $ git push origin master
- git pull: Fetch and integrate
We use "git pull" command to fetch and merge changes from a remote repository to the local one.// Fetch and merge changes $ git pull <remote_name> <branch_name> // Pull changes to the master branch $ git pull origin master
- git fetch:
We use "git fetch" command to fetch changes from a remote repository but don’t merge them.$ git fetch <remote_name> or $ git fetch
- git push --force:
We use "git fetch" command to push changes forcefully to the remote repository (use with caution).$ git push --force
- git reset:
We use "git reset" command to undo changes or reset the staging area.// Unstage a file (but keep changes) $ git reset <file> // Discard all changes and reset to the last commit $ git reset --hard // reset our repository back to that specific commit(commithash being the first 7 characters of the commit hash we found in the log) $ git reset <commithash>
- git revert: Undo a specific commit by creating
We use "git revert" command to undo a specific commit by creating a new commit that reverses the changes. It’s a safe way to undo changes without rewriting history.$ git revert <commit_hash> // revert the latest commit (revert the latest change, and then commit) $ git revert HEAD // By adding the option --no-edit, we can skip the commit message editor (getting the default revert message) $ git revert HEAD --no-edit
- git stash: Stashing Changes
We use "git stash" command to temporarily store uncommitted changes to work on something else.// Save changes to a stash $ git stash // Apply the last stash and remove it from the stash list $ git stash pop // List all stashes $ git stash list
- git tag: Mark specific points in your repository’s history
We use "git tag" command to mark specific points in our project repository’s history, such as version releases. Tags are useful for labeling important milestones.// Create a tag $ git tag <tag_name> // List tags $ git tag // Push a tag to the remote repository $ git push origin <tag_name>
- git cherry-pick:
We use "git cherry-pick" command to apply the changes from a specific commit from another branch.$ git cherry-pick <commit_hash>
- git rebase -i:
We use "git rebase -i" command to interactively rebase commits to edit, delete, or reorder commits.$ git rebase -i <commit_hash>
- git clean:
We use "git clean" command to remove untracked files from the working directory.// Remove untracked files $ git clean -f // Remove untracked files and directories $ git clean -fd
- git show:
We use "git show" command to show various types of objects, such as commits, trees, or tags.// Show details of a specific commit $ git show <commit_hash>
- git ls-files:
We use "git ls-files" command to see list of all files that are tracked by Git.$ git ls-files
Welcome to CodePits.
CodePits provide a collection of tutorials about many programming languages like PHP, Laravel Framework, Codeigniter Framework, Mysql Database, Bootstrap Front-end Framework, Jquery, Node JS, Ajax Example, APIs, CURL Example, Composer Packages Example, AngularJS, Ionic Framework, etc. 🙌
Other Posts
Categories
Tags
Comment
Comments (0)
🤔
No comments yet, be the first to help
load more