Skip to content

Instantly share code, notes, and snippets.

@jimathyp
Last active July 13, 2021 23:01
Show Gist options
  • Select an option

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

Select an option

Save jimathyp/cccbd57d3fde534cb634a899be4c3b39 to your computer and use it in GitHub Desktop.
Git usage

Git usage

http://gitready.com/

see also

  • git-usage-stash.md

Branch renaming

Local branch, when on any branch

git branch -m <old name> <new name>

When on the branch to be renamed

git branch -m <new name>

(Remmeber m for mv mov, the long command is --move)

Will need to update the linkage between local and remote (ie the upstream tracking branch)

git push origin :<old_name> <new_name>

https://stackoverflow.com/questions/6591213/how-do-i-rename-a-local-git-branch

Git aliases

Add aliases like this:

git config --global alias.rename 'branch -m'

Changing branches - but you have changes

simplest option - use stash

git stash
git checkout otherbranch
git stash pop

error: Your local changes to the following files would be overwritten by checkout:
        some_file
Please commit your changes or stash them before you switch branches.
Aborting

git stash save

https://stackoverflow.com/questions/22424142/error-when-changing-to-master-branch-my-local-changes-would-be-overwritten-by-c

Deleting a branch

git branch -d <local-branch>

If you have no changes that might be lost this won't work.

To force a delete

git branch -D >local-branch>

To delete a remote branch

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





$ git branch -d branch_name
warning: deleting branch 'branch_name' that has been merged to
        'refs/remotes/origin/branch_name', but not yet merged to HEAD.
Deleted branch branch_name (was 8dcb882).

Can undo recent changes - look at https://git-scm.com/docs/git-reflog https://github.blog/2015-06-08-how-to-undo-almost-anything-with-git/ http://gitready.com/intermediate/2009/02/09/reflog-your-safety-net.html

https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely

https://www.git-tower.com/learn/git/faq/delete-local-branch

Changing personal access tokens (PAT)

List config

# list all
git config --list

# list only the url used to push/pull
git config --get remote.origin.url

# change config
git remote set-url origin new.git.url/here

If PAT is added to the config, then it will appear in plain text

Unstage changes

eg. git added file, or accidentally clicked stage all changes

git reset <commit> -- <path>

commit is optional
-- is argument disambiguation, since argument can be related to branches or directories

git add somefile
git reset -- somefile

git reset - opposite of git add

git reset 
will unstage ALL changes from staging

Git pull, git fetch

Git fetch will go to the default remote and download the data to your local repo. It does not merge or modify what you are working on

Git pull will fetch and merge the remote branch into your local branch

remotes

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