Git Commands

35+ Git Commands List Every Programmer Should Know

Useful Git Commands List

Command Description
git init Initialize a local Git repository
git clone repo_url Clone public repository
git clone ssh://git@github.com/[username]/[repository-name].git Clone private repository
git status Check status
git add [file-name] Add a file to the staging area
git add -A Add all new and changed files to the staging area
git commit -m "[commit message]" Commit changes
git rm -r [file-name.txt] Remove a file (or folder)
git branch List of branches (the asterisk denotes the current branch)
git branch -a List all branches (local and remote)
git branch [branch name] Create a new branch
git branch -d [branch name] Delete a branch
git branch -D [branch name] Delete a branch forcefully
git push origin --delete [branch name] Delete a remote branch
git checkout -b [branch name] Create a new branch and switch to it
git checkout -b [branch name] origin/[branch name] Clone a remote branch and switch to it
git branch -m [old branch name] [new branch name] Rename a local branch
git checkout [branch name] Switch to a branch
git checkout - Switch to the branch last checked out
git checkout -- [file-name.txt] Discard changes to a file
git merge [branch name] Merge a branch into the active branch
git merge [source branch] [target branch] Merge a branch into a target branch
git stash Stash changes in a dirty working directory
git stash clear Remove all stashed entries
git push origin [branch name] Push a branch to your remote repository
git push -u origin [branch name] Push changes to remote repository (and remember the branch)
git push Push changes to remote repository (remembered branch)
git push origin --delete [branch name] Delete a remote branch
git pull Update local repository to the newest commit
git pull origin [branch name] Pull changes from remote repository
git remote add origin ssh://git@github.com/[username]/[repository-name].git Add a remote repository
git remote set-url origin ssh://git@github.com/[username]/[repository-name].git Set a repository’s origin branch to SSH
git log View changes
git log --summary View changes (detailed)
git log --oneline View changes (briefly)
git diff [source branch] [target branch] Preview changes before merging
git revert commitid Revert commit changes
git config --global user.name "your_username" Set globally Username
git config --global user.email "your_email_address@example.com" Set globally Email id
git config --global --list Get global config

—-

How do I change the URI (URL) for a remote Git repository?


git remote -v
# View existing remotes
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)

git remote set-url origin https://github.com/user/repo2.git
# Change the 'origin' remote's URL

git remote -v
# Verify new remote URL
# origin  https://github.com/user/repo2.git (fetch)
# origin  https://github.com/user/repo2.git (push)

How to Perform a Git Force Pull – How to Overwrite Local Changes With Git Force Pull

It is fine when you and the rest of your team are working on different files. But sometimes, multiple people simultaneously work on the same files, and that’s where the problems arise.

Just a Note: git pull = git fetch + git merge

In this scenario, when you have local changes in your system and you pull the latest contribution, you got this error.

error: your local changes to the following files would be overwritten by merge: readme.md please commit your changes or stash them before you merge. Aborting…

Now you have 2 major choices

Choice 1: you want to keep local changes
git stash (stash the local changes clean the workspace) git pull (pull the latest changes from remote ) git stash apply (apply the latest stash)

———–(or)—————

git fetch (fetch the local machine folder) git stash (stash the local changes clean the workspace) git merge ‘@{u}’ (merge the changes from local folder to workspace folder) git stash pop (apply the latest stash )

By default, the stash changes will become staged. If you want to unstage them, use git restore –staged (git ver > 2.25.0).

Choice 2: you do not want the local changes
git reset –hard HEAD (reset to the head means remove all local changes) git pull (get the latest changes)

———–(or)—————

git fetch (fetch the local machine folder) git reset –hard HEAD (reset to the head means remove all local changes) git merge ‘@{u}’ (merge the changes from the local folder to workspace folder)

git pull –force

Now you must be thinking, what is git pull –force then?

it feels like it would help to overwrite local changes. instead, it fetches forcefully but does not merge forcefully (git pull –force = git fetch –force + git merge).

Like git push, git fetch allows us to specify which local and remote branch we want to work on. git fetch origin/ft-1:my-ft means the changes in the ft-1 branch from the remote repository will end up visible on the local branch my-ft. When such kind of operation modifies the existing history, it is not allowed by the Git without an explicit –force parameter.

Leave a Reply

Your email address will not be published. Required fields are marked *