Snippets

Giuliano Ribeiro Git Stuffs/Tips

Created by Giuliano Ribeiro last modified

If you include some files on Git but after that you have included them on gitignore, use this tip to remove them from Git in batch.

git ls-files --ignored --exclude-standard | xargs git rm --cached
git commit -am "Remove ignored files"

Reference: https://stackoverflow.com/a/23839198

1 - How to Create a Branch in Git/GitHub and PUSH it REMOTE?

git checkout -b [name_of_your_new_branch]
git push origin [name_of_your_new_branch]
git branch

2 - How to CLONE a remote branch in GitHub?

git clone -b [branch_name] [remote_repo_git]

3 - How to DELETE a LOCAL BRANCH?

Delete LOCAL

git branch -d [NAME_OF_LOCAL_BRANCH]
# IF you want to FORCE to delete you can do
git branch -D [NAME_OF_LOCAL_BRANCH]

Delete REMOTE

git push origin --delete NAME_OF_REMOTE_BRNACH

4 - How to Download a GitHub Pull Request?

git clone [GIT_URL_MASTER]
git fetch origin pull/[PULL_REQUEST_ID]/head:[GIT_BRANCHNAME]
git checkout [GIT_BRANCHNAME]

5 - How to Sync a FORK with/from MASTER?

git remote -v 
git remote add upstream [HTTPS_URL_GIT_MASTER]
git remote -v 
git fetch upstream 
git checkout master 
git merge upstream/master 

6 - GitHub How to Sync a Branch inside a FORK with the Original Master for PR?

git checkout [BRANCH_I_WANT_SYNC]
git remote -v 
git remote add upstream [HTTPS_URL_GIT_MASTER] 
git remote -v 
git fetch upstream 
git merge upstream/master 
# RESOLVE ALL CONFICTS - IF THERE ARE ANY
git commit -m "Merge Master" 
git push 

7 - How to download a remote branch from github/git?

git fetch origin $remote-branch:$new-local-branch

8 - How to create a Tag?

git tag
git tag -a v1.4 -m "my version 1.4"
git tag
git push origin v1.4

9. How to undo git add?

git reset 
git status

10. How to Delete a remote Branch in github?

git push origin --delete $remote-branch-name

11. How to create an Empty branch in github?

git checkout --orphan $NEWBRANCH-NAME
git rm -rf .
# Edit your files
git add .
git commit -m "my commit"
git push

12. How to list all REMOTE branchs?

git branch -a

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.