Git status, add, commit, and log commands
Git provides several commands for managing version control. Here are some of the most commonly used ones:
git status
git status shows the current state of your repository by displaying which files have been modified, added or deleted since the last commit.
git add
git add adds changes to the staging area. This prepares them for committing. You can either add specific files using their names or use git add . to add all changes in the current directory.
git commit
git commit creates a new snapshot of your changes with a message describing what you did. This permanently saves your changes into the repository history.
To make a commit, run git commit -m "Commit message" where “Commit message” is a brief summary of what you changed.
git log
git log displays a list of all commits made to the repository along with their metadata such as author, date and commit message.
You can customize how Git logs commits by specifying options such as --graph, --oneline, or --pretty=format:"%h %s (%an)".
These commands form the foundation of basic Git workflow. By using them effectively, you can manage changes to your codebase over time and collaborate with others on projects more efficiently.
Git Diff and Show Commands for Viewing Changes
Git provides two commands, git diff and git show, for viewing changes made to your codebase. Here’s how to use them:
git diff
git diff shows the differences between the working directory and the staging area or between two commits.
To view the difference between the working directory and the staging area, run git diff.
To view the difference between two commits, run git diff <commit1> <commit2> where <commit1> and <commit2> are commit hashes or branch names.
git show
git show displays information about a specific commit including its metadata and changes made.
To display information about a specific commit, run git show <commit> where <commit> is a commit hash or branch name.
You can also use options such as -p to see a patch of the changes made in that commit or --stat to see statistics about how many lines were added or removed.
These commands are useful for reviewing changes before committing them or when troubleshooting issues in your codebase. By using them effectively, you can gain insights into how your project has evolved over time.
Git checkout and revert commands for undoing changes
Git provides two commands, git checkout and git revert, for undoing changes made to your codebase. Here’s how to use them:
git checkout
git checkout switches branches or restores files from the staging area or a specific commit.
To switch to a different branch, run git checkout <branch> where <branch> is the name of the branch you want to switch to.
To restore a file from the staging area, run git checkout -- <file> where <file> is the name of the file you want to restore.
To restore a file from a specific commit, run git checkout <commit> -- <file> where <commit> is the commit hash or branch name and <file> is the name of the file you want to restore.
git revert
git revert creates a new commit that undoes changes made in a previous commit.
To undo changes made in a specific commit, run git revert <commit> where <commit> is the commit hash or branch name.
This will create a new commit that undoes the changes made in that commit. The original commit will still be in your repository history, but its changes will be reversed.
These commands are useful when you need to undo changes made in your codebase. By using them effectively, you can maintain an accurate history of your project while also correcting mistakes or reverting unwanted changes.
Git reset command for moving to a previous state
Git provides the git reset command for moving your codebase to a previous state. Here’s how to use it:
git reset
git reset moves the current branch pointer and staging area to a specified commit, effectively undoing any commits made after that point.
To move to a specific commit, run git reset <commit> where <commit> is the commit hash or branch name you want to move to.
By default, git reset will preserve changes in your working directory. To discard all changes made after the specified commit, use the --hard option: git reset --hard <commit>.
Be careful when using git reset, as it can permanently delete commits and make it difficult to recover lost work.
These commands are useful when you need to undo multiple commits or revert your codebase to an earlier state. By using them effectively, you can maintain control over your project’s history and ensure that your codebase is always in the desired state.
Git branch and merge commands for branching and merging changes
Git provides the git branch and git merge commands for branching and merging changes in your codebase. Here’s how to use them:
git branch
git branch creates a new branch based on the current commit.
To create a new branch, run git branch <branch> where <branch> is the name of the new branch.
To switch to the new branch, run git checkout <branch>.
git merge
git merge combines changes from one or more branches into the current branch.
To merge changes from another branch into the current branch, first switch to the current branch: git checkout <current-branch>. Then run git merge <other-branch> where <other-branch> is the name of the other branch you want to merge into the current one.
If there are conflicts between changes made in different branches, Git will prompt you to resolve them manually before completing the merge.
Example Workflow
Here’s an example workflow for using these commands:
- Create a new feature branch:
git checkout -b feature-branch - Make changes and commit them:
git add . && git commit -m "Added feature" - Switch back to main/development/master:
git checkout main - Merge changes from feature-branch:
git merge feature-branch
By using these commands effectively, you can maintain multiple versions of your codebase and easily integrate changes made by different team members or contributors.
Git stash command for temporarily saving changes
Git provides the git stash command for temporarily saving changes that are not yet ready to be committed. Here’s how to use it:
git stash save
git stash save saves the changes in your working directory to a temporary location, allowing you to revert back to them later.
To save changes, run git stash save <message> where <message> is an optional message describing the changes you are stashing.
git stash list
git stash list shows a list of all stashed changes.
To view the list of stashed changes, run git stash list.
git stash apply
git stash apply applies the most recent stashed changes to your working directory.
To apply the most recent stashed changes, run git stash apply.
If you have multiple stashes and want to apply a specific one, use git stash apply <stash> where <stash> is the name or index of the desired stash.
Example Workflow
Here’s an example workflow for using these commands:
- Make some changes to your codebase.
- Run
git stash save "Work in progress"to temporarily save your changes. - Switch to another branch or work on something else.
- When ready, switch back to your original branch:
git checkout original-branch. - Run
git stash applyto restore your saved changes.
By using these commands effectively, you can easily switch between different tasks or branches without losing any work in progress.