Skip to content

Instantly share code, notes, and snippets.

@anguschiu1
Last active July 30, 2019 09:20
Show Gist options
  • Save anguschiu1/60a6bcc979ab446c8e29f0f437bd6f98 to your computer and use it in GitHub Desktop.
Save anguschiu1/60a6bcc979ab446c8e29f0f437bd6f98 to your computer and use it in GitHub Desktop.
[Git tips] daily operation tips #git

How to get just several files/folders from another branch

git checkout master	# first get back to master
git checkout experiment doc/* app.js package.json	# then copy the version of files from branch "experiment"
git add . 
git commit -m "added something"

How to replace master branch in git, entirely, from another branch? [duplicate]

git checkout seotweaks
git merge -s ours master
git checkout master
git merge seotweaks

Merge development branch with master

I generally like to merge master into the development first so that if there are any conflicts, I can resolve in the development branch itself and my master remains clean.

(on branch development)$ git merge master
(resolve any merge conflicts if there are any)
git checkout master
git merge development (there won't be any conflicts now)

How to delete a pushed remote commit

git log --pretty=oneline --abbrev-commit

46cd867 Changed with mistake
d9f1cf5 Changed again
105fd3d Changed content
df33c8a First commit

git rebase -i HEAD~2

Editor is shown as below:

pick d9f1cf5 Changed again
pick 46cd867 Changed with mistake

# Rebase 105fd3d..46cd867 onto 105fd3d
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

One thing to notice here is that the most recent commit is the one at the bottom. We just need to delete the line that corresponds to the commit we want to delete and save the file.

We can see that the change was applied correctly:

git log --pretty=oneline --abbrev-commit

d9f1cf5 Changed again
105fd3d Changed content
df33c8a First commit

To remove a commit you already pushed to your origin or to another remote repository you have to first delete it locally like in the previous step and then push your changes to the remote. git push origin +master Notice the + sign before the name of the branch you are pushing, this tells git to force the push.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment