If you need to add everything except untracked files, i.e. only update changes to files that are already tracked
git add -u # --update$ cat .ssh/config# Check the Host line
# Then the remote origin url should be modified from, e.g.
# git@github.com:epfl-lasa/robetarme_ros2_wp5.2.git
# to
# git@github.com-robetarme-user:epfl-lasa/robetarme_ros2_wp5.2.git
Host github.com-robetarme-user
HostName github.com
User git
IdentityFile ~/.ssh/robetarme_passphraseless_key
IdentitiesOnly yes
# Default identity
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_iti581_github
IdentitiesOnly yesLet the commit in question have hash abc123. Then
git checkout <branch-a>
git cherry-pick abc123git diff main:src/file_a.txt feature:src/folder/file_b.txtGIT_SSH_COMMAND='ssh -i /path/to/private_key' git fetch originSay you want to get all yaml files which changed between two commits
git diff --name-only <commit-sha-1> <commit-sha-2> -- '**/*.yaml'If you want to diff them
commit_sha1=<commit-sha-1>
commit_sha2=<commit-sha-2>
files=$(git diff --name-only $commit_sha1 $commit_sha2 -- '**/*.yaml')
for file in $files; do git difftool $commit_sha1 $commit_sha2 -- "$file"; doneDon't worry. You can add the file(s) to the latest commit with
git add <forgotten-files>
git commit --amendpre-commit installpre-commit run -agit lfs installgit lfs track "file-larger-than-100MB.psd"or place files names and regexes in $REPOSITORY_PATH/.gitattributes
git lfs migrate import --include="*.bin,*.zip" --everything- Delete files
- Commit
bash git lfs prune
git status --porcelain | awk '{print $2}'If you add the following to ~/.gitconfig then issuing git changed-files will constitute an abbreviation
[alias]
changed-files = "!git status --porcelain | awk '{print $2}'"Useful when you need to merge changes but not incorporate all of them
# Switch to target branch (e.g. master)
git checkout target-branch
# Merge but don't commit yet. The use of --no-ff is essential
git merge --no-commit --no-ff source-branch
# Reset a specific file to its state before merge
git checkout HEAD -- path/to/file.txt
# Now commit the merge
git commit -m "Merge source-branch, excluding changes to file.txt"Unstage everything
git resetThen you may keep the files that you need and discard the rest.
GIT_SSH_COMMAND='ssh -i /path/to/private_key' git push origin main
--force-with-leaseis safer than--forceas it will refuse to push if someone else has pushed to the branch in the meantime, thus preventing you from accidentally overwriting their work
You are on branch master and you want to rebase origin/master on top of it. You can keep the version of origin/master instead of master with
# On branch master
# git rebase origin/master
git checkout --ours -- .You are on branch branch and you want to rebase your work on master. You can keep the version of branch instead of master with
# On branch branch
# git rebase master
git checkout --theirs -- .It is possible to resolve rebase conflicts automatically by favouring the changes from branch b (the branch being rebased) over those from master (the base branch, the branch onto which b is rebased). However, Git doesn’t do this by default; you need to explicitly instruct it. During a rebase with conflicts, Git pauses and asks you to resolve them. If you want to automatically accept all changes from branch b (i.e., ours in the context of a rebase) you can use:
git checkout --theirs -- .Caution
During a rebase the meanings of ours and theirs are reversed compared to a merge:
oursrefers to the incoming base (i.e.,master, the branch you're rebasing onto).theirsrefers to the current commit from branchb(the one being replayed).
After running the above command in the conflicted directory (or for specific files), stage the resolved files:
git add .Then continue the rebase:
git rebase --continueIf you want to do this non-interactively for the entire rebase, you can pass a strategy option before the rebase starts:
git rebase -X theirs masterThis tells Git to automatically prefer the changes from branch b (the theirs side during rebase) whenever conflicts occur.
Caution
Automatically discarding changes from master may unintentionally remove important updates.
git revert <commit-hash>With interactive rebase
git rebase -i <base-branch-or-commit>Locate the commit you want to remove in the editor. Then change the word pick to drop. Save and close the editor. If rebasing requires resolving conflicts then resolve them and continue the rebase with
git rebase --continuegit checkout <commit-hash> -- <filename>git revert --no-commit OLDEST_COMMIT_HASH^..NEWEST_COMMIT_HASH
git commit -m "Revert multiple commits: description of changes"# Revert specific commits (in reverse chronological order - latest first)
git revert --no-commit COMMIT_HASH_n COMMIT_HASH_n-1 ... COMMIT_HASH_n-k
git commit -m "Revert specific commits"You've made N incremental local commits but you want to squash them into one commit before pushing it.
git rebase -i HEAD~NAn editor will open with a list like:
pick 123abc Commit message 1
pick 456def Commit message 2
pick 789ghi Commit message 3
...
pick 012jkl Commit message NChange all but the first entries to squash
pick 123abc Commit message 1
squash 456def Commit message 2
squash 789ghi Commit message 3
...
squash 012jkl Commit message NSave and close the editor. Git will then prompt you to edit the new commit message. You can combine or edit the messages as needed. Then save and close the editor again. After that the commits will be squashed into one.
You need to merge a branch onto master but you want to squash all commits from b into one commit.
git checkout master
git merge --squash <branch>This does not create a merge commit. Instead it stages all the changes from <branch> into the index. Commit the squashed changes with
git commit -m "A single commit message summarizing all changes between <branch> and master"You have multiple commits on one branch that you need to rebase onto master, and there are potential conflicts with each commit. You want to avoid resolving multiple conflicts per commit and instead resolve the potential conflicts of one commit (if there are any after squashing them). Let the branch be my-branch. If you prefer not to rewrite my-branch's history then
- Create a new branch from
masterand switch to it
git checkout master
git checkout -b my-branch-squashedApply the net changes from my-branch as one commit:
git merge --squash my-branch
git commit -m "Squashed all changes from my-branch"You can now replace my-branch with my-branch-squashed
# On my-branch-squashed still
git rebase masterIf you prefer to rewrite history then do an interactive rebase on my-branch first:
Checkout my-branch
git checkout my-branchSquash all commits in my-branch into one (relative to master)
git rebase -i masterThis opens an interactive editor listing all commits in my-branch that are not in master. In the editor:
- Keep the first commit as pick.
- Change all subsequent commits to squash (or s).
- Save and close.
Git will then prompt you to edit the commit message for the squashed commit. Finalize it and save. Now my-branch has a single commit on top of master. If you want to update master with this change:
git checkout master
git merge --ff-only `my-branch`git resetgit show <commit-hash> -- <file>git log -p --follow <file>
# List commits only
# git log --follow <file>git difftool --cached <file>git diff --word-diffgit ls-files --others --ignored --exclude-standard