Git

38 Notes
+ Proxy (March 27, 2022, 12:30 p.m.)

export http_proxy=http://proxy:8080 Set proxy for git globally: git config --global http.proxy http://proxy:8080 To check the proxy settings: git config --get http.proxy Just in case you need to you can also revoke the proxy settings: git config --global --unset http.proxy

+ Config (March 27, 2022, 12:30 p.m.)

git config http.postBuffer 1048576000 git config --global user.name "Mohsen Hassani" git config --global user.email "mohsen@mohsenhassani.com" git config --global color.ui true git config --global color.status auto git config --global color.branch auto git config --list

+ Start / init project (March 27, 2022, 12:29 p.m.)

cd my_project git init git remote add origin https://MohsenHassani@bitbucket.org/MohsenHassani/my_project.git git commit -m 'initial commit' git push origin master

+ Show username and email (March 27, 2022, 12:26 p.m.)

git config --list git config user.name git config user.email git config --list --system

+ Update repository (Dec. 13, 2020, 7:47 p.m.)

git fetch -p Prunes all stale references

+ Change HTTPS origin to SSH (Dec. 13, 2020, 7:58 a.m.)

git remote set-url origin git@git.mohsenhassani.com:Mohsen/tiptong.git

+ Change commits author (Oct. 23, 2020, 10:44 a.m.)

1- Update the author details git config user.name "Mohsen Hassani" git config user.email "mohsen@mohsenhassani.com" 2- Reset the author of all commits after a specific commit: - You need to get the commit ID using the command "git reflog". - The commit ID should the one earlier than the one you need to change the author. Because it will ignore the ID you are using, it will start from the next commint. - The following step, the "956951bf" is the comment ID. git rebase -i 956951bf -x "git commit --amend --reset-author -CHEAD" 3- An editor will get opened which shows what it is going to do (shows you the changes). Exit the editor CTRL+X 4- If the commits are already pushed to the server, push these changes too: git push --force-with-lease origin develop

+ Undo a Git Add (Oct. 22, 2020, 3:28 p.m.)

To unstage changes: git reset <file> OR git reset

+ Ignore file mode (chmod) changes (Oct. 22, 2020, 3:15 p.m.)

git config core.fileMode false git config --global core.fileMode false

+ Sync a branch with master (Sept. 21, 2020, 2:19 p.m.)

git checkout develop git pull origin master --rebase

+ Status (Sept. 20, 2020, 3:56 p.m.)

git status git status -s

+ Get remote URL (Sept. 17, 2020, 8:44 p.m.)

To get only the remote URL: git config --get remote.origin.url Then change the URL from: git@gitlab.mohsenhassani.com:Mohsen/tiptong.git To https://gitlab.mohsenhassani.com/mohsen/tiptong.git --------------------------------------------------------------- In order to get more details about a particular remote, use: git remote show origin ---------------------------------------------------------------

+ Log (May 6, 2020, 11:51 a.m.)

git log git log -p (Show the patch introduced with each commit.) git log --stat git log --graph git log --pretty=oneline git log --since=2.weeks # Also works with "2008-01-15" or "2 years 1 day 3 minutes ago" git log --pretty=oneline --abbrev-commit git log --pretty=oneline --author="Mohsen"

+ See changes of a commit (April 27, 2020, 9:18 a.m.)

git show <COMMIT> ---------------------------------------------------------------- Shows the changes made in the most recent commit: git show ---------------------------------------------------------------- Display the difference or changes of the last 3 commits: git log -p -3 ----------------------------------------------------------------

+ View logs of a user's commits (April 27, 2020, 9:17 a.m.)

git log --author="Mohsen"

+ See changes before pulling (March 15, 2020, 3:09 p.m.)

1- Fetch the changes from the remote: git fetch origin 2- Show commit logs of the changes: git log develop ..origin/develop 3- Show diffs of changes: git diff develop..origin/develop 4- Apply the changes by merge: git merge origin/develop Or just pull the changes: git pull

+ Clone a specific branch (Feb. 20, 2020, 8:55 p.m.)

git clone -b <branch> <remote_repo>

+ Server certificate verification failed. CAfile (Feb. 20, 2020, 5:46 p.m.)

git config --global http.sslverify "false"

+ git clean (Feb. 19, 2020, 12:53 p.m.)

Remove files from your working directory that are not tracked. If you change your mind, there is often no retrieving the content of those files. ----------------------------------------------------------------- A safer option is to run git stash --all to remove everything but save it in a stash. ----------------------------------------------------------------- git clean --dry-run git clean -d -n interactive mode git clean -x -i -----------------------------------------------------------------

+ .git/info/exclude vs .gitignore (Aug. 8, 2018, 10:06 a.m.)

gitignore is applied to every clone of the repo (it comes along as a versioned file), .git/info/exclude only applies to your local copy of the repository. ----------------------------------------------------------------------- The advantage of .gitignore is that it can be checked into the repository itself, unlike .git/info/exclude. Another advantage is that you can have multiple .gitignore files, one inside each directory/subdirectory for directory-specific ignore rules, unlike .git/info/exclude. So, .gitignore is available across all clones of the repository. Therefore, in large teams, all people are ignoring the same kind of files Example *.db, *.log. And you can have more specific ignore rules because of multiple .gitignore. .git/info/exclude is available for individual clones only, hence what one person ignores in his clone is not available in some other person's clone. For example, if someone uses Eclipse for development it may make sense for that developer to add a .build folder to .git/info/exclude because other devs may not be using Eclipse. In general, files/ignore rules that have to be universally ignored should go in .gitignore, and otherwise files that you want to ignore only on your local clone should go into .git/info/exclude

+ Change Remote Origin (Oct. 24, 2018, 1:56 p.m.)

git remote rm origin git remote add origin git@github.com:username/repositoryName.git git config master.remote origin git config master.merge refs/heads/master

+ Force Push (Oct. 14, 2018, 1:43 p.m.)

git push https://git.... --force git push --force origin ..... git push https://git.... -f git push -f origin .....

+ Cancel a local git commit (Feb. 25, 2019, 2:34 p.m.)

Unstage all changes that have been added to the staging area: To undo the most recent add, but not committed, files/folders: git reset . --------------------------------------------------------- Undo most recent commit: git reset HEAD~1 ---------------------------------------------------------

+ Delete from reflog (Feb. 25, 2019, 2:34 p.m.)

git reflog delete HEAD@{3}

+ Revert changes (Feb. 25, 2019, 2:33 p.m.)

Reverting a single file If the file isn’t committed: git checkout filename If the file is already committed: # filename is the path to your file, abcde is the hash of the commit you want to switch to. git checkout abcde filename or git reset abcde filename ------------------------------------------------------------------ Unstaged local changes (before you commit) Discard all local changes, but save them for possible re-use later: git stash Discarding local changes (permanently) to a file: git checkout -- <file> Discard all local changes to all files permanently: git reset --hard ------------------------------------------------------------------

+ Comparing two branches (Feb. 25, 2019, 11:50 a.m.)

git diff branch_1 branch_2

+ Rename a branch (Feb. 25, 2019, 2:31 p.m.)

1- Rename the local branch name: If you are on the branch: git branch -m <newname> If you are on a different branch: git branch -m <oldname> <newname> 2- Delete the old name remote branch and push the new name local branch: git push origin :old-name new-name 3- Reset the upstream branch for the new-name local branch: Switch to the branch and then: git push origin -u new-name

+ Delete a branch (Feb. 28, 2019, 7:47 a.m.)

Delete a Local GIT branch: Use either of the following commands: git branch -d branch_name git branch -D branch_name The -d option stands for --delete, which would delete the local branch, only if you have already pushed and merged it with your remote branches. The -D option stands for --delete --force, which deletes the branch regardless of its push and merge status, so be careful using this one! ------------------------------------------------------ Delete a remote GIT branch: Use either of the following commands: git push <remote_name> --delete <branch_name> git push <remote_name> :<branch_name> ------------------------------------------------------ Push to remote branch and delete: If you ever want to push your local branch to remote and delete your local, you can use git push with the -d option as an alias for --delete. ------------------------------------------------------

+ Fetch vs Pull (March 2, 2019, 8:33 a.m.)

In the simplest terms, git pull does a git fetch followed by a git merge. --------------------------------------------------- git fetch only downloads new data from a remote repository - but it doesn't integrate any of this new data into your working files. Fetch is great for getting a fresh view of all the things that happened in a remote repository. --------------------------------------------------- git pull, in contrast, is used with a different goal in mind: to update your current HEAD branch with the latest changes from the remote server. This means that pull not only downloads new data; it also directly integrates it into your current working copy files. This has a couple of consequences: Since "git pull" tries to merge remote changes with your local ones, a so-called "merge conflict" can occur. Like for many other actions, it's highly recommended to start a "git pull" only with a clean working copy. This means that you should not have any uncommitted local changes before you pull. Use Git's Stash feature to save your local changes temporarily.

+ Merge (March 4, 2019, 10:59 a.m.)

