Git

April 8th, 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

 

  • 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
    
  • 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}
  • 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 mobiledevicesupport
    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
  • te

 

 

 

 

Comments are closed.