Ultimate Git Command Guide 2024
Ultimate Git Command Guide 2024
February 27, 2024
On this page
Important commands
Command | Description |
---|---|
git config –global user.name | Sets the name that will be attached to your commits and tags globally. |
git config –global user.email | Sets the email address that will be attached to your commits and tags globally. |
git init | Initializes a new Git repository in the current directory. |
git add -A | Adds all changes to the staging area, including new files, modified files, and deleted files. |
git clone https://github.com/gauravrjoshi/YOUR_REPO.git | Clones the repository located at the given URL into a new directory on your local machine. |
git checkout master | Switches to the master branch in your local repository. |
git checkout YOUR_BRANCH_NAME | Switches to the specified branch in your local repository. |
git status | Shows the status of files in the working directory and staging area. |
git add . | Adds all new and changed files in the current directory to the staging area. |
git commit -m “commit message” | Commits the staged changes to the repository with the provided commit message. |
git push origin YOUR_ANOTHER_BRANCH_NAME | Pushes the commits from your local branch to the specified branch on the remote named ‘origin’. |
git pull origin YOUR_BRANCH_NAME | Fetches changes from the specified branch on the remote and merges them into your current branch. |
git pull origin YOUR_ANOTHER_BRANCH_NAME | Fetches changes from another specified branch on the remote and merges them into your current branch. |
git push origin YOUR_BRANCH_NAME | Pushes the commits from your local branch to the same branch on the remote named ‘origin’. |
git diff | Shows the differences between the working directory and the index (or between two commits). |
git remote add first-push-test https://github.com/gauravrjoshi/first-push-test.git | Adds a new remote named ‘first-push-test’ to your local repository. |
git remote add origin https://github.com/gauravrjoshi/first-push-test.git | Adds a new remote named ‘origin’ to your local repository. |
git push origin master | Pushes the commits from your local master branch to the master branch on the remote named ‘origin’. |
git mergetool | Opens a graphical merge conflict resolution tool to resolve merge conflicts. |
git reflog –all | Shows a log of all the references (branches, tags) and their updates in the local repository. |
git config –local user.email “username@email.com” | Sets the email address for your commits in the current repository. |
git config –local user.name “User Name” | Sets the name for your commits in the current repository. |
git config –local credential.helper “” | Removes the credential helper for the current repository, affecting how passwords are stored. |
git config –list | Lists all settings Git can find at that point. |
git fetch –all | Fetches all branches from all remotes. |
git checkout -b feature/YOUR_BRANCH_NAME main | Creates a new branch named ‘feature/YOUR_BRANCH_NAME’ from ‘main’ and checks it out. |
git branch –delete YOUR_BRANCH_NAME | Deletes the specified local branch. Use ‘–delete –force’ if the branch has unmerged changes. |
git pull . master | Pulls changes from the master branch into the current branch within the same repository. |
git merge master | Merges changes from the master branch into the current branch. Useful for local branches without needing to pull. |
git log -n 5 –pretty=format:”%h %ad %s” –date=short | Displays the latest 5 commits in a concise format that includes the commit hash, commit date, and commit message. |
git log -n 5 –oneline | Shows the latest 5 commits with a concise one-line format displaying the commit hash and commit message. |
git reset –hard HEAD~1 | Resets the current branch to the state it was in just before the last commit, discarding any changes. |
git reset –soft HEAD~1 | To undo the last commit while keeping your changes staged for a new commit. |
git push -f origin sandbox | Force-pushes the current branch state to the ‘sandbox’ branch on the remote named ‘origin’, potentially overwriting changes. |
git fetch origin | Fetches updates from the remote named ‘origin’ but does not merge them into the current branch. |
git reset –hard origin/branch-name | Resets the current branch to match the specified branch on the remote named ‘origin’, discarding any local changes. |
git branch -d branch_name | Deletes the specified local branch. Use -D to force deletion if the branch has unmerged changes. |
git clean -n | shows you what would be removed without actually deleting any files |
git clean -f | Command is used to forcefully remove untracked files from your working directory |
git count-objects | We can see the total repository size and how many objects are being used to calculate that size. |
git count-objects -vH | Find the current size of your git repo |
git checkout target_branch git merge source_branch git commit -m “Merge changes from source_branch” git push origin target_branch |
Switches to the target branch, merges changes from the source branch, commits the merge, and pushes the changes to the target branch on the remote named ‘origin’. |
For more information on Git and version control, visit Git’s official documentation. |
Git local configuration
git config --local user.email "username@email.com"
git config --local user.name "User Name"
git config --local credential.helper ""
Checking Your Settings
git config --list
https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control
You can fetch all branches from all remotes like this:
git fetch --all
Create a branch in Git from another branch
git checkout -b feature/YOUR_BRANCH_NAME main
How to delete a Git branch locally
git branch --delete YOUR_BRANCH_NAME
How to “pull” from a local branch into another one?
You have to tell Git from where to pull, in this case from the current directory/repository (.):
git pull . master
But when working locally, you can simply use merge (pull internally calls merge):
git merge master