Skip to content

Instantly share code, notes, and snippets.

@nbigot
Created September 20, 2024 23:19
Show Gist options
  • Save nbigot/76d949fb028e507f39e0072c38086b08 to your computer and use it in GitHub Desktop.
Save nbigot/76d949fb028e507f39e0072c38086b08 to your computer and use it in GitHub Desktop.
How to - git commands

Howto - GIT

Docs

http://rogerdudler.github.io/git-guide/index.fr.html

Install dir (on Windows)

"C:\Program Files\Git\git-cmd.exe" --cd-to-home
"C:\Program Files\Git\git-bash.exe"

Some commands

git pull master

create a branch:

create branch gh-1234

git checkout -b gh-1234

commit

git status
git add src/dir/myfile.py

git commit -a -m 'gh-1234: my comment'
git commit -m 'gh-1234: my comment'
git push --set origin origin/gh-1234
git push origin origin/gh-1234

switch back to the master branch

git checkout master

git status
git stash save gh-XXXXXX
git stash list
git stash show stash@{0}
git stash apply stash@{0}
git stash drop stash@{0}


git log master --oneline
git log master
git log master --pretty=format:"%h - %an, %ar : %s"
git log master --since="2024-08-01"
git log master --since=1.month
git log master --since=1.day
git log master --grep=myname --pretty=format:"%h - %an, %ar : %s"
git log master --grep=gh-1234 --since=30.day
git log master --since=1.month --pretty=format:"%h - %an, %ar : %s"

git log master --grep=gh-1234 --since=30.day

display the content of a commit

git log -p 1bdb2b2b12c6260f23a8978e563163539960781e

compare 2 commits

git show --pretty=oneline --name-only --abbrev-commit ^2423a1d4 c142d5b7

merge master into my branch

git checkout mybranch
git pull
git status
git fetch origin master
git merge origin/master
git push
git status

merge my branch into master

git checkout master
git pull
git status
git fetch origin mybranch
git merge origin/mybranch
git push
git status

compare two revisions in git command line

configure winmerge in

C:\Users\<my account>\.gitconfig

compare all files from 2 commits

git difftool 2423a1d4 c142d5b7

compare a specific file from 2 commits

git difftool 2423a1d4:src/myfile.py c142d5b7:src/myfile.py

get the list of available branches

git branch -a

git branch --contains a4668a08

git difftool a4668a08:src/myfile.py c142d5b7:src/myfile.py

cancel an ongoing merge not yet committed

git merge --abort

clone the repo myproject

git.exe clone --progress -v "git@git.priv.mydomain.com:team/myproject" "C:\projects\myproject"

retrieve the branch at a specific commit

git checkout <sha1 commit value>

force retrieve the latest version of the branch

(useful when the branch has been overwritten)

git checkout mybranch
git fetch
git reset --hard origin/mybranch
git pull

rename a branch

let's imagine my current branch is mybranch I want to rename it to gh-1234

git checkout mybranch
git pull
git branch -m gh-1234
--non-- git push
git push origin gh-1234
git push --set-upstream origin gh-1234
git difftool 085c4586dc93f30e538238850

git reset --hard HEAD
git reset --hard origin/master

git reset --hard origin/another-branch
git pull
git status
git log

Revert changes to modified files

git reset --hard

Remove all untracked files and directories

(-f is force, -d is remove directories)

git clean -fd

display the commit history of a specific file

gitk <filename>
exemple:
gitk mydirectory/myfile.py

get a specific version of the branch

git fetch origin
git checkout YOUR_SHA_HERE

ex: git checkout 44cdf6a8095a58f7d6770f7eba1a320a08dbb223

Verify that branch X has been merged into branch Y

$ git branch --all --merged origin/Y X

Clean the repo

    $ git gc

How to force an overwrite of local files

    $ git fetch --all
    $ git reset --hard origin/master

Misc

    $ git log --graph --decorate

Delete a branch

Delete the local branch:

$ git branch -d <branchname>
$ git gc

Remove a remote branch from the server:

$ git push origin --delete {the_remote_branch}

Find old branches

Find all local branches

$ git branch --sort=-committerdate -v

Then

For all <branchname> in [old branches(already closed/on master)]:
$ git branch -d <branchname>
$ git gc

Magic rebase

If another dev has pushed to my branch, and I want to retrieve their modifications while I have files in progress:

https://cscheng.info/2017/01/26/git-tip-autostash-with-git-pull-rebase.html

This command:

  • stashes the files in progress
  • retrieves the commits pushed to the branch (by other devs)
  • unstashes the stashed files
$ git pull --rebase --autostash

Apply patch in a new branch

$ git checkout mybranch
$ git pull
$ gut status
$ git fetch origin master
$ git merge origin/master
$ git pull
$ git status
$ git diff mybranch..master > ../mybranch.patch
$ git checkout master
$ git pull
$ git checkout -b gh-1234
$ git status
$ git apply --3way --ignore-space-change --ignore-whitespace /projects/mybranch.patch
$ git commit
$ git push

Squash the last commits

/!\ works only if no branch has been merged into this branch (neither master)

Squash last commits of a specifc branch into a single commit.

$ git checkout <branch_name>
$ git pull
$ git status

(replace 3 by the number of commits to squash)

$ git reset --soft HEAD~3
$ git commit -m "my commit message"
$ git push origin <branch_name> --force

Example:

$ git reset --soft HEAD~3
$ git commit -m "feat(gh-1234): my new great feature"
$ git push origin gh-1234 --force

Rebase master into branch

Reintegrate the latest changes from master into my branch without using merge

$ git checkout master
$ git pull
$ git checkout <branch_name>
$ git pull --rebase origin master
$ git push --force-with-lease

In case there are merge conflicts

$ git checkout master
$ git pull
$ git checkout <branch_name>
$ git pull --rebase origin master

example if there are conflicts:

git pull --rebase origin master
From gitlab.priv.mydomain.com:team/project
 * branch            master     -> FETCH_HEAD
error: could not apply ce26679... feat(gh-1234): my commit message
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply ce26679... feat(gh-1234): my commit message
Auto-merging config/myfile.yaml
CONFLICT (content): Merge conflict in config/myfile.yaml

resolve the merge conflicts, then

$ git rebase --continue
$ git push --force-with-lease

Pycharm Squash commits & rebase

tutorial

  1. Open the git window
  2. Left-click the first commit (the oldest in time) of my branch to select it
  3. Right-click and choose "interactive rebase" in the popup menu
  4. Left-click on the bottom-most commit in the list to select it
  5. Click the "Squash" button
  6. Edit the commit message
  7. Click on the icon on the left that looks like 2 small circles
  8. Click the "Start Rebasing" button
  9. In the git window, click on the newly generated commit (to select it)
  10. Right-click to open the popup and click "Push All up to Here..."
  11. DO NOT CLICK THE "Push" BUTTON!!!
  12. Click on the down arrow of the "Push" button to display the drop-down menu
  13. Click "Force Push"
  14. (a dialog box appears) Confirm by clicking the "Force Push" button

Git archive

$ git archive -o myproject.zip HEAD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment