Git-Commands

Git-Commands

Collection of git commands that you need to use quite often.

  • Git-Branching-Basic-Branching-and-Merging: https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
  • Git-Branching-Rebasing: https://git-scm.com/book/en/v2/Git-Branching-Rebasing
  • Git-Tools-Stashing-and-Cleaning: https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning
  • View user details:

    πŸ“ git config user.name

    πŸ“ git config user.email

  • View all details:

    πŸ“ git config --list

  • Set details globally:

    πŸ“ git config --global user.name "<user_name>"

    πŸ“ git config --global user.email "<email>"

    Not recommended to use global settings, better use project level settings. Helpful if you have multiple accounts.

  • Set details at project level:

    πŸ“ git config user.name "<user_name>"

    πŸ“ git config user.email "<email>"

  • Get remote repository to local:

    πŸ“ git clone <repository_url>

  • List all existing remotes:

    πŸ“ git remote -v

  • Add remote:

    πŸ“ git remote add <remote_name> <repository_url>

  • Update/Change remote url:

    πŸ“ git remote set-url <existing_remote_name> <new_url>

  • Remove remote:

    πŸ“ git remote remove <remote_name>

    More Explanation: http://help.github.com/articles/changing-a-remote-s-url

  • Ignore tracked files:

    πŸ“ git update-index --assume-unchanged <file>

  • View branches:

    πŸ“ git branch

  • Switch to a branch:

    πŸ“ git checkout <branch_name>

  • Switch to a branch igorning non-commited changes on current branch

    πŸ“ git checkout -f <branch_name>

  • Get a remote branch on local and switch to it:

    πŸ“ git fetch && git checkout <branch_name>

  • Cut a new branch from an existing branch and switch to the newly created branch:

    πŸ“ git checkout -b <new_branch_name> <existing_branch_name>

  • Save changes in the branch (on local):

    πŸ“ git commit for bigger commit messages

    πŸ“ git commit -m "<commit_message>" for smaller commit messages

  • Push changes from local branch to remote branch:

    πŸ“ git push -u <remote_name> <branch_name> if branch is created locally and it doesn’t exist at remote yet,

    πŸ“ git push <remote_name> <branch_name> otherwise

  • Rename branch:

    πŸ“ git branch -m <new_name> if on the branch that needs to be renamed:

    πŸ“ git branch -m <old_name> <new_name> if on different branch:

    The above is enough if the branch only exists in your local, but if the branch has been pushed to remote, follow these two additional steps:

    πŸ“ git push origin :<old_name> <new_name> This will delete old_name remote branch and push new_name local branch

    πŸ“ <switch to branch and> git push origin -u <new_name>This will reset the upstream branch for the new-name local branch:

  • Merge a branch with another:

    πŸ“ git checkout <destination_branch>

    πŸ“ git merge <source_branch>

    I typically merge using GUI(IntelliJ for Java Projects)

  • Delete local branch:

    πŸ“ git branch -d <branch_name>

    πŸ“ git branch -D <branch_name> if you have uncommited/unmerged changes:

  • Revert to older commits:

    πŸ“ git reset --hard <destination_commit_id>

    If the commits that are being reverted were already pushed to remote do git push --force also, however, be very very careful, this will rewrite your history. You will lose the commit and is generally not a very nice thing to do in a collaborative environment.

  • Pull changes from remote ignoring any local changes:

    πŸ“ git reset ---hard to go to last commit and then,

    πŸ“ git pull <remote_name> <repository_url>

  • Unstage a file:

    πŸ“ git reset file/to/unstage

  • Edit a commit message which is not yet pushed

    πŸ“ git commit --amend for bigger messages

    πŸ“ git commit --amend -m <new_commit_message>

    If commits were already pushed to remote, update the remote too with below command

    πŸ“ git push <remote_name> <branch_name> -f or git push <remote_name> <branch_name> --force

  • Revert specific files to specific commit:

    πŸ“ git checkout <commit_id> -- <file1/to/restore> <file2/to/restore>

  • Recover a branch after its deleted:

    πŸ“ git checkout -b <branch_name> <sha>

    To find sha do git reflog or in case you have just deleted your branch, then scroll up in terminal to find something like Deleted branch <your_branch> (was <sha>)

Latest Posts