Skip to content

Instantly share code, notes, and snippets.

@arsho
Last active November 16, 2023 03:53
Show Gist options
  • Select an option

  • Save arsho/0f11ce4b625a518522f0646423f99e8c to your computer and use it in GitHub Desktop.

Select an option

Save arsho/0f11ce4b625a518522f0646423f99e8c to your computer and use it in GitHub Desktop.
Git workflow , git conflict , merging repository , git remote

Switching remote URLs from HTTPS to SSH

  • Open Terminal.
  • Change the current working directory to your local project.
  • List your existing remotes in order to get the name of the remote you want to change.
    $ git remote -v
    > origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
    > origin  https://github.com/USERNAME/REPOSITORY.git (push)
    
  • Change your remote's URL from HTTPS to SSH with the git remote set-url command.
    $ git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
    
  • Verify that the remote URL has changed.
    $ git remote -v
    # Verify new remote URL
    > origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
    > origin  git@github.com:USERNAME/REPOSITORY.git (push)
    

References:

Branch Push

git add --all
git commit -m "Issue:xxx message"
git push origin branch_name

Create New Branch

git fetch origin
git checkout develop
git reset --hard origin/develop
git checkout -b branch_name

Working on existing Branch Shortcut

git checkout branch_name
git fetch origin
git rebase origin/develop
git push origin branch_name -f

Reset to original file / Remove untracked code from a file

git checkout FILE_PATH

Good Practice for existing branch

git fetch origin
git checkout develop
git reset --hard origin/develop
git checkout branch_name
git rebase develop
git push origin branch_name -f

Install Git in Ubuntu 18.04 LTS

  • Install Git:
    sudo apt install git
    
  • Add global configuration for Git user:
    git config --global user.email "you@example.com"
    git config --global user.name "Your Name"
    
  • Add SSH. Details can be found in this gist

Reference:

Scenario

git status -s is listing all files as modified even after resetting hard git reset --hard origin/main.

To resolve this:

git config core.filemode false
git reset --hard origin/main

Reference

Creating new branch from current branch content with uncommitted changes

Check the current git status

git status
# Output: On branch develop

Save uncommited changes and move to the last commit of develop

git stash

Create new branch feature/NEW-142 and switched to it

git checkout -b feature/NEW-142

Bring the uncommited changes to this new branch

git stash pop

Add every changes of this branch to git

git add .

Create a commit

git commit -m "NEW-152: added env variables"

Push the commit to this branch

git push origin feature/NEW-142

Resolve Git conflicts

git fetch origin
git rebase TARGET_BRANCH
git rebase --continue
CHANGE FILES
git add FILENAME
git rebase --continue
git push -f TARGET_BRANCH

Keep the origin's changes

git checkout --ours FILENAME

Scenario

The error occurred when trying to push code to the same branch. It was showing:

Updates were rejected because the tip of your current branch is behind

Solution

Rebase the current branch with the dev(Change it to your own main branch) branch.

git add --all
git commit -m "feature-555: commit message"
git push origin feature-555
(Error occurred in this step)
git fetch origin
git rebase origin/dev
git push origin feature-555 -f

Scenario

A repository is forked from another repository. Now the forked repository is needed to rebase with original repository.

Solution

  • Add the original repository as upstream:
    git remote add upstream https://example.com/[Original Owner Username]/[Original Repository].git
    
  • List currently configured remote repositories:
    git remote -v
    
    It will show the origin and upstream:
    origin https://example.com/[Your UserName]/[Your Fork].git (fetch)
    origin https://example.com/[Your UserName]/[Your Fork].git (push)
    upstream https://example.com/[Original Owner UserName]/[Original Repository].git (fetch)
    upstream https://example.com/[Original Owner UserName]/[Original Repository].git (push)
    
  • Fetch contents from upstream:
    git fetch upstream
    
  • Checkout to an existing branch (e.g. master):
    git checkout master
    
  • Rebase this branch with upstream's branch:
    git rebase upstream/master
    
  • Push changes to forked repository:
    git push origin master
    

References:

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