Cheatsheets and common recipes for git
Last active
March 22, 2024 23:14
-
-
Save benc-uk/5df97c0d572169eacf25f89272ed8ac6 to your computer and use it in GitHub Desktop.
Git Cheatsheet & Recipes
Command | Description |
---|---|
git init |
Create a git repo in current folder |
git clone https://blah/repo |
Clone a remote repo |
git remote add origin https://github.com/user/blah.git
git push -u origin master
echo "# foo" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/user/blah.git
git push -u origin master
Command | Description |
---|---|
git push |
Default push (normally to upstream, see push.default docs) |
git push -u origin mybranch |
Push branch mybranch to origin and set it to be upstream |
git pull |
Pull changes from default remote and bring everything up to date |
git pull someremote mybranch |
Pull branch mybranch from remote someremote |
get fetch |
Download remote changes but don't merge them, they will be in FETCH_HEAD |
git fetch --prune |
Re-sync local and remote branches |
Command | Description |
---|---|
git branch mybranch |
Create branch but don't change to it |
git checkout -b mybranch |
Create branch and change to it |
git checkout mybranch |
Change to existing branch |
git branch -d mybranch |
Delete local branch |
git branch -D mybranch |
Delete local branch force |
git push -d origin mybranch |
Delete local & remote branch |
cd ~/.ssh
ssh-keygen -t rsa -C "user_blah"
Save to a new file, for this example I'll use the name new-git-key
but it can be anything of course!
Add the SSH agent to your bash/zsh start up script(s), i.e. .bashrc
or .zshrc
This does:
- Enables and runs the agent in the background
- Adds your keys to the agent, place these lines in the same start-up script (they don't persist otherwise) just after enabling it. Add your default key (normally called
id_rsa
) and your new one.
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa 2> /dev/null
ssh-add ~/.ssh/new-git-key 2> /dev/null
Create ~/.ssh/config
if it doesn't exist, and add the following.
The name github_new
is effectively an alias, and you can pick any name you like.
Host github_new
HostName github.com
IdentityFile ~/.ssh/new-git-key
Then use the alias github_new
instead of hostname github.com
in your git operations
Example:
git clone git@github_alt:some-org/some-repo.git
-
Create a branch from the tag
git branch {tagname}-branch {tagname}
git checkout {tagname}-branch
-
Include the fix manually if it's just a small change
git add .
git commit -m "Fix included"
or cherry-pick the commitgit cherry-pick {num_commit}
or merge a branch (e.g. master) with fixes/patchgit merge master
-
Delete and recreate the tag locally
git tag -d {tagname}
git tag {tagname}
-
Delete and recreate the tag remotely
git push --delete origin {tagname}
git push origin {tagname}
-
Update local repository with the updated tag
git fetch --tags
-
Merge back to master if required
git checkout master
git merge {tagname}-branch
git branch -d {tagname}-branch
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment