User Tools

Site Tools


linux:git

This is an old revision of the document!


GIT

Check out remote branch

git switch --track origin/branch-name // 1. Creates a new local branch called branch-name
                                      // 2. Set its upstream to origin/branch-name (the remote branch)
                                      // 3. Move your working directory to that branch

DELETE REMOTE BRANCH

Your local Git keeps a list of remote branches under remote/origin/*, you can see it with:

git branch -a
git push origin --delete <branch-name>

Accidentally committed on master

If changes were accidentally committed on master and we want to move them to a new branch:

git switch -c new-branch         // Creates a new branch and switches to it.
git checkout master              // Go back to master
 
# Cleaning master - Option 1
 
git reset --hard origin/master   # This will delete the commit from master,
                                 # but it still exists on new branch
                                 # Resets to match the remote branch exactly.
                                 # Used to discard all local commits since last fetch/pull
 
# Cleaning master - Option 2
 
git reset --hard HEAD~1          # Moves one commit back in local history.
                                 # Used to undo the last local commit (or a few with HEAD~2)
 
# Bonus - local history in compact form
 
git log --oneline master
linux/git.1765447718.txt.gz · Last modified: by v1ctor