Skip to content

Instantly share code, notes, and snippets.

@bluenex
Last active April 28, 2021 00:16
Show Gist options
  • Save bluenex/479206cf0df374cc637a to your computer and use it in GitHub Desktop.
Save bluenex/479206cf0df374cc637a to your computer and use it in GitHub Desktop.
Git command note

The latest commit

git commit --amend

The command amend changes the latest commit by replacing with the new one. Suppose you have changes in working directory, by running git commit --amend the new changes will be committed as replacement of the old latest commit. If there is no new changes, this command will let you change commit comment of the latest commit and save as new commit replacing old latest commit.

# undo commit
git reset --soft HEAD~1
# undo add
git reset HEAD

The above commands let you clear latest commit and add. The changes will remain as unstaged files in working directory.

# delete on local first
git branch -d <branch>

# push to remote using :
git push origin :<branch>

Merge latest commits

# locally merge with adding new comment to the commit
# where x is the number of HEAD to be merged
git reset --soft HEAD~x && git commit -m "<comment>"

# remotely merge can be confusing
# first way is just force push what local is merged to remote
git reset --soft HEAD~x && git commit -m "<comment>"
git push origin +<branch>

# second way
git rebase -i origin/master~x master
git push origin +<branch>

src: locally, remotely (second way)

git rm --cached <filename>

Add existing sources to repository

git remote add origin <repo>
git push -u origin master # pushes up the repo and its refs for the first time

Changing URL between SSH and HTML can use git remote set-url too.

git remote set-url <remote name> <repo>
# remote name can be origin or upstream in common. 

git remote -v # list existing remote names
# set
git config --global http.proxy http://<username>:<password>@<proxy server>:<proxy port>
git config --global https.proxy https://<username>:<password>@<proxy server>:<proxy port>

#unset
git config --global --unset http.proxy
git config --global --unset https.proxy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment