Skip to content

Instantly share code, notes, and snippets.

@Sysa
Last active December 13, 2023 12:35
Show Gist options
  • Save Sysa/575973430edb1e60d5480491b97cbd4a to your computer and use it in GitHub Desktop.
Save Sysa/575973430edb1e60d5480491b97cbd4a to your computer and use it in GitHub Desktop.
Git_tips
Working with your repository
I just want to clone this repository
If you want to simply clone this empty repository then run this command in your terminal.
git clone http://localhost:7990/scm/tes/mytestrepo.git
My code is ready to be pushed
If you already have code ready to be pushed to this repository then run this in your terminal.
cd existing-project
git init
git add --all
git commit -m "Initial Commit"
git remote add origin http://localhost:7990/scm/tes/mytestrepo.git
git push -u origin master
My code is already tracked by Git
If your code is already tracked by Git then set this repository as your "origin" to push to.
cd existing-project
git remote set-url origin http://localhost:7990/scm/tes/mytestrepo.git
git push -u origin --all
git push origin --tags
-----------------------
git status
git add .
git commit -m "comment"
git push origin master
Deleting local branches in Git
$ git branch -d feature/login
Deleting remote branch in Git:
$ git push origin --delete feature/login
create new branch:
git checkout -b my-new-branch
to branch from master:
git checkout -b branch_name master
-------------------------
git fetch
git merge
(==git pull)
git fetch origin
git diff origin/master
to merge files:
-add filename
-commit
<git fetch origin>
git merge origin my-new-branch
git push origin my-new-branch:master
-------------------------
checkout into current branch from master:
git checkout dmgr2 # gets you "on branch dmgr2"
git fetch origin # gets you up to date with origin
git log
git merge origin/master
or just
git checkout dmgr2
git pull origin master
--------------------------
git remote set-url origin http://localhost.com/repo.git
http://localhost.com/repo.git
--------------------------
merge current branch into develop:
git checkout develop
git fetch
git pull origin develop
git checkout `your_current_branch_name`
git merge develop
git push origin `your_current_branch_name`
profit!
rebase:
git checkout experiment
git rebase master
maybe fast-forward merge required for master branch in this case:
git checkout master
$ git merge experiment
rebase in case of three branches:
$ git rebase --onto master serverBranch clientBranch
If clientBranch was checkouted from serverBranch branch,
it will take changes only from clientBranch and rebase them to the master,
without changes in serverBranch
rebase (include all work from serverBranch to the master branch):
git rebase master serverBranch
git blame:
git blame -L 50,54 filepath/evaluation/utils.py
git stash:
git stash
git checkout master
git pull origin master
git checkout -b "feat/new_branch"
git stash list
git stash pop OR git stash apply stash@{0}
revert just one file from another branch:
git checkout develop -- MessageProcessorTests.cs
this will copy the version of `MessageProcessorTests.cs` file from `develop` branch
git checkout specific file from other branch:
git fetch --all
git pull origin feat/mybranch
git checkout origin/develop -- package.json
# checkout only specific files (not commits) from another branch:
git checkout master
git pull
git checkout -b "3rd-branch"
git checkout --patch <2nd-branch> desired-file-name
and follow interactive gitdiff flow.
---
git patch file from another branch:
git checkout feature/my-fix
git fetch --all
git checkout --patch develop -- path/to/file
Stage this hunk [y,n,q,a,d,/,e,?]? ? <-- Look here, what I typed to get these.
y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk nor any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk nor any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
--- show current branch name in the terminal ---
--- linux/bash ---
# ~/.bashrc:
```
git_branch() {
git rev-parse --abbrev-ref HEAD 2> /dev/null
}
PS1='$....$ $(git_branch) \$'
or:
export PS1="$PS1\$(git_branch) \$ "
```
source ~/.bashrc
--- mac/zsh ---
# cat ~/.zshrc
# Git branch in prompt
parse_git_branch() {
git branch 2>/dev/null | sed -n -e 's/^\* \(.*\)/ (\1)/p'
}
setopt PROMPT_SUBST
PS1='%n@%m:%~$(parse_git_branch) $ '
--- remove / update git tags:
remove locally: git tag -d EMF.467
remove on remote: git push -d origin EMF.467
create locally: git tag -a EMF.467 -m "version EMF:467 - iOS" 339f42b
push to remote: git push origin --tags
list locally: git tags --list
list on remote: git ls-remote --tags origin
additional check: git log -7 --oneline
--- rollback to specific commit on local and remote:
git reset --hard 339f42b
git merge/do what you need
git add/commit
git push --force origin BranchName
--- working with PR branches `refs/remotes/pull/170414/merge` ---
git fetch origin pull/170414/merge:CI_problems
git checkout CI_problems
..do changes/tests..
git push -u origin CI_problems
--- history of authors in the project, based on commits (not lines of code) ---
git shortlog -sn
--- git accept theirs while conflicting with cli ---
for all files:
git merge --strategy-option theirs
file-by-file (theirs/ours):
git checkout --theirs -- <filename>
--- nasty push to remote from local for specific commit ---
git push <remotename> <commit SHA>:<remotebranchname>
--- revert last commit (not pushed to the remove) ---
git reset HEAD~1 --soft
--- recover from losing uncommited changes from git reset ---
git reflog
---
tf modules over ssh instead of https:
git config --global url."git@ssh.dev.azure.com:v3".insteadOf "https://CompanyName@dev.azure.com"
to cleanup it:
git config --global --remove-section url."git@ssh.dev.azure.com:v3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment