Git

December 9th, 2020

.gitconfig

[user]
    name = XXX
    email = XXX
[credential]
    helper = manager
[core]
    autocrlf = true
[push]
    default = tracking
[branch]
    autosetuprebase = always
[pull]
    rebase = false

Line endings in Git

The key to dealing with line endings is to make sure your configuration is committed to the repository, using .gitattributes. For most people, this is as simple as creating a file named .gitattributes at the root of your repository that contains one line:

* text=auto

With this set, Windows users will have text files converted from Windows style line endings (\r\n) to Unix style line endings (\n) when they’re added to the repository.

Why not core.autocrlf?

Originally, Git for Windows introduced a different approach for line endings that you may have seen: core.autocrlf. This is a similar approach to the attributes mechanism: the idea is that a Windows user will set a Git configuration option core.autocrlf=true and their line endings will be converted to Unix style line endings when they add files to the repository.

The difference between these two options is subtle, but critical: the .gitattributes is set in the repository, so its shared with everybody. But core.autocrlf is set in the local Git configuration. That means that everybody has to remember to set it, and set it identically.

Source: Git for Windows: Line Endings

Additional:

 

How To

 

  • Copy a git repo without history (git documentation)

    --depth <depth>
    Create a shallow clone with a history truncated to the specified 
    number of revisions.
  • Stash the changes in a dirty working directory away
    git stash
    git status
    git log
    ...
    git stash pop
    git status
  • Update submodules

    git submodule update --init --recursive
    # download up to 8 submodules at once
    git submodule update --init --recursive --jobs 8
    git clone --recursive --jobs 8 [URL to Git repo]
    # short version
    git submodule update --init --recursive -j 8
    # 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
    ### etc
  • How to combine two commits into one commit?
    git rebase -i

    It performs an interactive rebase.
    If you’re currently on your “commit 1”, and the commit you want to merge, “commit 2”, is the previous commit, you can run

    git rebase -i HEAD~2

    and change the first word of the second line from “pick” to “squash”. Then write your file, and quit. Git will squash your first commit into your second last commit.

  • How to squash commits in git after they have been pushed?
    Squash commits locally with

    git rebase -i origin/featurebranch~4 featurebranch

    and then force push with

    git push origin +featurebranch

    featurebranch – can be master also

  • How can I undo the last commit?
    $ git reset --soft HEAD~1

    Reset will rewind your current HEAD branch to the specified revision. In our example above, we’d like to return to the one before the current revision – effectively making our last commit undone.

    Note the –soft flag: this makes sure that the changes in undone revisions are preserved. After running the command, you’ll find the changes as uncommitted local modifications in your working copy.

    If you don’t want to keep these changes, simply use the –hard flag. Be sure to only do this when you’re sure you don’t need these changes anymore.

    $ git reset --hard HEAD~1
    

    or

    git reset --hard origin/master
    
  • Remove Untracked Files (this or How to remove local untracked files from the current Git branch)
    Before running the actual command and removing untracked files and directories use the -n option that will perform a “dry run” and show you what files and directories will be deleted:

    git clean -d -n {dir_path}

    If some of the files listed above are important, you should either start tracking these files with git add <file> or add them to your .gitignore.

    Once you are sure you want to go ahead and delete the untracked files and directories, type (beware: this will delete files):

    git clean -d -f {dir_path}
  • Renaming Git Branch

    git branch -m {new_name}
  • Fixing conflict between master and feature branch (link)
    Fixing via merge:

    git fetch origin         # gets latest changes made to master
    git checkout feature     # switch to your feature branch
    git merge master         # merge with master
    # resolve any merge conflicts here
    git push origin feature  # push branch and update the pull request

    Fixing via rebase:

    git fetch origin         # gets latest changes made to master
    git checkout feature     # switch to your feature branch
    git rebase master        # rebase your branch on master
    # complete the rebase, fix merge conflicts, etc.
    git push --force origin feature

    Note carefully that the –force option on the push after rebasing is probably required as the rebase effectively rewrites the feature branch. This means that you need to overwrite your old branch in GitHub by force pushing.

    Regardless of whether you do a merge or a rebase, afterwards the conflicts should be resolved and your reviewer should be able to complete the pull request.

  • How to keep a git branch in sync with master
    git remote update
    git rebase origin/master

    or

    git checkout master
    git pull
    git checkout thefuturebranch
    git merge master
  • reset the local master to the origin master (or Git force pull to overwrite local files)
    git fetch origin master
    git reset --hard origin/master
    git clean -f -d
    

    or

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

    Warning: this is a hard reset, it will lose all your changes (committed* or not).

  • How merge two commits
    git rebase -i HEAD~2

    or

    git rebase --interactive HEAD~2
  • Viewing unpushed Git commits
    git log origin/master..HEAD

    or

    git log --stat origin/master..HEAD

    You can also view the diff using the same syntax

    git diff origin/master..HEAD

    If you want to see all commits on all branches that aren’t pushed yet, you might be looking for something like this:

    git log --branches --not --remotes

    And if you only want to see the most recent commit on each branch, and the branch names, this:

    git log --branches --not --remotes --simplify-by-decoration --decorate --oneline
  • Create Git Patch for Specific Commit (see Creating and Applying Git Patch Files)

    git format-patch -1 {commit_sha}
  • Apply Git Patch Files
    git apply {patch_file}
  • an internal housekeeping utility that deletes lost or “orphaned” Git objects
    git prune
  • Solution removing the history (stackoverflow)
    to prune the old commits. This makes the old commits and their objects unreachable:

    git fetch --depth=1

    To expire all old commits and their objects:

    git reflog expire --expire-unreachable=now --all

    to remove the old objects:

    git gc --aggressive --prune=all
  • a
  • z
  • x
  • s
  • te

 

 

 

 

Comments are closed.