Git Cheat Sheet - Anji Keesari
Git Cheat Sheet - Anji Keesari
In this article, I am going to present a comprehensive cheat sheet of commonly used Git
commands with examples.
Installing git
Here are the commands to install Git on different operating systems:
# Ubuntu/Debian:
sudo apt-get install git
Caching credentials:
Typing in login credentials repeatedly can be time consuming. To streamline this process, you can
store your credentials in the cache using the command:
git config -l
Initializing git
Before diving into Git commands, you need to initialize a new Git repository locally in your project's
root directory. Execute the command:
git init
Git clone
To work on an existing Git repository, you can clone it using the command
Git log
To view the commit history of a repository, use the git log command. It provides you with an
overview of past commits and their respective details. also, you can use git log -p to see the
commit history along with the changes made to each file.
Commit details
Use this command to see a specific commit in details
Note: replace commit-id with the id of the commit that you can find in the git log
Git status
This command will show the status of the current repository including staged, unstaged, and
untracked files.
git status
Undoing changes:
If you have already pushed a commit to a remote repository and want to undo it, you need to create
a new commit that undoes the changes. The following command will create a new commit that
undoes the changes introduced by the specified commit:
If you have already committed changes and want to undo the most recent commit, you have a few
options depending on your desired outcome: - Undo the commit and keep the changes as unstaged
modifications:
Viewing differences
To compare the differences between versions, you can use the git diff command. It displays the
changes made to files since the last commit.
git diff
This will show the line-by-line differences between the current state of the files and the last
committed version.
Pushing changes
To push your local commits to a remote repository, you need to use following command.
# if you haven't set the upstream branch yet, you can use this
Pulling changes
Use this command to incorporate the latest changes from a remote repository into your local
repository.
Git fetch
To fetch the latest changes from the remote repository without merging them into your local
branches.
git fetch
Switching branch:
To switch to a different branch in your Git repository, you can utilize the git checkout command
followed by the name of the branch you want to switch to.
List branches
It will show a list of all branches and mark the current branch with an asterisk and highlight it in
green.
# press q to quit
git remote -v
Merging branches
In Git, merging allows you to combine the changes from one branch into another. To merge a
branch into another branch, you can use the git merge command followed by the name of the
branch you want to merge. Here's an example:
# For instance, if you want to merge the changes from the develop branch into the
main branch
# cd to the folder
git checkout main
git merge develop
After performing the merge, it's a good practice to check the status of your repository using git
status to ensure that the merge was successful and there are no conflicts to resolve. also, you can
view the commit history using git log to see the merged commits and their details.
git status
git logs
Delete branch
To delete a branch in Git, you can use either of the following commands:
example
# git branch to see list of branches before delete
git branch
# delete the branch
git branch --delete <branch-name>
# git branch again to see list of branches after delete
git branch
git status
If there are merge conflicts, you will see a message indicating which files have conflicts.
2. Open the conflicted files in a text editor and look for the conflict markers. The markers will look
something like this:
<<<<<<< HEAD
This is the content from the current branch.
=======
This is the content from the branch you are merging.
>>>>>>> <branch-name>
1. Decide what changes you want to keep and remove the conflict markers and any
unnecessary content. The final content should only include the changes you want to keep.
Temporary commits
In Git, you can use temporary commits to store modified, tracked files temporarily, allowing you to
switch branches without losing your changes. This is a useful technique when you want to work on
a different branch but are not ready to commit your changes yet.
Stash your changes: This will create a temporary commit that stores your modifications,
allowing you to switch branches.
git stash
Git stash list Running this command will show you the stash ID, along with a description that
includes the branch name and commit message.
Git stash pop: his command is used to apply the changes from the top of the stash stack and
remove that stash from the stack.
Git stash drop: This command allows you to discard a stash from the stash stack. It
permanently removes a stash and its changes, freeing up space in the stack.