User Tools

Site Tools


git

This is an old revision of the document!


git

Make the clone at the remote repository.

git clone git@github.com:dblume/get-shit-done
git remote add upstream git://github.com/icambridge/get-shit-done

Double checking the remote names…

$ git remote -v
origin  git@github.com:dblume/get-shit-done (fetch)
origin  git@github.com:dblume/get-shit-done (push)
upstream        git://github.com/icambridge/get-shit-done (fetch)
upstream        git://github.com/icambridge/get-shit-done (push)

Creating a new remote repository from an existing local one

I created hexbright-factory at https://github.com/new. Then, to create a new repository on the command line at the local computer:

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:dblume/hexbright-factory.git
git push -u origin main

Renaming branches

Updating both local and remote repos

To rename a remote branch:

  1. Rename the local one.
  2. Push the deletion of the old name.
  3. Push the new name.
  4. git remote prune origin

When someone else renamed a remote branch:

$ git fetch --all  # TODO investigate why fetch
$ git remote prune origin  # (where origin is the name of the shared repo)

Creating a branch (and possibly pushing to upstream origin)

$ git switch -c new_branch
Switched to a new branch 'new_branch'

That was the same as “git branch new_branch; git checkout new_branch

And now if you want to create that branch name at the remote branch, then:

$ git push --set-upstream origin new_branch

Changing a local branch to a new remote branch

This'll work if you don't have a local branch with that name already.

$ git checkout --track origin/branch_name

Making the current local branch track a new remote branch

$ git branch -u origin/branch_name

Updating a local repo after remote's already renamed its branch

git branch -m master main
git fetch --all --prune
git branch -u origin/main main
git remote set-head origin -a

Fixing a bug in its own branch

git switch -c bugfix/JIRA-1-new-bugfix
 
# If main is getting updated, rebase like so:
#   git checkout main
#   git pull
#   git checkout bugfix/JIRA-1-new-bugfix
#   git rebase main
 
# Consider whether you want to squash commits before pushing
#   git reset --soft HEAD~3
 
git commit -m "fixed bug"
git push --set-upstream origin bugfix/JIRA-1-new-bugfix
(do a merge/pull request that deletes the original branch at the remote)
git checkout main
git branch -d bugfix/JIRA-1-new-bugfix
git pull

Alternative to Rebasing: Stash, Pull, Unstash

Problem:

I attempted to rebase my branch to main and end up pulling in all of the intermediate commits on main into my branch, and the merge request suddenly requires approval from unrelated code owners.

Workaround:

Instead of doing a rebase before commit, stash your changes, set the upstream to origin/main, do a pull, unstash the change, then commit and push -f upstream to the branch. Kind of like this:

git stash push -m "hold for pull"
git branch --set-upstream-to origin/main
git pull
git stash pop (restores stash on top of main)
git add/commit
git push -f origin <branchname>

When using Merge Commits instead of Rebasing

Use –first-parent with git log.

Two Independent Remotes

After you've already set up one remote, origin, and you want to map your main to other-main at other-origin, you can do so like:

git remote add other-origin ssh://other.com/project.git
git fetch other-origin
git branch --set-upstream-to=other-origin/other-main main
git pull other-origin other-main --allow-unrelated-histories
git mergetool <file-with-conflicts>
... do your pushes and pulls, then to switch back to origin...
git branch --set-upstream-to=origin/main main

git at dlma.com

I created a remote git repo at dlma like so:

At the server:

git$ mkdir testcode.git
git$ cd testcode.git/
testcode.git$ git init --bare

Then, at the local computer:

testcode$ git init
Initialized empty Git repository in /home/David/testcode/.git/
testcode$ git add .
testcode$ git commit -m "first commit"
...
testcode$ git remote add origin ssh://USERNAME@dlma.com/~/git/testcode.git
testcode$ git push origin main

I could've used gitweb but I used GitHub-like gitlist at http://git.dlma.com.

git.1667338743.txt.gz · Last modified: 2023/04/12 20:44 (external edit)