Switch to the production branch and: git merge other_branch

+ Untracking/Re-indexing files based on .gitignore (March 3, 2019, 11:35 p.m.)

git add . git commit -m "Some Message" git push origin master git rm -r --cached . git add . git commit -m "Reindexing..."

+ Stash (March 4, 2019, 2:27 p.m.)

git stash git stash pop git stash list git stash apply git stash show stash@{0} git stash apply --index ------------------------------------------------------------------------ git stash --patch Git will not stash everything that is modified but will instead prompt you interactively which of the changes you would like to stash and which you would like to keep in your working directory. ------------------------------------------------------------------------ Creating a Branch from a Stash git stash branch <new branchname> ------------------------------------------------------------------------

+ Submodule (Nov. 29, 2017, 4:47 p.m.)

Add a submodule to the existing project: git submodule add https://github.com/Mohsen/tiptong.git apps/tiptong ----------------------------------------------------------- Cloning a repository that contains submodules: git clone --recursive [URL to Git repo] ----------------------------------------------------------- If you already have cloned a repository and now want to load its submodules: git submodule update --init If there are nested submodules: git submodule update --init --recursive ----------------------------------------------------------- Pulling with submodules: Pull all changes in the repo including changes in the submodules: git pull --recurse-submodules pull all changes for the submodules git submodule update --remote ----------------------------------------------------------- Delete a submodule from a repository: git submodule deinit -f -- mymodule rm -rf .git/modules/mymodule git rm -f mymodule ----------------------------------------------------------- 1- CD to the path you need the module get cloned. 2- git submodule add https://github.com/ceph/ceph-ansible.git ----------------------------------------------------------- In case of this error raises: blah blah already exists in the index :-D git rm --cached blah blah and you should also delete the files from this path: rm -rf .git/modules/... ----------------------------------------------------------- To remove a submodule you need to: Delete the relevant section from the .gitmodules file. Stage the .gitmodules changes git add .gitmodules Delete the relevant section from .git/config. Run git rm --cached path_to_submodule (no trailing slash). Run rm -rf .git/modules/path_to_submodule Commit git commit -m "Removed submodule <name>" Delete the now untracked submodule files rm -rf path_to_submodule -----------------------------------------------------------

+ Commands (July 29, 2017, 9:56 a.m.)

git pull ------------------------------------------------- git fetch ------------------------------------------------- git pull master ------------------------------------------------- Create a branch: git checkout -b branch_name ------------------------------------------------- Work on an existing branch: git checkout branch_name ------------------------------------------------- View the changes you've made: git status ------------------------------------------------- View differences: git diff ------------------------------------------------- Delete all changes in the Git repository: To delete all local changes in the repository that have not been added to the staging area, and leave unstaged files/folders, type: git checkout . ------------------------------------------------- Delete all untracked changes in the Git repository: git clean -f ------------------------------------------------- Unstage all changes that have been added to the staging area: To undo the most recent add, but not committed, files/folders: git reset . ------------------------------------------------- Undo most recent commit: git reset HEAD~1 ------------------------------------------------- Merge created branch with master branch: You need to be in the created branch. git checkout NAME-OF-BRANCH git merge master -------------------------------------------------

+ Diff (July 29, 2017, 9:47 a.m.)

If you want to see what you haven't git added yet: git diff myfile.txt or if you want to see already-added changes git diff --cached myfile.txt

+ Modify existing / unpushed commits (Jan. 28, 2017, 1:42 p.m.)

git commit --amend -m "New commit message"

+ Delete file from repository (Jan. 28, 2017, 1:34 p.m.)

If you deleted a file from the working tree, then commit the deletion: git add . -A git commit -m "Deleted some files..." git push origin master ---------------------------------------------------------------------- Remove a file from a Git repository without deleting it from the local filesystem: git rm --cached <filename> git rm --cached -r <dir_name> git commit -m "Removed folder from repository" git push origin master

+ .gitingore Rules (Jan. 28, 2017, 1:26 p.m.)

A blank line matches no files, so it can serve as a separator for readability. A line starting with # serves as a comment. An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again. If a negated pattern matches, this will override lower precedence patterns sources. If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory. In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how path spec works in general in git). If the pattern does not contain a slash /, git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file (relative to the top level of the work tree if not from a .gitignore file). Otherwise, git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname. For example, Documentation/*.html matches Documentation/git.html but not Documentation/ppc/ppc.html or tools/perf/Documentation/perf.html. A leading slash matches the beginning of the pathname. For example, /*.c matches cat-file.c but not mozilla-sha1/sha1.c.