Skip to content

Instantly share code, notes, and snippets.

@lhoupert
Last active December 20, 2021 21:56
Show Gist options
  • Save lhoupert/4688a3f7ea9d88a2696670bb6fd7b177 to your computer and use it in GitHub Desktop.
Save lhoupert/4688a3f7ea9d88a2696670bb6fd7b177 to your computer and use it in GitHub Desktop.
Advances file manipulation in Git

Useful Git commands



How to add changes in an old commit:

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.

  • ^ in OLDCOMMIT^ indicates the commit just before OLDCOMMIT

    ⚠️ When rewritting git history, fixup or squash commits should only be made when the repository has not been published anywhere else...

https://stackoverflow.com/questions/2719579/how-to-add-a-changed-file-to-an-older-not-last-commit-in-git



How to check for changes on remote Git repository?

# 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



How to get a better git log view

  • 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



How to fix the branches master and origin/master if they have diverged ?

To force the the origin/master to be identical to the local master branch:

git push origin master -f

To force the local master branch to be identical to the origin/master branch:

git reset --hard origin/master

⚠️ Carefull You will lose all changes not yet pushed to origin/master.



How to revert to a previous stage ?

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

Temporarily switch to a different 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.

Hard delete unpublished commits

  1. To get rid of everything you've done since a specific commit: git reset --hard 0d1d7fc32

⚠️ This will destroy any local modifications.

  1. 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

Other ways to reset to a previous stage

  1. To reset the HEAD to n commits back the current point: git reset --hard HEAD~n
  2. 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}, or HEAD@{12:43}. See gitrevisions



How to copy commits from one branch to another ?

  1. Identify the commit ID (a1bdf98 and e8df632) to be copied from branchA to branchB
  2. git checkout branchB
  3. git cherry-pick a1bdf98 e8df632
  4. git push origin branchB



How to delete a branch locally and remotely ?

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>



How to work with a fork directory ?

Set up a forked repository

  • 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)

Fetch/pull changes from original repository onto a local copy of the forked repository

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 named legacy), 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

  • 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/

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