Handy git commands

 by Aditya Kapre Shrewsbury Massachussets Boston

To reword/change last N commit messages

    git rebase -i HEAD~N
    [Then replace "pick" by letter "r" and exit]
    [Interactively change each message one by one by exiting each time message is changed]

Do not check in certain files such as local config values
https://stackoverflow.com/a/48882519/3361121 
    git update-index --skip-worktree MySetupClass.java
    
Undo above skip worktree
------------------------
    git update-index --no-skip-worktree <file>

get-latest-from-master-to-feature
----------------------------------
1. Assume you are on feature branch currently and have untracked changes (git add not applied yet), 
then Stash current changes not committed
    a. git stash save --include-untracked -m "my changes to be used for later"
    b. git stash list --date=relative (see those are shown in list)
    c. git stash list --format='%gd (%cr): %gs'  (more detailed that above)

2. Checkout master
    a. git checkout master
    b. git pull (get latest master)
    c. git checkout feature/branch (Checkout feature again)
    d. git rebase master (get current changes from master into feature branch)
    e. git push -f (so that remote branch also becomes current w/ changes in master)
    f. git stash apply stash@{stash_number}  (get back to where we left on feature branch, 
    now you have changes from recent master)

Delete particular stash
-----------------------
git stash drop stash@{put_stash_number_to_be_deleted}

Stash particular file/s
-----------------------
    [Folder name should end in /]
    git stash push {file/folder name seperated by space} -m "my _stash_comment" --include-untracked
    
Checkout remote branch named test
---------------------------------
    git checkout -b test origin/test
    
Rename local branch
-------------------
    Checkout local branch and run below
    git branch -m <newname>

Rename remote branch
--------------------
    First rename local branch as above
    
    Then,
    # Delete the old branch on remote - where <remote> is, for example, origin
    git push <remote> --delete <old_name>
    
    # Prevent git from using the old name when pushing in the next step.
    # Otherwise, git will use the old upstream name instead of <new_name>.
    git branch --unset-upstream <old_name>
    
    # Push the new branch to remote
    git push <remote> <new_name>
    
    # Reset the upstream branch for the new_name local branch
    git push <remote> -u <new_name>
    
Check which remote branch is local branch tracking
--------------------------------------------------
    git branch -vv
    
Make terminal compatible with git
---------------------------------
    Refer following doc : https://github.com/ohmyzsh/ohmyzsh
    Install required fonts: https://stackoverflow.com/a/54096652/3361121
    For intelliJ use following: https://stackoverflow.com/a/66479094/3361121
    
Delete local branch
-------------------
    git branch -d {branch_name}
    
Remove all files accidently added using git add .
-------------------------------------------------
    git reset
    
Push ONLY a SPECIFIC commit to remote branch
--------------------------------------------

    https://stackoverflow.com/a/3230241
    
    git push <remotename> <commit SHA>:<remotebranchname>
    
    <remotename> : 
    It is mostly "origin", can be found by executing below command. 
    This is a remote repo name to which push will happen
        git remote -v
        
    <commit SHA> : 
    It is the SHA (Secure Hash Algorithm) value of the commit we want to push. 
    It can be found using below command. Below command shows all the commits on the branch you made. 
    Select the commit you want to push to remote. 
    After pushing the commit and executing the below command again, it will show new state of your remote.
        git log master..HEAD
    
    <remotebranchname> : 
    This is name of the remote branch that you created for writing code for this feature. 
    You can go on github and find the name of this branch
    
To view all your commits on the branch
--------------------------------------
    git shortlog master..HEAD
    
Change timestamp of commit
--------------------------
    https://stackoverflow.com/a/41997774/3361121
    
    git rebase -i HEAD~N
    e
    :wq
    git commit --amend --date="now" OR git commit --amend --date="2021-05-24T10:00:00"
    :wq
    git rebase --continue
    e
    :wq
    .......so on till N
    
    ALSO BELOW COMMAND WORKS TO CHANGE COMMITER DATE AS WELL AS AUTHOR DATE
        GIT_COMMITTER_DATE="2021-07-08T18:00:00" git commit --amend --no-edit --date="2021-07-08T18:00:00"
    After this command there is no need to do :wq since it does not edit commit message as we specify --no-edit
        
    COMMITER DATE AND AUTHOR DATE CAN BE SEEN VIA BELOW COMMAND
        git show Your commit SHA number --pretty=fuller
    
    
Change commit message
---------------------
    git commit --amend
    
    OR
    
    git rebase -i HEAD~n
     - then choose e/r
    https://stackoverflow.com/a/179147/3361121  

Comments

StackOverflow

profile for Aditya Kapre Shrewsbury at Stack Overflow, Q&A for professional and enthusiast programmers

LinkedIn

Popular posts from this blog

API Keys

How to Use JIVE