<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>DevMemo – git</title><link>https://devmemo.gitlab.io/categories/git/</link><description>Recent content in git on DevMemo</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="https://devmemo.gitlab.io/categories/git/index.xml" rel="self" type="application/rss+xml"/><item><title>Cheatsheets: Git Cheatsheet</title><link>https://devmemo.gitlab.io/cheatsheets/git/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://devmemo.gitlab.io/cheatsheets/git/</guid><description>
&lt;h2 id="commands">Commands&lt;/h2>
&lt;h3 id="new-repo">New Repo&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Create a new local git repo.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git init &lt;span style="color:#f92672">[&lt;/span>project name&lt;span style="color:#f92672">]&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Clone a git repo.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git clone &amp;lt;url&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Clone a git repo to a local dir.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git clone &amp;lt;url&amp;gt; &amp;lt;local-dir&amp;gt;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="config">Config&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Set your user name.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git config --global user.name &lt;span style="color:#e6db74">&amp;#34;name&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Set your email address.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git config --global user.email &lt;span style="color:#e6db74">&amp;#34;email&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Edit git global config.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git config --global --edit
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="branch">Branch&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># List all local branches.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git branch
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># List all local and remote branches.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git branch -a
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Check out to another branch.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git checkout &amp;lt;branch-name&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Check out to the previous branch.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git checkout -
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Create a new local branch and check it out.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git checkout -b &amp;lt;branch-name&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Delete a local local branch.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git branch -D &amp;lt;branch-name&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Merge the target branch changes into the current branch.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git merge &amp;lt;branch-name&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Merge a commit into the current branch.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git cherry-pick &amp;lt;commit-id&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Rebase the current branch onto the target branch.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git rebase &amp;lt;branch-name&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Fetch all remote branches from alias.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git fetch &lt;span style="color:#f92672">[&lt;/span>alias&lt;span style="color:#f92672">]&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Fetch and merge commits from tracking remote branch&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git pull
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Push a local branch to remote with same branch name.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git push &lt;span style="color:#f92672">[&lt;/span>alias&lt;span style="color:#f92672">]&lt;/span> &lt;span style="color:#f92672">[&lt;/span>branch&lt;span style="color:#f92672">]&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Push a local branch to another remote branch.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git push &lt;span style="color:#f92672">[&lt;/span>alias&lt;span style="color:#f92672">]&lt;/span> &lt;span style="color:#f92672">[&lt;/span>local-branch&lt;span style="color:#f92672">]&lt;/span>:&lt;span style="color:#f92672">[&lt;/span>remote-branch&lt;span style="color:#f92672">]&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Delete a remote branch&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git push origin --delete &amp;lt;branch-name&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Fetch master branch from other branch and rebase on it.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git fetch origin master:master &lt;span style="color:#f92672">&amp;amp;&amp;amp;&lt;/span> git rebase master
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="changes">Changes&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Show local changes.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git status
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Stage file.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git add &amp;lt;file&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Unstage file.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git reset &amp;lt;file&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Stage changes for all tracked files.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git add .
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Commit all staged changes.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git commit -m &lt;span style="color:#e6db74">&amp;#34;commit message&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Amend last commit&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git commit --amend -m &lt;span style="color:#e6db74">&amp;#34;new message&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Reset everything to last commit.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git reset --hard
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Delete all untracked files and directories.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git clean -df
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Undo local modifications to all files.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git checkout -- .
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Show non-staged changes.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git diff
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Show staged changes.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git diff --staged
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Save the current changes&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git stash
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># List all the stashed changes.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git stash list
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Pop the last stashed change.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git stash pop
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Drop the last stashed change.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git stash drop
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Show commit logs.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git log
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Show pretty commit logs.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git log --color --graph --pretty&lt;span style="color:#f92672">=&lt;/span>format:&lt;span style="color:#e6db74">&amp;#39;%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)&amp;lt;%an&amp;gt;%Creset&amp;#39;&lt;/span> --abbrev-commit --first-parent
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="tags">Tags&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># List all tags.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git tag
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Get remote tags.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git pull --tags
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Switch to an existing tag.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git checkout tags
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Create a new tag.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git tag -a tag_name -m &lt;span style="color:#e6db74">&amp;#34;tag message&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Push all tags to the remote repo.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git push --tags
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="remote">Remote&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Create a new alias for a git URL.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git remote add &amp;lt;alias&amp;gt; &amp;lt;url&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Show the remote repo names you have.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git remote
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Show the remote repo names and urls you have.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git remote -v
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Remote a remove repo.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git remote rm &amp;lt;remote-repo-name&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Change the URL of the origin alias.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git remote set-url origin &lt;span style="color:#f92672">[&lt;/span>git-url&lt;span style="color:#f92672">]&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="submodule">Submodule&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Create a submodule.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git submodule add &amp;lt;git-repo-url&amp;gt; &amp;lt;submodule-dir-path&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Init submodules.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git submodule init
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Update and init submodules recursively.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git submodule update -i -r
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Set a new url for the submodule.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git submodule set-url -- &amp;lt;submodule-dir-path&amp;gt; &amp;lt;new-git-repo-url&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Sync all submodules.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git submodule sync
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Execute command for each submodule.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git submodule foreach --recursive &amp;lt;command&amp;gt;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="aliases">Aliases&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>git config --global alias.co checkout
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git config --global alias.br branch
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git config --global alias.ci commit
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git config --global alias.st status
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item></channel></rss>