GitThese are my personal notes that I use as a quick help in my work.
|
|
git config --global user.name "My Name"
git config --global user.email e@mail.com
Show main configurations, with the related config file:
git config --show-origin --list
git config --global credential.helper store
To reset, remove the [credential]
section in the .gitconfig
file, and recreate with command above. The next time I enter the username and pw/token, they are stored.
Or, to reset, do:
git config --global --unset credential.helper
Instead of "global" try "local" for just the repository.
Instead of "store" use "cache", which stores for 15 minutes
To change the default password cache timeout, enter the following (default 15 minutes):
git config --global credential.helper 'cache --timeout=3600'
Of course, use a token instead of the password.
git clone https://github.com/.../....git
git checkout -b new-branch-name
to create a new branch and check it out
git checkout <the-branch-name> #
Switch to branch git branch -av #
See branchesgit status [file] #
See current statusgit add .
git add <filename>
git commit -m "a message summarizing the changes"
git commit --amend #
Opens the editor. The first line is like a summary. Leave a blank line, and put a detailed descriptiongit push -u origin <the-branch-name> #
Add the "-u"
When other team members have modified the remote code, you need to bring in those changes
Simplest command:
git pull origin --rebase
Sometimes, we want more control:
git checkout <working-branch> #
Switch to to local branchgit fetch origin #
If the response is empty, then no changes were downloaded: skip the restgit diff <working-branch> origin/<working-branch>
git merge origin/<working-branch> #
Merge remote into localgit status # everything should be committed !
git branch -av # verify which branch I am on
git checkout master # go to master
git fetch origin # do a merge is something was downloaded
git merge origin/master # bring in changes from server
git diff <working-branch> # list the differences between master and branch
# branch shows as minus, master as plus, q=quit
git merge <working-branch> # merge into master
# take note of the conflicts
git status
git diff <working-branch> # do this again: there should be no differences
git push origin master # push the merges up to repository
git fetch origin
git branch --merged # shows branches safe to merge (any others not listed)
git branch -d <working-branch> # remove the local branch, as it is merged into master
git push origin -d <working-branch># delete remote branch, as it is merged into master
git checkout -b v2 # create and checkout a new branch, ready for new code
# best practice to remove merged branches and start a new one
git fetch --all --prune # remove deleted branchesf
Stash current modifications and go back to HEAD:
git stash push
Or simply:
git stash
Stash with a comment:
git stash push -m "a comment"
You may have to do "git add filename
" before stashing a specific file with "git stash push filename
".
Show all stashes:
git stash list
Show a specific stash:
git stash show stash@{0}
Apply modifications from a stash:
git stash apply stash@{2}
If no argument, it applies the most recent.
The files go into stage if no conflict.
When conflicts are resolved, do git add ...
Apply and remove (git stash apply
does not remove):
git stash pop stash@{0}
git remove -v
List with detailsgit remote add abbrev git_url
(OK if remote already exists)git remote rename old_abbrev new_abbrev
git diff #
difference with what is committed
git diff main #
difference with main
git diff main [space] -- [space] a/file/name #
difference with main
for a given file
git diff branch1 branch2 [space] -- [space] a/file/name #
difference for a given file between two branches
Compare a local file in current branch to what is in the repo. Remember to fetch from the repo first.
git fetch origin #
Remember to fetch first
git diff -- file origin/branch_name/path/file #
Without path
git diff -- path/file origin/branch_name/path/file #
With path
To ignore a whole directory, add a .gitignore
file in the directory
with itself and "*", as follows:
.gitignore
*
git log # ` <small><i>History of commits</i></small>
git log -p <filename>
git log --oneline # ` <small><i>Shows one line per commit</i></small>
git log --oneline --graph # ` <small><i>Shows graph</i></small>
git log --no-merges <curr-br>..origin/master # ` <small><i>Shows only changes to the origin/master and not to <curr-br></i></small>
git checkout main/master
git fetch
git checkout <working-branch>
git fetch
git merge main/master #
Merge from main/master to branchgit status #
Look at conflicts and resolve with regular editorgit add .
git commit -m ". . ."
git push origin <working-branch>
git log filename
This shows the commit points. Select the commit point BEFORE the commit point you want to remove from the repo
Copy the code (remove .git from the copy after copying)
git reset commit-point-taken-from-above
This undoes all commits , but preserves the files
Commit code
git add .
git commit -m "..."
Verify that all code is correct. Compare to the copy made above.
At this point, there are two options:
1) If you did the reset to a local commit that is after the latest remote commit, then you are all set. Just do:
git push origin branch-name
2) If the remote repo has commits that have to be deleted, meaning that the commit point in the reset command above is before the latest remote commit, then create a new branch
git branch -b new-branch-name
Remove old branch
git branch -d old-branch
git push origin -d old-branch
git branch -m <new-name>
Important: you have to delete the old branch on the server and push the new branch.
Important: before renaming, be sure that the local branch is later or the same as the remote branch.
"error: failed to push some refs"
This worked: added "-u" in "git push -u origin the-branch"
Look for an old version of a file:
on web site, under the main code page, click on the clock with the arrow around it.
Chose a commit point in the past. For something a long time ago, click on the calendar.
git reset the-old-commit-point
# here, I still have the new version of my file, but it is un-committed
git restore the-file-name
# now, I have the old version