Skip to content

Instantly share code, notes, and snippets.

@viswanath11
Forked from ThomasTJdev/GIT commands.md
Created October 24, 2019 02:18
Show Gist options
  • Save viswanath11/a85a0cfc37cdaf0464dad3686ce1d54c to your computer and use it in GitHub Desktop.
Save viswanath11/a85a0cfc37cdaf0464dad3686ce1d54c to your computer and use it in GitHub Desktop.
GIT commands

Fix last commit message

Made a typo or wrong description in a commit message? Run and edit:

git commit --amend

Add a file to latest commit

git commit --amend --reuse-message HEAD
#or
git commit --amend --no-edit

Remove added file

git reset <filename>

If you already have committing the file:

git reset --soft HEAD~1
get reset <filename>
rm <filename>
git commit

Branches

See all

git branch

New branch

git checkout -b newbranch

If you want to rename a branch while pointed to any branch, do:

git branch -m <oldname> <newname>

#If you want to rename the current branch, you can do:
git branch -m <newname>

Delete a branch

git branch -d <branchname>

# To force delete use -D

Remote branch

# List all remote
git branch -r

# Checkout
git checkout -b LocalName origin/remotebranchname

Merge

git checkout master
git merge <featurebranch>

Merge master into branch

git checkout <featurebranch>
git rebase master

Override (delete) local changes

git fetch --all
git reset --hard origin/master
#git reset --hard origin/<branch_name>

Update fork from original repo

git remote -v
#origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
#origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
git remote -v
#origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
#origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
#upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
#upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)

git fetch upstream
git checkout master
git merge upstream/master

Download pull request

git fetch origin pull/ID/head:BRANCHNAME
git checkout BRANCHNAME
git push origin BRANCHNAME
#or merge

Update local PR when author add more commits

git branch --set-upstream-to=origin/BRANCHNAME BRANCHNAME
git pull
#resolve

Rebase

Rebase a remote branch locally (named devel)

git checkout devel
git fetch upstream
git merge upstream/devel
# or??
git rebase devel upstream/devel

Your GIT is messsssed

git reflog

Review the output. Find the commit/item and note the identification number, e.g. 3a5f876. Now reset the git HEAD:

git reset HEAD@<identification number>

Shorten GIT url

curl -i https://git.io -F "url=https://github.com/..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment