Skip to main content

3 posts tagged with "git"

View All Tags

· One min read
Hreniuc Cristian-Alexandru

This is a tutorial on how to set a git config for a specific folder where you have work related projects and a folder where you have personal projects.

Create a gitconfig file for your personal projects

Contents of th:

# In ~/data/personal/configs/git/.personal_gitconfig
[user]
name = Hreniuc Cristian-Alexandru
email = email@gmail.com

Add the path to that config in the global git config:

[includeIf "gitdir:~/data/personal/"]
path = ~/data/personal/configs/git/.personal_gitconfig

Make sure you add it at the end of .gitconfig and make sure the last \ from the If exists, otherwise, it won't work.

Source

· One min read
Hreniuc Cristian-Alexandru

I got this error:

⬤ quotes_api ⑂chreniuc_351 ♦ git commit -m "added rule 2.1, 3.2"
error: inflate: data stream error (incorrect header check)
error: unable to unpack 2429559f950ba19b0ce5a9aeedd87820305f36b6 header
fatal: loose object 2429559f950ba19b0ce5a9aeedd87820305f36b6 (stored in .git/objects/24/29559f950ba19b0ce5a9aeedd87820305f36b6) is corrupt
[2]+ Done gitk --all
⬤ quotes_api ⑂chreniuc_351 ♦ git status

I ran git fsck --full and removed all objects that were corupted, but nothing seemed to work.

I've recloned the repository in a different folder and did the following:

$ cd ..
$ git clone someremote newsrc
$ cd src
$ mv .git /tmp/bad-git
$ mv ../newsrc/.git .
$ rm -rf ../newsrc

Explaination: I've used the .git folder of a valid git repository. The changes of the files that were not commited were not lost, they appeared as changed. And then I added them and created a commit.

Drawbacks:

  • you will lose all local branches, when you use the .git folder
  • you will lose all local commits that were on your current branch and were not pushed on the remote. The changes will remain, they will apear as changes that were not added to a commit.

Source

· 2 min read
Hreniuc Cristian-Alexandru

1. Rebase ony one(or X) commits from a brach over another branch:

One commit:

git rebase --onto origin/devel branch_name~1 branch_name

Multiple commits:

git rebase --onto origin/devel branch_name~4 branch_name

2. Add a git worktree:

git worktree add --track -b new_branch_name name_of_folder_to_be_created_for_worktree branch_to_start_from

Example:

git worktree add --track -b branch_1 project_1 master

3. Checkout a single file from a specific commit:

git checkout commit_or_branch file_path

4. Overwriting local branch with remote branch:

git checkout branch
git fetch --all
git reset --hard origin/branch

be careful, this will remove any changes from your working tree!


5. Merge two branches witout checking out on the branch that me want to merge:

I want to merge the chreniuc_548 into devel.

One way to do it is this, but this does a checkout:

git checkout devel
git merge chreniuc_548

Without checking out, you can do this:

# Merge local branch foo into local branch master,
# without having to checkout master first.
# Here `.` means to use the local repository as the "remote":
git fetch . chreniuc_548:devel

# Merge remote branch origin/foo into local branch foo,
# without having to checkout foo first:
git fetch origin foo:foo

Source: https://stackoverflow.com/a/17722977


6. Push current branch:

git config --global push.default current
# Afterwards:
git push

Source: https://stackoverflow.com/questions/14031970/git-push-current-branch-shortcut


7. Aliases:

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status

# Afterwards:

git st

Source: https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases

Or, we can define them in .bashrc:

alias status='git status'
alias branch='git branch'
alias commit='git commit'
alias checkout='git checkout'
alias push='git push'
alias fetch='git fetch origin -p'
alias add='git add'
alias merge='git merge'