Git - animeshtrivedi/notes GitHub Wiki
git diff --name-only <commit1> <commit2>
https://www.geeksforgeeks.org/how-to-list-only-the-names-of-files-that-changed-between-two-commits/
- First, switch to the branch where you want to copy the file to (the target branch).
git checkout target-branch
- Checkout the file from the other branch
git checkout source-branch -- path/to/file_or_dir
- Add the new files to the target branch
git add path/to/file
- Commit and push
git commit -m "Copy file from source-branch to target-branch"
git push origin target-branch
git restore -s <SHA> -SW ./specfic-file.txt
you should get the index reverted as well.
https://stackoverflow.com/questions/610208/how-to-retrieve-a-single-file-from-a-specific-revision-in-git https://git-scm.com/docs/git-restore
https://gist.github.com/animeshtrivedi/7b2f742524a0ee3795635a79666f8faf
git rev-list --left-right --count origin/develop...feature-branch | awk '{print "Behind "$1" - Ahead "$2""}'
IMPORTANT; make a copy of the git repo code
atr@atrnuc:~/vu/github/animeshtrivedi/papers/xxx (another copy)$ git reflog
6189770 (HEAD -> master) HEAD@{0}: reset: moving to HEAD@{4}
6189770 (HEAD -> master) HEAD@{1}: reset: moving to ORIG_HEAD
0bb137d (origin/master, origin/HEAD) HEAD@{2}: pull origin master --rebase (finish): returning to refs/heads/master
0bb137d (origin/master, origin/HEAD) HEAD@{3}: pull origin master --rebase (pick): adding opening 3 paragraphs for introduction
d50767d HEAD@{4}: pull origin master --rebase (start): checkout d50767df79a500f4b8701c0215965db870962a5a
6189770 (HEAD -> master) HEAD@{5}: commit: adding opening 3 paragraphs for introduction
d6e2c15 HEAD@{6}: commit (amend): Merge branch 'master' of repo_name
d50767d HEAD@{7}: clone: from repo_name.git
atr@atrnuc:~/vu/github/animeshtrivedi/papers/xxx (another copy)$ git checkout HEAD@{5}
Note: switching to 'HEAD@{5}'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 6189770 adding opening 3 paragraphs for introduction
atr@atrnuc:~/vu/github/animeshtrivedi/papers/xxx (another copy)$ ls ./sections/
00-abstract.tex 10-introduction2.tex 10-introduction.tex 20-background.tex 30-design.tex 40-implementation.tex 50-evaluation.tex 60-discussion.tex 80-relatedwork.tex 90-conclusion.tex consistency.tex
Life saver on getting the files which were deleted due to the git command activity
- https://stackoverflow.com/questions/134882/undoing-a-git-rebase
- https://stackoverflow.com/questions/26247614/files-deleted-after-rebase-with-git-how-to-recover
sign_and_send_pubkey: signing failed for RSA "/home/atr/.ssh/id_rsa" from agent: agent refused operation
most likely loose permission issue.
chmod 600 ~/.ssh/*
atr@u24clean:~$ cat ~/.ssh/config
Host github.com
IdentityFile ~/.ssh/github-key
# in an already other git
git submodule add [email protected]:animeshtrivedi/test.git test
Cloning into '/home/atr/vu/github/animeshtrivedi/zns-resources/test'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
$ git submodule update --init --recursive
$ cd test/
$ ls
README.md
https://github.blog/2016-02-01-working-with-submodules/
For error about the branch
fatal: You are on a branch yet to be born
Unable to checkout submodule 'test'
# delete the .git/submodule/test <-- whatever folder was the repo name
git log --tags --simplify-by-decoration --pretty="format:%ci %d"
https://stackoverflow.com/questions/13208734/get-the-time-and-date-of-git-tags/13208830
$ git remote rename <old-name> <new-name>
https://stackoverflow.com/questions/33840617/how-do-i-rename-a-git-remote
git remote add my_awesome_new_remote_repo git@git
https://stackoverflow.com/questions/9512549/how-to-fast-forward-a-branch-to-head
https://www.alexkras.com/getting-started-with-git/
git merge --abort
https://stackoverflow.com/questions/5741407/how-to-undo-a-git-merge-with-conflicts
git push upstream local-name:remote-name
https://penandpants.com/2013/02/07/git-pushing-to-a-remote-branch-with-a-different-name/
Always clone and make your modification in a separate branch. You can keep pushing changes there, and the pull request will update accordingly.
https://stackoverflow.com/questions/40503417/how-to-add-a-file-to-the-last-commit-in-git
git add the_left_out_file
git commit --amend --no-edit
git push --force original master # make sure that you dont mess up history
git add --patch <filename>
Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]?
y stage this hunk for the next commit
n do not stage this hunk for the next commit
q quit; do not stage this hunk or any of the remaining hunks
a stage this hunk and all later hunks in the file
d do not stage this hunk or any of the later hunks in the file
g select a hunk to go to
/ search for a hunk matching the given regex
j leave this hunk undecided, see next undecided hunk
J leave this hunk undecided, see next hunk
k leave this hunk undecided, see previous undecided hunk
K leave this hunk undecided, see previous hunk
s split the current hunk into smaller hunks
e manually edit the current hunk
? print hunk help
# then
* git diff --staged to check that you staged the correct changes
* git reset -p to unstage mistakenly added hunks
* git commit -v --sign-off to view your commit while you edit the commit message.
https://stackoverflow.com/questions/1085162/commit-only-part-of-a-file-in-git
git restore .
clean up the current working directory
git archive master | bzip2 >source-tree.tar.bz2
https://stackoverflow.com/questions/160608/do-a-git-export-like-svn-export
git reset --soft HEAD^
#or
git reset --soft HEAD~1
#Then reset the unwanted files in order to leave them out from the commit:
git reset HEAD path/to/unwanted_file
#Now commit again, you can even re-use the same commit message:
git commit -c ORIG_HEAD
https://stackoverflow.com/questions/12481639/remove-files-from-git-commit
atr@valva:~/zrl/external/github/apache/incubator-crail$ git push apache master
Username for 'https://git1-us-west.apache.org': atrivedi
Password for 'https://[email protected]':
To http://git-wip-us.apache.org/repos/asf/incubator-crail.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'http://git-wip-us.apache.org/repos/asf/incubator-crail.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
git fetch apache
git rebase apache/master
git push --force origin master
git push apache master
git remote rename old_name new_name
https://support.beanstalkapp.com/article/1000-how-do-i-rename-an-existing-git-remote
How to rebase:
git rebase upstream/master master
Q: What does --ff-only does?
git merge --ff-only origin/master
When you have a linear history from the current branch to the master. Then checkout the branch and then call merge with --ff-only.
git merge? what does it do
git merge JIRA-61 master
git reset --hard origin/master
git show COMMIT
git diff COMMIT^ COMMIT
https://stackoverflow.com/questions/17563726/how-to-see-the-changes-in-a-git-commit
git config user.email "[email protected]"
git config user.name "Animesh Trivedi"
git commit --amend --signoff
git commit --signoff
https://stackoverflow.com/questions/1962094/what-is-the-sign-off-feature-in-git-for
Guidelines: https://gist.github.com/robertpainsi/b632364184e70900af4ab688decf6f53
In case you have pushed already then do the same and then
git push --force example-branch
https://help.github.com/en/github/committing-changes-to-your-project/changing-a-commit-message
When to sign multiple previous commits: Example for 2
git filter-branch --msg-filter "cat - && echo && echo 'Signed-off-by: Your Name <[email protected]>'" HEAD~2..HEAD
#then
git push -f origin
https://gist.github.com/kwk/d70f20d17b18c4f3296d
git remote -v
git remote add upstream https://
and then
git checkout master
git fetch upstream
git merge --ff-only upstream/master
https://help.github.com/articles/syncing-a-fork/#platform-linux https://help.github.com/articles/configuring-a-remote-for-a-fork/
git fetch upstream
git pull upstream master
git push origin master
https://gist.github.com/CristinaSolana/1885435
# undo the last commit, but leave the changes available
git reset HEAD~ --soft
git stash
# move to the correct branch
git checkout name-of-the-correct-branch
git stash pop
git add . # or add individual files
git commit -m "your message here"
# now your changes are on the correct branch
git add -p [filename]
https://stackoverflow.com/questions/35736116/making-a-git-push-from-a-detached-head/35736137
git push -u new_remote_name local_branch:new_remote_branch
https://multiplestates.wordpress.com/2015/02/05/rename-a-local-and-remote-branch-in-git/
- Rename your local branch. If you are on the branch you want to rename:
git branch -m new-name
If you are on a different branch:
git branch -m old-name new-name
- Delete the old-name remote branch and push the new-name local branch.
git push origin :old-name new-name
- Reset the upstream branch for the new-name local branch. Switch to the branch and then:
git push origin -u new-name
git branch branchName <sha1>
https://stackoverflow.com/questions/4025916/git-undo-local-branch-delete
git rm --cached _file_
git tag <tagname>
git push origin --tags # for all tags
git push origin <tagname> # for one tag
https://stackoverflow.com/questions/18216991/create-a-tag-in-github-repository
Adding a tag to a specific commit
$ git tag -a v1.2 9fceb02 -m "message"
https://git-scm.com/book/en/v2/Git-Basics-Tagging
The last commit
git revert HEAD
Last multiple commits
$ git revert --no-commit B..HEAD
$ git commit -m 'message'
https://stackoverflow.com/questions/1463340/revert-multiple-git-commits
delete remote commits
git reset --hard id
git push remote_id (origin) +master
delete local commits
git reset --hard origin/<branch_name>
git commit --amend
https://help.github.com/articles/changing-a-commit-message/
git pull origin master
git push origin branch
http://stackoverflow.com/questions/52704/how-do-i-discard-unstaged-changes-in-git
git checkout .
discard everything and reset:
git reset --hard HEAD
https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-both-locally-and-remotely
git push -d <remote_name> <branchname> # Delete remote
git branch -D <branchname> # Delete local
Note: In most cases, <remote_name> will be origin.
Delete Local Branch To delete the local branch, use one of the following:
git branch -d <branch_name>
git branch -D <branch_name>
- The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch.
- The -D option is an alias for --delete --force, which deletes the branch "irrespective of its merged status." [Source: man git-branch] As of Git v2.3, git branch -d (delete) learned to honor the -f (force) flag.
- You will receive an error if you try to delete the currently selected branch.
git branch -a (for_all)
git branch -r (for remote)
Get the remote branch name from the list command.
git checkout -b dev remotes/origin/dev
git checkout -b <branch>
git push -u origin <branch>
$ git clone
will give you the whole repository. After the clone, you can list the tags with
$ git tag -l
and then checkout a specific tag:
$ git checkout tags/<tag_name>
Even better, checkout and create a branch (otherwise you will be on a branch named after the revision number of tag):
$ git checkout tags/<tag_name> -b <branch_name>
When a local branch has the same name as a tag, it crates a conflict. So when you checkout a tag as a branch and push it to another git repo you get
$ git push -u origin XXX
error: src refspec XXX matches more than one.
error: failed to push some refs to '[email protected]'
To remedy this, one can refer to a branch as refs/heads/XXX
and the tag as refs/tags/XXX
. This removes the ambiguity. Then one can push as
$ git push -u origin refs/heads/XXX
When updating an already cloned git, you need to fetch tags explicitly as
$ git fetch --tags repo branch
https://stackoverflow.com/questions/1204190/does-git-fetch-tags-include-git-fetch
you can find commitID that you want to apply and then just
$ git cherry-pick commitID
FYI: http://stackoverflow.com/questions/881092/how-to-merge-a-specific-commit-in-git
and if you want to revert is then (to undo your last commit)
git reset --hard HEAD~
https://stackoverflow.com/questions/30986376/how-to-undo-a-successful-git-cherry-pick
if you checkout a local branch with certain tag or a name as another remote branch, and you try to push it another remote server then you get
$ git push -u origin v2.1.0
error: src refspec v2.1.0 matches more than one.
error: failed to push some refs to '[email protected]:abc/xyz.git'
How to fix:
git push origin refs/heads/v2.1.0:refs/heads/v2.1.0
http://stackoverflow.com/questions/9378760/git-push-local-branch-with-same-name-as-remote-tag
You can delete the files in the there and do a fresh checkout
$ rm *
$ git checkout .
<<<<<<< HEAD
foo
=======
bar
>>>>>>> cb1abc6bd98cfc84317f8aa95a7662815417802d
- the top half is the branch you a merging into
- the bottom half is from the commit that you are trying to merge in
https://wincent.com/wiki/Understanding_Git_conflict_markers
https://help.github.com/articles/importing-a-git-repository-using-the-command-line/
https://help.github.com/articles/importing-a-git-repository-using-the-command-line/
git clone --bare
git clone --mirror
and then you can clone from the local folder to anther place to get a working copy
git clone /bare/repo/dir.git mycheckout
https://stackoverflow.com/questions/12450245/getting-a-working-copy-of-a-bare-repository