Skip to content

Instantly share code, notes, and snippets.

@jimathyp
Last active August 24, 2022 09:57
Show Gist options
  • Select an option

  • Save jimathyp/c7b8703e7aad99cd89f1cd6ae67c52c0 to your computer and use it in GitHub Desktop.

Select an option

Save jimathyp/c7b8703e7aad99cd89f1cd6ae67c52c0 to your computer and use it in GitHub Desktop.

Git Branching

Creating

$ git checkout -b iss53
Switched to a new branch "iss53"

This is shorthand for:

$ git branch iss53
$ git checkout iss53

Renaming

# switch to branch to rename
git checkout old_branch

# rename local
git branch -m new_branch_name

# rename remote
git push origin -u new_branch_name

# delete local branch that hasn't been pushed
git branch -d branch_name

# delete old branch on remote
git push origin --delete old_branch_name

Merging

giot checkout master error: Your local changes to the following files would be overwritten by checkout:

If you want to throw away local changes, then force

git checkout -f branch

Get branch from remote

git switch remote_branch_name

Listing branches

list by commit date

# descending order
git branch --sort=-committerdate
  
# ascending order
git branch --sort=committerdate
  
# placed in ~/.bash_aliases
alias git-branch-last='git branch --sort=-committerdate'


# local
git for-each-ref --sort='-committerdate:iso8601' --format='%(committerdate:relative)|%(refname:short)|%(committername)' refs/heads/ | column -s '|' -t | head

# remote
git for-each-ref --sort='-committerdate:iso8601' --format='%(committerdate:relative)|%(refname:short)|%(committername)' refs/remotes/ | column -s '|' -t | head

Resetting a file in a branch

# options
git checkout origin/master fileename

git checkout master -- filename
-- differences are cached, to see differences, git diff --cached

#or (experimental - new)
git restore --source HEAD filename


# specific revision
git checkout some_hash -- fileename_1 filename_2

# commit before a revision use ~1
git checkout some_hash~1 -- fileename_1 filename_2

# commit state
git checkout some_hash

git checkout somebranch file_path

git restore

new command - THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.

after revert no difference?

git diff --cached some_file

'--' before a file list just tells git that next args are filenamees, not anything else

after revert usee git diff --cached

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