- How to add changes in an old commit:
- How to check for changes on remote Git repository?
- How to get a better git log view
- How to fix the branches master and origin/master if they have diverged ?
- How to revert to a previous stage ?
- How to copy commits from one branch to another ?
- How to delete a branch locally and remotely ?
- How to work with a fork directory ?
git add <my fixed files>
git commit --fixup=OLDCOMMIT
git rebase --interactive --autosquash OLDCOMMIT^
-
git rebase --interactive
will bring up a text editor to confirm (or edit) the rebase instruction sequence. Just save and quit the editor (:wq in vim) to continue with the rebase. -
--autosquash
will automatically put any--fixup=OLDCOMMIT
commits in the desired order.--autosquash
is only valid when the--interactive
option is used. -
^
inOLDCOMMIT^
indicates the commit just beforeOLDCOMMIT
⚠️ When rewritting git history, fixup or squash commits should only be made when the repository has not been published anywhere else...
# check the status of the remote repository
git remote update
git status
# check what files were modified in the last commit
git whatchanged origin/master -n 1
# bring local up to date
git pull origin master
- Run this one-time command in terminal:
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
- Then everytime you need to see the log, just run
git lg
and to see the lines that changed:git lg -p
git push origin master -f
git reset --hard origin/master
⚠️ Carefull You will lose all changes not yet pushed toorigin/master
.
To identify a specific commit, open the log with git lg
or, if the alias is not install, with git log --color --graph --pretty=oneline --abbrev-commit
Check out the desired commit:
git checkout 0d1d7fc32
To go back to where you were, just check out the branch you were on again.
- To get rid of everything you've done since a specific commit:
git reset --hard 0d1d7fc32
⚠️ This will destroy any local modifications.
- Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to.
More on this Stackoverflow post
- To reset the HEAD to
n
commits back the current point:git reset --hard HEAD~n
- To reset to a particular point in time instead of a specific commit, do:
git reset --hard master@{"10 minutes ago"}
. Other examples:HEAD@{1.day.2.hours.ago}
,HEAD@{yesterday}
,HEAD@{2020-01-01 18:30:00}
, orHEAD@{12:43}
. See gitrevisions
- Identify the commit ID (a1bdf98 and e8df632) to be copied from branchA to branchB
git checkout branchB
git cherry-pick a1bdf98 e8df632
git push origin branchB
Locally:
$ git branch -d branch_name
$ git branch -D branch_name
Note: The -d
option is an alias for --delete
, which only deletes the branch if it has already been fully merged in its upstream branch. You could also use -D
, which is an alias for --delete --force
, which deletes the branch "irrespective of its merged status." [Source: man git-branch
]. In addition, git branch -d branch_name
will fail if you are currently
in the branch you want to remove.
Remotely:
git push -d <remote_name> <branch_name>
- Once a repository is forked from an original repository and cloned locally, add the address of the upstream repository that will be synced with the fork:
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
- Verify the new upstream repository you've specified for your fork:
git remote -v
> origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
> origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
> upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
> upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
To sync a fork directory with the original directory, we follow these steps:
-
In the working git directory, fetch the latest changes from the upstream repository using the command:
git fetch upstream
-
Sync local branches with upstream branches:
- Make sure that a local branch corresponding to each upstream branch exist. To list the local and remote branches use the command
git branch -av
. To create a local copy of branches that exist only on upstream directory (e.g. here a branch namedlegacy
), we can use this one-time command:
git checkout -b legacy upstream/legacy
Once this is done, the command
git branch -av
should return something like:git branch -av * legacy fe06d0c . master ca9fe77 [ahead 1] dy120 exec version remotes/origin/HEAD -> origin/master remotes/origin/master fe06d0c . remotes/upstream/legacy fe06d0c . remotes/upstream/master ca9fe77 dy120 exec version
- To delete all the changes made on the forked master branch and make it identical to the upstream/master branch do:
# ensures current branch is master git checkout master # pulls all new commits made to upstream/master git pull upstream master # this will delete all your local changes to master git reset --hard upstream/master # take care, this will delete all your changes on your forked master git push origin master --force
Now the fork repository should have its two branches up-to-date with the upstream repository
- Make sure that a local branch corresponding to each upstream branch exist. To list the local and remote branches use the command
-
Merge changes in the forked repository with changes made in the original repository. Particularly useful to not loose local local changes when bringing your fork's branch into sync with the upstream repository:
# First check out your fork’s local branch brancha
git checkout brancha
# Then merge the changes from the upstream branch branchb by doing
git merge upstream/branchb
Great best practice guide on git-workflow accessible here: https://www.asmeurer.com/git-workflow/